making loops and adding a counter

Asked by Richi

Hello,

im currently using sikuli and its really fantastic…

my sikuli script below allows me to know the time taken to play the scenario.

import time
a = time.clock()

doubleClick("image1.png")
wait(1)
hover("image2.png")
sleep(2)
click("image3.png")
sleep(2)
hover("image4.png")
click("image5.png")

print " Run time: ",time.clock()-a,"seconds"

I would like to

1) play the above scenario 5 times(5 loops)

2) and also i would like to add a counter which can allow me to know how many loops sikuli has done.

I would like to know which code lines that i have to add in my above script in order that i have perform the 2 tasks.

thanx

richi

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
Eugene S (shragovich) said :
#1

Hi ,

To do what you want, simple loop will do the job. Just enclose whatever code you have into it and add a simple counter. For example something like that:

import time

count = 1
for i in range(4): #To run 5 times
     print "This is run number %s" %(count)
     a = time.clock()

     doubleClick("image1.png")
     wait(1)
     hover("image2.png")
     sleep(2)
     click("image3.png")
     sleep(2)
     hover("image4.png")
     click("image5.png")

     print " Run time: ",time.clock()-a,"seconds"
     print "_____________________"
     count += 1

Revision history for this message
Richi (ateeshr) said :
#2

Hi,

i thank you very much, i have tested the code below and it works really well.

import time

count = 1

for i in range(4): #To run 5 times

     print "This is run number %s" %(count)

     a = time.clock()

     doubleClick("image1.png")
     wait(1)
     hover("image2.png")
     sleep(2)
     click("image3.png")
     sleep(2)
     hover("image4.png")
     click("image5.png")

     print " Run time: ",time.clock()-a,"seconds"
     print "_____________________"
     count += 1

Well, i would like to know how can i add a log file in the above code ,
that can leave a trace after every loop?

As such, for example , if after the 3rd loop, sikuli stops, a log file is generated in which it states that ,at the 3rd loop, sikuli stopped.

what code lines can i add?

thank you

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

use
Debug.user("some message")

which gives you some more options like writing to a file:
before the first use of Debug.user() write:

Debug.setUserLogFile("path to your log file")

Debug.user can be used according to the Java String.format rules

example:
Debug.user("finished %d loop", i)

use %d for numbers and %s for strings

Revision history for this message
Richi (ateeshr) said :
#4

Hi,

i have tried the code lines below but it is not working.

this is the path to my log file as i want the log file to be on the desktop--> C:\\Users\\Richi\\Desktop\\

Can someone help me to modify the code lines below in order that :

if after the 3rd loop, sikuli stops, a log file is generated in which it states that ,at the 3rd loop, sikuli stopped, please?

import time

count = 1

for i in range(4): #To run 5 times

     print "This is run number %s" %(count)

     a = time.clock()

     doubleClick("image1.png")
     wait(1)
     hover("image2.png")
     hover(“image3.png)
     wait(2
     click("image5.png")

     print " Run time: ",time.clock()-a,"seconds"

     print "_____________________"

    dir = “C:\\Users\\Richi\\Desktop\\”

    Debug.setUserLogFile("dir")

    Debug.user("finished %d loop", i)

     count += 1

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

the log file must denote a file, not only a directory:

 Debug.setUserLogFile("dir" + "mylogs.txt")

this should work
dir = “C:\\Users\\Richi\\Desktop\\”
Debug.setUserLogFile("dir" + "mylogs.txt")

for i in range(4): #To run 5 times

  print "This is run number %s" %(i)

  a = time.clock()

  doubleClick("image1.png")
  wait(1)
  hover("image2.png")
  hover(“image3.png)
  wait(2
  click("image5.png")

  print " Run time: ",time.clock()-a,"seconds"

  print "_____________________"

  Debug.user("finished %d loop", i)

BTW: take care for consistent indentation (see faq 1800)

Can you help with this problem?

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

To post a message you must log in.