great script

Asked by felipe cavalcante

Hello!
I have a script of many lines, this leaves the Sikuli nerd, as I have repeated many lines would lessen the number of lines. What tips can you guys give me about this?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

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.

Revision history for this message
felipe cavalcante (felipel-2-0) said :
#2

Thanks RaiMan, that solved my question.