How to get a final sorted match list after more than one findAll()

Asked by eduardobedoya

Hi Rainman, hope I'm not abusing, this is the most difficult script I would ever dare to make, I made a kinda pseudocode, hope you could help me. Thanks Advanced.

I have a little 330 x 600 px window (it can't be resized) inside chrome, inside a flash website, with this inside...

* __ idxxx __ somename
        idxxy __ anothername
        idxxz __ anewname
* __ idxyx __ yetsomename
        idxyy __ yetanothername
* __ idxyz __ yetanewname
        idyxx __ andyetsomename
* __ idyyx __ andyetanothername
        idyzx __ andyetanewname

It is a table listing some names, with a scrollbar at the right side (cuz the list is larger than 9 names), also some names have at their left side a "StarImage" (represented by starsign) that are either orange or black.

I would like to count from top to bottom how many "StarImages" are no matter if they are orange or black, any of both colors count as one. (eg. if there are 3 black and 3 orange, then are 6 in total)
Then I would like to create a variable named...
toclickonVar = 2
So if "toclickonVar = 2" I will click on the second "StarImage" found from top to bottom.

But if for example "toclickonVar = 6" and the total "StarImage" count was 4 (like in example table above), so ImageCount < "toclickonVar" then I will like to subtract ImageCount from "toclickonVar" and then press the PageDown key and the Down key (in order to scroll down), then count again how many "StarImages" are from top to bottom and click on the new(current) "toclickonVar" if it still is greater than the new(current) "StarImages" count, then repeat the steps described in this paragraph from the beginning to the end. (I guess this paragraph is a kindof loop)

Is this possible? I thought about the easiest way to achive this, if you got better idea pls correct me. So far I have write this...

App.focus("Chrome")
Settings.DelayAfterDrag = 0
Settings.DelayBeforeDrop = 0
Settings.MoveMouseDelay = 0
hover(Pattern("1427842411231.png").similar(0.94))
mouseDown(Button.LEFT)
count = 0
try:
    matches = list(findAll(Pattern("1425879837185.png").exact()))
    count = len(matches)
except FindFailed:
    pass #nothing to do
print "found:", count

if count > 0:
    sortedMatches = sorted(matches, key=lambda m:m.y) # sorts top to bottom
    hover(sortedMatches[-1]) # take the last one
mouseUp(Button.LEFT)

You helped me with this code, but this only count one type of images, I need to find two colors of StartImages, I either don't know how to click on second, third, forth. Thanks Advanced again man.

Question information

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

This question was reopened

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

- for every different element (the stars in your case) make a findAll(), which gives you a resulting match list.
this might well be put in a def() to reuse it.

- when all findAll() have been processed, combine the match list to on match list.

- sort the list as needed

- use it

Revision history for this message
eduardobedoya (gonnabdh) said :
#2

Hi Rainman, pls tell me what is the syntax for setting a variable?, what about adding, substracting variables? any docs? thanks advanced.

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

thanks, currently reading Python link
Please tell me, when you say, "combine match list to on match list" how do I do that?

THanks Advanced.

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

above mentioned tutorial chapter 5.1

Revision history for this message
eduardobedoya (gonnabdh) said :
#7

Thanks RaiMan, that solved my question.

Revision history for this message
eduardobedoya (gonnabdh) said :
#9

Hi Raiman, I've read the python tutorial, and Im currently reading the sikuli one
I got the script to do what I was intending to, but it takes 6 seconds to complete, I would like to double the speed.
I guess I have some useless stuff cluttering the script, please could you take a look at it and tell me if there could be some fixes?
Thanks Advanced, this is my script...

App.focus("IrfanView")
Settings.DelayAfterDrag = 0.5
Settings.DelayBeforeDrop = 0.5
Settings.MoveMouseDelay = 0.5

WhtNumImg = 0
try:
    matches = list(findAll(Pattern("1429333732294.png").exact()))
    WhtNumImg = len(matches)
except FindFailed:
    pass #nothing to do
if WhtNumImg > 0:
    sortedMatchesA = sorted(matches, key=lambda m:m.y) # sorts top to bottom

BlkNumImg = 0
try:
    matches = list(findAll(Pattern("1429334230870.png").exact()))
    BlkNumImg = len(matches)
except FindFailed:
    pass #nothing to do
if BlkNumImg > 0:
    sortedMatchesB = sorted(matches, key=lambda m:m.y) # sorts top to bottom

WhtStartImg = 0
try:
    matches = list(findAll(Pattern("1429334355293.png").exact()))
    WhtStartImg = len(matches)
except FindFailed:
    pass #nothing to do
if WhtStartImg > 0:
    sortedMatchesC = sorted(matches, key=lambda m:m.y) # sorts top to bottom

BlkStartImg = 0
try:
    matches = list(findAll(Pattern("1429334504677.png").exact()))
    BlkStartImg = len(matches)
except FindFailed:
    pass #nothing to do
if BlkStartImg > 0:
    sortedMatchesD = sorted(matches, key=lambda m:m.y) # sorts top to bottom

# I first tried to apped all matches in this way below, but I it added the numbers in this way... 7,0,1,1
#AllImg=[]
#AllImg.append(WhtNumImg)
#AllImg.append(BlkNumImg)
#AllImg.append(WhtStartImg)
#AllImg.append(BlkStartImg)

# Instead combining all matches in this way below give the result 9 instead of 7,0,1,1 (the same applied to AllsortedMatches)
AllImg = WhtNumImg + BlkNumImg + WhtStartImg + BlkStartImg

AllsortedMatches = sortedMatchesA + sortedMatchesC + sortedMatchesD

if AllImg > 0:
    FinalsortedMatches = sorted(AllsortedMatches, key=lambda m:m.y) # sorts top to bottom

for index, item in enumerate(FinalsortedMatches):
    hover (item)
    print index, item

print "AllImg:", AllImg

Beasides I still have a little bug in my script above:
The second image to find (sortedMatchesB) doesn't appear in my screen, and when this happens, if I add sortedMatchesB to AllsortedMatches I get an error saying that sortedMatchesB doesn't exists. In my screen every A, B, C, D image may appear or may not, how can I add them even if they are not found in screen, do I just create a variable sortedMatchesB = 0???
Could restricting the search area improve the script speed?

Thanks Advanced.

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

you have 4 findAll() in your script: each takes some 100 millisecs.

depending on the achievable restricted region, this might drop down to some 10 millisecs.

... but before end, you hover across the found matches. since you have the MoveMouseDelay at 0.5 seconds, with 9 elements, this counts up to 4.5 seconds ;-)

