Syntax for exists()

Asked by surfdork

Raimund,

I've been using exists to find objects on screen. However, I don't fully understand the syntax.

Questions:

The following waits for image1 to exist. The last component "0" allows the script to continue when a match exists before the autotimeout of 3 seconds.

while not exists ("Image1.png", 0):

What I don't understand is:

What is the purpose of adding an indented wait()? is the indented wait() another finder?

while not exists ("Image1.png", 0):
       wait(1)

The wait is a imed pause that loops unitl a condition is met?
How is this not an infinite loop?

Simple question however I've not found a clear explination.

My boss is questioning my scripts and wondering what i'm dong to handle this condition.
Good question because I could not answer.

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

--- syntax
the 0 is only relevant for NOT found situations, since a find operation always comes back, when something is matched. The 3 seconds limit the waiting time if something is not there yet (adjustable with setAutoWaitTimeout). So exists(img, 0) is an elegant way to tell the search to come back after the first try (usually after <0.3 seconds). This may speed up scripts dramatically, that have to decide very often based on the existence of visual objects.
So the standard usage of exists() is in decisions, that influence the further workflow (if/else, while).
The general advantage of exists() is, that you do not have to handle exceptions with try: except:.

--- usage in a while
Your boss is right, your example is a (possible) endless loop. I only use it this way, if I'm sure, it should normally come up (I would realize, that the script is looping endless and kill it).

--- What is the purpose of adding an indented wait()? is the indented wait() another finder?
No, it is just a pause for 1 second (I normally use 2 as minimum), after one try to find - and this goes on maybe forever ;-).
The advantages over exists(img, FOREVER): you reduce cpu usage (normally 3-5 searches per second and more if possible) and you can add some code.

--- To make it robust,
you have to add some code to avoid this:
e.g.
max = 100
found = False
for i in range(max):
   if exists(img, 0): found = True; break
   wait(1)
if not found: popup("Sorry"); exit(1)

this would loop for max 100 seconds plus processing time of 100 searches (adds about 30 seconds).

You may put this in a def() if you need it more often:

def myWaitUntil(img, tStep = 2, nMax = 60, msg=None):
   found = False
   for i in range(nMax):
      if exists(img, 0): found = True; break
      wait(step)
   if not found and msg: popup(msg+"\n"+img)
   return found

--- usage examples:
if not myWaitUntil("some.png", 3, nMax=10, msg="Sorry, did not come up:"): exit(1)
# we stop the workflow, since it does not make sense to continue

-- or:
if not myWaitUntil("some.png"): # waits for about 70 - 80 seconds
   print "[myScript Error]", img, "did not come up"
   # some corrective/recovery actions
# workflow continues

If you want exact timing, use time.time()-based calculations of a max waiting time.

Can you help with this problem?

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

To post a message you must log in.