← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #212011]: great script

 

Question #212011 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/212011

    Status: Open => Answered

RaiMan proposed the following answer:
Hoping I got your question right:

In the first place using functions and loops.

In the second place modularization and using import.

*** functions:
code, that is used in different places and only varies slightly in the workflow but heavily in the values used, but always does the same manipulations can be put away in functions having parameters to reflect the different situations.

a function once defined:

def some_name(parameter1, parameter2, parameter3="default"):
    print parameter1
    if parameter3 == default:
          print parameter3, "using default value"
          return
    print parameter2

can be used all over the place with just one line:

some_name(1, 2)

*** loops
I saw many people here, who just copy&paste a part of the code and replace the values with other ones.
So the first step might be to use functions, to get it shorter and put the focus on the workflow.
but if you repeat something (e.g. 10 times calling the same function with different values), then you might think off a loop.
see faq 1437

*** modules
you might cut your code into pieces, that are executed one after the other.
Each of these pieces is hosted in its own .sikuli and called a module.
If you put all these modules in one folder, then you can make a main module like this:
import module1
import module2
import module3

The code in these modules will now be run one after the other, the same
way as if you have run it standalone in the IDE.

Usually all 3 methods are combined in larger scripts. 
so you could put all your functions in one module and say
from functionModule import *
some_name(1, 2)

supposing we have put the above function definition into
functionModule.sikuli

more on modules: http://sikuli.org/docx/globals.html#importing-other-
sikuli-scripts-reuse-code-and-images

*** One more thing
Generally it is vital to have a naming concept for your variables, images, functions and modules to avoid conflicts and keep it clear and robust. Speaking names are the first step to good scripts.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.