Setting Default Region?

Asked by G-Mint

Hey

I was wondering if it was possible to set the default region manually away from SCREEN. I'm looking to do this as I'm setting up a mini framework where I have a bunch of reusable code in there own library file, which is imported as needed.

Breaking it down further, the specific problem I'm currently having (which this would solve) is I have methods in this library that themselves use Region methods, i.e. exist(). This causing a problem when I'm using a specific region (with 'with region:') as the library method does not recognize this and default back to the screen region.

So for example:

main file:

myReg = Region(App("Chrome").window())

with myReg:
    from import lib *
    eitherOr([img1, img2])

lib file:

from import sikuli *
def eitherOr(imageList):
        for x in range(0, len(imageList)):
            print x
            if(exists(imageList[x], 0)):
                return imageList[x]

The exists in the lib file does not pick up myReg as region.

Any help would be brilliant. Thanks!

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
j (j-the-k) said :
#1

I would do this (add an additional parameter to the method):

eitherOr(myRegion, imageList)

def eitherOr(myRegion, imageList):
        for x in range(0, len(imageList)):
            print x
            if(myRegion.exists(imageList[x], 0)):
                return imageList[x]

or this (set default region to another region):
oldSCREEN = SCREEN
SCREEN = myRegion

eitherOr(imageList)

def eitherOr(imageList):
        for x in range(0, len(imageList)):
            print x
            if(myRegion.exists(imageList[x], 0)):
                return imageList[x]

# if needed, set default region back to old default region.
SCREEN = oldSCREEN

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

@ j-the-k

I do not think,that your second version will work, because the "magic" of using SCREEN as default region, when using Region methods unqualified, is bound to the SCREEN object.
The object myRegion in your suggestion does not have this magic behavior.

BTW: there is a typo (copy/paste I guess): the second suggestion uses the original def unmodified:

def eitherOr(imageList):
        for x in range(0, len(imageList)):
            print x
            if(exists(imageList[x], 0)):
                return imageList[x]

see https://answers.launchpad.net/sikuli/+question/206119
for a SCREEN switching solution

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

@ Grant
the with-magic is only supported for methods belonging to the class and it's parents, that the with block was created for.
since every def() you use inside a with block does not belong to the with object, inside these def's the default "magic" SCREEN is used.

The first supposed solution is the "normal" one, but you need to change all your library functions, which I would do generally this way:

def eitherOr(imageList, myRegion=SCREEN):
        for x in range(0, len(imageList)):
            print x
            if(myRegion.exists(imageList[x], 0)):
                return imageList[x]

So SCREEN is used if you do not use a second parameter like
eitherOr([img1, img2])

But this solution is not very elegant together with the with feature.

The "real" solution would be, to completely override the Region class (including the original content) and add your functions.

As an alternative, you might experiment with setROI() instead of using with. This would avoid to adapt the lib functions.

myReg = Region(App("Chrome").window())
setROI(myReg)

# with myReg:
from import lib *
eitherOr([img1, img2])

setROI(Screen(0)) # reset to whole screen

Can you help with this problem?

Provide an answer of your own, or ask G-Mint for more information if necessary.

To post a message you must log in.