python time

MRAB python at mrabarnett.plus.com
Sun Mar 20 23:32:13 EDT 2011


On 21/03/2011 02:48, ecu_jon wrote:
> On Mar 20, 10:09 pm, Dave Angel<da... at ieee.org>  wrote:
>> On 01/-10/-28163 02:59 PM, ecu_jon wrote:
>>
>>
>>
>>> I'm working on a script that will run all the time. at time specified
>>> in a config file, will kick-off a backup.
>>> problem is, its not actually starting the job. the double while loop
>>> runs, the first comparing date works. the second for hour/min does
>>> not.
>>
>>> #python file
>>> import os,string,,time,getpass,ConfigParser
>>> from datetime import *
>>> from os.path import join, getsize
>>> from time import strftime,localtime
>>
>>> config = ConfigParser.ConfigParser()
>>> config.read("config.ini")
>>> source = config.get("myvars", "source")
>>> destination = config.get("myvars", "destination")
>>> date = config.get("myvars", "date")
>>> time = config.get("myvars", "time")
>>> str_time=strftime("%H:%M",localtime())
>>
>>> while datetime.now().weekday() == int(date):
>>>       while str_time == time:
>>>           print "do it"
>>
>> You're comparing two objects in that inner while-loop, but since they
>> never change, they'll never match unless they happen to start out as
>> matched.
>>
>> you need to re-evaluate str_time each time through the loop.  Make a
>> copy of that statement and put it inside the loop.
>>
>> You probably don't want those loops to be comparing to ==, though, since
>> if you start this script on some other day, it'll never loop at all.
>> Also, it'd be good to do some form of sleep() function when you're
>> waiting, so you don't bog the system down with a busy-loop.
>>
>> DaveA
>
> i guess im just having a hard time creating something like
> check if go condition,
> else sleep
>
> the double while loops take 12% cpu usage on my machine so this is
> probably unacceptable.

I would calculate the amount of time until the next event and then
sleep for that amount of time.

> also the sleep command does not like me :
>   >>>  from datetime import *

This imports everything (well, almost everything) that's in the
datetime module. One of those things is called "time".

Importing everything like this is not recommended because it 'pollutes'
the namespace.

>>>> from time import strftime,localtime,sleep

This imports 3 items from the time module, but the name "time" itself
still refers to what was imported by the previous line.

>>>> time.sleep(3)
>
> Traceback (most recent call last):
>    File "<pyshell#2>", line 1, in<module>
>      time.sleep(3)
> AttributeError: type object 'datetime.time' has no attribute 'sleep'
>>>>
>
This tells you that "time" (which you imported from the datetime
module) is a class in the datetime module.

You imported "sleep" from the time module, so refer to it as "sleep".

> here it is updated with the hour/min check fixed.
> #updated python code
> import os,string,time,getpass,md5,ConfigParser
> from datetime import *
> from os.path import join, getsize
> from time import strftime,localtime,sleep
>
> config = ConfigParser.ConfigParser()
> config.read("config.ini")
> date = config.get("myvars", "date")
> time = config.get("myvars", "time")
>
> while datetime.now().weekday() == int(date):
>      str_time=strftime("%H:%M",localtime())
>      while str_time == time:
>          print "do it"



More information about the Python-list mailing list