include() wanted by anyone else?

Asked by RaiMan on 2010-02-22

Wanted: include("filename")

to get some lines of code at the place of the include(), every time, the script is loaded and before the script is run. The included statements should never be saved. It should be possible, to make the added lines visible in the IDE, but normally they should be invisible. The line number reported for a selected line should be the effective number including the included lines to match the error reporting. It should be possible to set the base directory (may be the same as the picture dir), from where the includes should be taken. The pieces of code itself should be ...sikuli, to have the possibility, to contain pictures, that have to be included as well. May be that a special "save as include file" is necessary, to "namespace" the contained pictures.

the more you work with something like Sikuli, the more you have a bunch of workflows and functions or just a sequence of statements, that you want to use more often and especially in more than one script.

today you have to do some copy and paste from a repository (that should not be within Sikuli-IDE, because of the strange behaviour with saving scripts) with the drawback, that you have to manage the changes you make to the repository yourself.

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
TPasadena (chou-terrence) said : #1

My experience, use 'exec'
Example, (in Windows XP)
   I wrote a function called square(x) to calculate the square of x
        def square(x):
        r = x * x
        return r
   Save it in Sikuli folder called 'square'.

   Main program:
       exec open("D:\\Program Files\\Sikuli\\square.sikuli\\").read()
       x = square(80)
       print x

    Hope this answer can help.

RaiMan (raimund-hocke) said : #2

Big, big thank you. For me, its the tip of the year (this is possible, since 3 weeks ago, I only knew, that python is a programming language ;-).

In german we say, why search the horizon, when the good things can be found before your feet (or so ...).

With your tip I found execfile(), that only needs the filename.

So with only one global constant I can build a library, where even complete Sikuli scripts can be hosted including their pictures:

filing structure:
myLibrary # the base folder # only python code
   aScript # an adapted Sikuli script
to adapt a Sikuli you have to turn it into a def(): and adapt the references to the pictures.

in a script:
myLib="path to my base folder"
execfile(myLib+"") # to get my basic defs
aScript() # runs the adapted Sikuli script

with this construction its even possible to give parameters to a Sikuli script.

Tsung-Hsiang Chang (vgod) said : #3

Tsung-Hsiang Chang suggests this article as an answer to your question:
FAQ #1114: “Importing user defined Python modules in Sikuli”.

Gromadusi (gromadusi+fwd) said : #4

perfect! thanks a lot, that helps me a lot building a professional testing library (way to go though)