Creating time markers in sikuli

Asked by Calvin on 2012-10-17

I wanted to set a timer and run at the moment I click run. Can sikuli import timer?
What about math?

Thanks and regards
Calvin

Question information

Language:
English Edit question
Status:
Answered
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2012-10-23
Last reply:
2012-10-23
RaiMan (raimund-hocke) said : #1

All of the built-in Python modules can be imported. Other modules have to be written in plain Python (no C-based stuff) or be available in a ported version for Jython.

If you know Java: every Java class/package can be imported and used.

Brian Redmond (bredmond) said : #2

Here's my method for timing my scripts:

from time import clock
from datetime import timedelta

scriptStart = clock()

#script body goes here

popup('Test Run completed. Time elapsed: ' + str(timedelta(seconds=(int(clock() - scriptStart)))))

RaiMan (raimund-hocke) said : #3

@ Brian
With Sikuli, this is the shorter one (since time is already imported by Sikuli ;-)

start = time.time()

#script body goes here

print "Elapsed: %d seconds"%(int(time.time-start))

RaiMan (raimund-hocke) said : #4

and again (missed the brackets :-(

print "Elapsed: %d seconds"%(int(time.time()-start))

Brian Redmond (bredmond) said : #5

Thanks for the tip and thanks for all your work on this project RaiMan!

Calvin (calvin-v99) said : #6

One more question: Can this start = time.time() used globally? I'm writing a function that when the timer reaches a specific time, it will return true. This function will be in a Actions Sikuli script which I will inherit to use in other scripts.

Calvin (calvin-v99) said : #7

Oh Ya and How to reset timer?

RaiMan (raimund-hocke) said : #8

start = time.time()
is not a timer.

it just stores the time at the moment this statement is processed into the value start.
So later on all around the place, where you have access to the variable start, you can check the time, that elapsed since start:
elapsed = time.time() - start

So without stepping into subprocessing or even event processing, you need a function that starts a new timer and can be asked, wether a given time has elapsed and a global storage for the timer values.

basic timer function:

def timer(name, elapse=0):
    if elapse > 0: # we start a timer
        exec("Settings.myTimer" + str(name) + "=time.time()+" + str(elapse)
    else # we check the timer
        rest = eval("Settings.myTimer" + str(name)) - time.time()
        if rest > 0: return False # not yet elapsed
        else return True

usage:
timer(1, 100) will start now and elapse in 100 seconds
# ... some code
if timer(1): print "timer1: time is over"

As global storage for the timer values I use the Sikuli Settings class.

Since the timer not really does anything, it is just storing a value for later comparison, you do not need a "reset".

just repeating the above sequence would give a new value to Settings.myTimer1

Can you help with this problem?

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

To post a message you must log in.