← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #156488]: Sleep function for certain time of day

 

Question #156488 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/156488

RaiMan posted a new comment:
--- the print statements?
running the script successfully gives you some intermediate information, about what the function is doing and shows you the intermediate content of the variables in the IDE's message area. You might just delete the print statements. And you might eliminate some intermediate variables:

def sleepUntil(h, m, s=0):
 a = time.localtime()
 at = time.mktime(a)
 bt = time.mktime(time.struct_time((a[0], a[1], a[2], h, m, s, 0, 0, a[-1])))
 wait(bt-at)

I left it "so complex", to give you a chance to understand, what is
happening, step by step.

--- this is the shortest possible version with the same effect (no print out) - all on one line!
def sleepUntil(h, m, s=0): a=time.localtime(); wait(time.mktime(time.struct_time((a[0], a[1], a[2], h, m, s, 0, 0, a[-1]))) - time.mktime(a))

and the usage still is
sleepUntil(14, 30)

Though this is valid Python, you should not want it ;-)

BTW: using reusable functions is the THE tool to HIDE complexity. Sikuli
script is a good example: You might not imagine how many statements,
functions and other programming constructs have been setup to make it
possible for you to say: click on that image that I can see on the
screen - and it works ;-)

Principally, the developers could integrate a waitUntil(some-hour, some-
minute) into Sikuli script (I do not think, they will do it ;-) So you
would not see the function, but it would be there. So complexity depends
on from where you are looking ;-)

--- alternative with no function:
every time, you want to wait until a specific time (e.g. 14:30) of day, you could just write:
a=time.localtime(); wait(time.mktime(time.struct_time((a[0], a[1], a[2], 14, 30, 0, 0, 0, a[-1]))) - time.mktime(a))

So no reusable function, and no nice sleepUntil(14, 30) :-(

So using functions is your decision based on your judgement about the
effort / benefit ratio.

--- one more thing:
the function does not check, whether the given time is in the future. If it is in the past, the script will fail with an error. Just try it and see what happens.

Always welcome.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.