← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #210973]: Sikuli code organization / code reuse

 

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

David posted a new comment:
RaiMan, I'm sorry to say, but this just isn't working. Furthermore, your
example doesn't make much sense because you're calling the object's
attribute, and not the method.

So let's say we have a class sub:

class sub:
         i = "12345"
         def doSomething(self):
                  pass
         def doSomethingElse(self):
                  pass
         def doAnotherThing(self):
                  pass

I'm sure you know this, but I want to make sure we're on the same page
here. In other words you could say:

sub().doSomething()

and this would dynamically create an object instance in Python that
would be created just for executing this method.

Usually though, it isn't efficient to do this because it will assign a
new object every time. An example of this would be:

sub().doSomething()
sub().doSomethingElse()
sub().doAnotherThing()

Instead you usually would create an object instance and use that so you
don't have to create a new object every time:

s = sub()
s.doSomething()
s.doSomethingElse()
s.doAnotherThing()


So let's go back to your example:

import sub
class test:
          def s1(self):
               sub.doSomething()

The problem with this is that sub.doSomething() returns sub's attribute
"doSomething", which doesn't exist. If you ran this code you would get
the error:

AttributeError: 'module' object has no attribute 'doSomething'

Instead, if we changed the code to the following, it would return the
attribute i as the string "12345":

import sub
class test:
          def s1(self):
               return sub.i

But this still doesn't let me instantiate the object properly. Not only
that, but I'm running into a whole bunch of problems with this approach,
which I've outlined below.

--------------------------------------------------------------------------------------------------------------


When I do:

import sub

I get the error:
"No module named sub"

Even though I have sub.sikuli in the same directory as test.sikuli

In other words, my directory structure is:

/test/
/test/sub.sikuli
/test/test.sikuli

If I explicitly get the path, it resolves this problem:

import os
myPath = os.path.dirname(getBundlePath())
if not myPath in sys.path: sys.path.append(myPath)

but then I still can't properly instantiate an object

import sub
class test:
	def t1(self):
		s = sub() #TypeError: 'module' object not callable
		s.s1()  


I'm using Sikuli X-1.0rc3(r905)

I wish what you said above worked. This is making me sad. :(  I tried to
include as many details as I possibly could to try to resolve the issue.

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