So it seems, that your findAll()s are already working rather fast.

to measure elapsed time:

start = time.time()
# some work
print time.time() - start

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

aaah, your "bug" and list usage

--- AllImg.append(WhtNumImg) adds a new element to the list, but not the elements of the list.
What you need is:
AllImg.extend(sortedMatchesD) # see docs ;-)

to avoid the not exist problem:
sortedMatchesA = []

add it somewhere at the beginning for each sorted...
So you have at least an empty list in all cases.

Revision history for this message
eduardobedoya (gonnabdh) said :
#13

Yes Raiman, Thanks for the "list.extend(list)" thing
Sikuli IDLE warn me about the need to use an empty list "list=[]" xD
So I fixed the script, but it still takes 6 seconds before any mouse move or hover.
Could restricting the search area improve the script speed? How could I increase speed??

This is my Script:

App.focus("IrfanView")
Settings.DelayAfterDrag = 0
Settings.DelayBeforeDrop = 0
Settings.MoveMouseDelay = 0

try:
    MatchesWNI = list(findAll(Pattern("1429333732294.png").exact()))
    WhtNumImg = len(MatchesWNI)
except FindFailed:
    WhtNumImg = 0
    MatchesWNI = []

try:
    MatchesBNI = list(findAll(Pattern("1429334230870.png").exact()))
    BlkNumImg = len(MatchesBNI)
except FindFailed:
    BlkNumImg = 0
    MatchesBNI = []

try:
    MatchesWSI = list(findAll(Pattern("1429334355293.png").exact()))
    WhtStartImg = len(MatchesWSI)
except FindFailed:
    WhtStartImg = 0
    MatchesWSI = []

try:
    MatchesBSI = list(findAll(Pattern("1429334504677.png").exact()))
    BlkStartImg = len(MatchesBSI)
except FindFailed:
    BlkStartImg = 0
    MatchesBSI = []

#This below combine the Imgs giving this result [7, 0, 1, 1]
#AllImg=[]
#AllImg.append(WhtNumImg)
#AllImg.append(BlkNumImg)
#AllImg.append(WhtStartImg)
#AllImg.append(BlkStartImg)

#This below combine all Imgs giving this result [7, 0, 1, 1]
#AllImg = [WhtNumImg] + [BlkNumImg] + [WhtStartImg] + [BlkStartImg]

#This below combine all Imgs giving this result 9
AllImg = WhtNumImg + BlkNumImg + WhtStartImg + BlkStartImg

#This below combine all Matches giving a result that can be sorted
AllMatches=[]
AllMatches.extend(MatchesWNI)
AllMatches.extend(MatchesBNI)
AllMatches.extend(MatchesWSI)
AllMatches.extend(MatchesBSI)

#This below combine all Matches giving a result that can be sorted
#AllMatches = MatchesWNI + MatchesBNI + MatchesWSI + MatchesBSI

if AllImg > 0:
    SortedAllMatches = sorted(AllMatches, key=lambda m:m.y) # sorts top to bottom

#for item in FinalsortedMatches:
# hover(item)

for index, item in enumerate(SortedAllMatches):
    hover (item)
    print index, item

print "AllImg:", AllImg

Thanks Advanced.
Please tell is there is something else I could do to improve speed in this code.

Revision history for this message
eduardobedoya (gonnabdh) said :
#15

Hi Raiman
This job performance gave me several doubts about finding images inside restricted areas, I asked in this link https://answers.launchpad.net/sikuli/+question/265446
I guessed I should post them in a new question in order to avoid out of topic.
Thanks Advanced

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

user switches to new question