onChange runs without happening any change

Asked by Pablo Frank

When i run "caller.sikuli" (see below), the doubleClick("firefoxIcon") in doubleClicker.sikuli (see below) doubleclicks without pause after a change happens in Region(761,14,605,422). I just want the "firefoxIcon" to be doubleClicked *once* for *one* change, but i don't find the way.

caller.sikuli

def caller():
    import doubleClicker
    doubleClicker.A()

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

doubleClicker.sikuli

def pop(event):
   doubleClick("firefoxIcon")
   wait(2)

def A():
 reg =Region(761,14,605,422)
 reg.onChange(1, pop)
 reg.observe(20)

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Pablo Frank
Solved:
Last query:
Last reply:

This question was reopened

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

# --- caller.sikuli

import doubleClicker #---1

def caller():
    doubleClicker.A()

caller()

# --- doubleClicker.sikuli

def pop(event):
   doubleClick("firefoxIcon")
   event.region.stopObserver() #---2

def A():
 reg =Region(761,14,605,422)
 reg.onChange(pop) #---3
 reg.observe(20)

#---1 import should not be in a def()

#---2 if you want to stop the observation after the first detected change

#---3 normally a change of a square containing 50 pixel ( 7 x 7 pixels ) triggers a change event. This is a good value to detect even small changes that make sense. (your 1 as 1st parameter triggers with 1 pixel out of about 255.000 ;-)

Revision history for this message
Pablo Frank (frank-pablo) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
Pablo Frank (frank-pablo) said :
#3

#---1 import should not be in a def() <----- I've done a bunch of script where a function just import other functions, i mean this:

def XXXX():
                    import y
                    y.z()
                    import e
                    e.p
                    etc...

It works ok. why should i change that?

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

Sure, it works. but an import needs only to be processed once per module when this is loaded. The imported stuff is put into the modules global context and can be used everywhere in the module.

In your case, the import has to be processed each time, when the def() is called, since it has a local context with its own local namespace, which is built again on each entry.

This is a huge wast of resources (stack memory and processing time).

There is no need at all, to have the imports inside the def()s. If you put them on top of your script, it is all the same for your coding.

It might be, that you have only one def() per import. If this is the case, you have to rethink your module concept.

Usually it is - example:
# module myLib
def x1(i):
...

def x2():
...

def x3():
...

def x4():
...

def x5():
...

# script main
import myLib

def func1(i):
      myLib.x1(i)

for i in range 10:
   func1(i)

Revision history for this message
Pablo Frank (frank-pablo) said :
#5

thanks