Improve copy paste between Sikuli scripts

Asked by Tim Chan

I want to copy paste some code from one Sikuli script to another.
I noticed that only the text is copied over and not the .png files.

Could this be improved in future releases?

Is there a better way for me to reuse logic/code?

Question information

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

Reuse code and/or images:
look: https://answers.launchpad.net/sikuli/+question/127275

The new version Sikuli X will support this even better, but copying code including pictures from one tab to another in the IDE will not be supported yet.

Revision history for this message
Tim Chan (tim-chan) said :
#2

Hi RaiMan,

I was able to utilize the code/image reuse, but when I try to incorporate it into a unit test, I get the following error:

Exception in thread "TestRunner-Thread" TypeError: run() takes exactly 1 argument (2 given)
Running...

Is there an example with code/image reuse, running as a unit test?

Revision history for this message
RaiMan (raimund-hocke) said :
#3

I guess, when you tested the import/reuse feature, it was with some experimental code??
This error means, that you are using run() in a wrong way. Has nothing to do with import/reuse.

what are you using run() for and how?

pls. paste your statement.

Revision history for this message
Tim Chan (tim-chan) said :
#4

Below is the script I am running. I'm running it as a unit test. I am not calling the run() method myself.

----

import sys

myLib = "/home/tchan/Workspace/Sikuli-Scripts/loginlogout.sikuli"
if not myLib in sys.path:
 sys.path.append(myLib)

# making unit reload without restarting IDE (workaround)
if 'loginlogout' in sys.modules:
 del sys.modules['loginlogout']
 loginlogout = None
 del loginlogout

# import
from loginlogout import *

def testNada(self):
 print "hello"
 assert not exists( )

Revision history for this message
RaiMan (raimund-hocke) said :
#5

Sorry, it seems, that I sent you on a buggy way ;-)

In theorie it should work. But somehow these imports inside the test class (which is wrapped around by the Siklui test feature) make the whole thing hang (the run() seems to be the internal call of the feature, that runs the test).

I tried with execfile(), and that works. Not as flexible and convenient as import, but ...

It would look like this:

execfile("/home/tchan/Workspace/Sikuli-Scripts/loginlogout.sikuli/loginlogout.py")

def testNada(self):
 print "hello"
 assert not exists( )

execfile() works as if the contained lines were in your script.

Revision history for this message
Tim Chan (tim-chan) said :
#6

I see. So if I had two functions in my loginlogout.py class then I will need to break them out into two separate python scripts?

Revision history for this message
Tim Chan (tim-chan) said :
#7

I'm using exefile() and I'm getting this error: Cannot load target image __pyclasspath__/1289956715719.png

Revision history for this message
RaiMan (raimund-hocke) said :
#8

Just tested with a pre-version of the new version Sikuli X, that comes with an built-in import feature for .sikuli's.

There the import solution works in the test environment.

So I suggest to live with execfile() in the moment, to get things going. When Sikuli X is available, you can easily switch to import based on your reusable .sikuli's.

Revision history for this message
RaiMan (raimund-hocke) said :
#9

question:
I will need to break them out into two separate python scripts?
answer:
no leave everything as is. what is read using execfile() is just like inline code.

error:
Cannot load target image __pyclasspath__/1289956715719.png
answer:
in your loginlogout.py seems to be an image reference like "__pyclasspath__/1289956715719.png". What do you think should that be? __pyclasspath__ is a magic member of sys.path, that in normal code has no meaning. If you have images in your loginlogout.sikuli, you have to either reference them with full-path or use setBundlePath().

With the current possibilities (either with import or with execfile) handling the images is a challenge. For library functions I use a central image repository and full-path references.

Again here the new version Sikuli X will solve this issue.

Can you help with this problem?

Provide an answer of your own, or ask Tim Chan for more information if necessary.

To post a message you must log in.