Using A list of regions --- wrong usage of sorted()

Asked by geo

Hello ,
i have run into a problem when using lists in sikuli.
i have 8 regions in a list and the code is supposed to run through this list checking for a match, In this case its "img"
if the match has been found it returns 5 ,if not 10 and it is supposed to sort it .The final part would be to highlight the regions without the match .
But every time i click run the code Sikuli just disappears with no errors or any warning .Any idea whats wrong with my code ?
Code :
ListR = [(Region1),(Region2),(Region3),(Region4),(Region5),(Region6),(Region7),(Region8)]
img =Pattern("img.png").similar(0.67)
# Sort key function
def crosscheck(img):
    for x in range(0, 8):
        y = ListR[x]
        if y.find(img):
            return 5
        else:
            return 10
# Main function
while exists("loopImage.png"):
    sortedMatches = sorted(ListR, key=crosscheck)
    ListR[5].highlight(1)
    ListR[6].highlight(1)
    ListR[7].highlight(1)

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

Your usage of the sorted function does not make sense.
That it does not come back might be because your usage produces internally an endless loop - just guessing.

Ignoring your intention, the correct usage would be:

# variable names should start with lowercase letter
listR = [region1, region2, region3, region4, region5, region6, region7, region8]
img =Pattern("img.png").similar(0.67)
# Sort key function
def crosscheck(reg): # the parameter is the next list element to be sorted
    if reg.exists(img):
         return 0
    return 1
# Main function
while exists("loopImage.png"):
    sortedMatches = sorted(listR, key=crosscheck)

this would sort the regions, where the img was found, to the start of the list (with no specific sequence) and the others towards the end. But you do not have an information, in which regions the image was found.

matches = [] # to get the matches
listR = [region1, region2, region3, region4, region5, region6, region7, region8]
img =Pattern("img.png").similar(0.67)

for reg in listR:
    matches.append(reg.exists(img, 0)) # only 1 search, do not wait on not found

for n in range(len(listR)):
    if matches[n]:
        listR[n].highlight(1)

the information, which region has a match, is preserved in matches in the same order as listR.
The second loop highlights only regions, that had a match in the first loop.

Can you help with this problem?

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

To post a message you must log in.