← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #162176]: User defined exceptions

 

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

    Status: Open => Answered

RaiMan proposed the following answer:
I think I know the history of your problem/challenge.

Defining your own exception does not really help, since I think you want
to connect the "not found" situation with a specific object, that is not
found.

So you have a 1-1 relation a -> b, that says if a not found, then do b.

In your case, a is some image or pattern object and b is some piece of
code to be run, if a is not found.

This is normally done with (I know you know that ;-)

if not exists():
   pass # handling if not found
else:
   pass # handling if found

this could be packed into a def(), so your code gets leaner:

a basic example (working on the whole screen):

def myCheck(img, msg=None):
    if not exists(img): # img might be an image filename, pattern object or text 
        if not msg:
             print("<%s> could not be found"%(img))
             return None
        else:
             popup(msg)
             exit()
        return None
    return getLastMatch()

theMatch = myCheck("some-img.png")
click theMatch # does nothing if not found, script continues

or

theMatch = myCheck("some-img.png", "this is a show stopper")
popup("script continues") # will not popup, if not found

so you could pack all needed actions into myCheck() and control the
effects with parameters as you like.

a more general approach could use a dictionary, that maps the object to
a handler, so you need some register function to manage your dictionary.

a basic example:

d = {} # the dictionary

def xH(): # should be processed for x
    print "i am handler xH"

def yH(): # should be processed for y
    print "i am handler yH"

# now we "register" relations   
x = "some-image.png"
d[x] = xH # for x, handler xH should be processed

y = Pattern(x)
d[y] = yH # for y, handler yH should be processed

print d # show our repository

d[x]() # run the action for x
d[y]() # run the action for y

hope it helps ;-)

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