sikuli-driver team mailing list archive
-
sikuli-driver team
-
Mailing list archive
-
Message #13810
Re: [Question #210973]: Sikuli code organization / code reuse
Question #210973 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/210973
Status: Open => Answered
RaiMan proposed the following answer:
--- Python
...does not matter for Sikuli. it uses the bundled Jython 2.5.2, which is a Java implementation of Python on language level 2.5
--- Sikuli's automatic import using the form "import module" is broken
... is not true. It just uses the Python original import feature after intervening the fact, that the .py is found in a .sikuli when you say
import something
and something.sikuli is found in one of the standard places (mainly same
directory and sys.path)
--- your example giving the name error
sub.sikuli
--------
from sikuli import *
class sub:
def doSomething(self):
popup("it worked")
test.sikuli
-------
import sub
sub().doSomething() #TypeError: 'module' object is not callable
this gives an error in C-Python too, because you mean the class name
sub, which is not known in the current namespace (would only be, if you
would use <from sub import *>.
correct usage in this case:
sub.sub().doSomething()
**** your conclusion is completely wrong
... except one thing: something like "Sikuli packages" is not easily possible like with the Python __init__()
your #1: wrong see above
your #2: namespace conflicts mean, that if the same name exists in the
current namespace and the namespace of the imported module, this name
will only be reachable in this case using sub.name, because name alone
will reference the object in the current namespace (each module in
Python has its own namespace, plus the differentiation inside a module
in global and local (inside classes and refs).
your #3: nearly wrong: currently you have to put all folders in the
structure containing .sikuli folders to be imported into sys.path, since
Sikuli not yet supports package structure (but I put it on the list)
Sikuli's import feature was mainly implemented, to allow each module to
have its own image store (but an image filename should be unique in a
structure of imports, since the first image top down sys.path is taken)
So besides the missing package feature everything with importing .sikuli
works as expected.
BTW: the construct
if __name__ == '__main__':
is only needed in a Python module if it can be used as an imported module or as the top level module given to the Python interpreter.
IMHO in Sikuli scripts this is not needed.
One situation:
if you implement tests for the sub module features inside the submodule, this can be elegantly masked this way, so the test are run only, if the submodule is run as a main.
--
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.