python time

E

ecu_jon

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"

#config file
#change the time variable to "now"+a min or two to observe
#this needs to be where ever the actual python script is
[myvars]
#only set source if you want it to be different
source: c:\users\jon
destination: \\mothera\jon\
#what day of the week to perform backup?where Monday is 0 and Sunday
is 6
date: 6
#time to start
time: 21:02
 
D

Dave Angel

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
 
E

ecu_jon

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.
also the sleep command does not like me :
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
time.sleep(3)
AttributeError: type object 'datetime.time' has no attribute '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"
 
E

ecu_jon

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.
also the sleep command does not like me :
 >>> from datetime import *


Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    time.sleep(3)
AttributeError: type object 'datetime.time' has no attribute '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"

i think this is what you are talking about
except that the time.sleep just does not work.
even changing "from time import strftime,localtime" to "from time
import strftime,localtime,sleep" does not do it.
#python code
import os,string,time,getpass,md5,ConfigParser
from datetime import *
from os.path import join, getsize
from time import strftime,localtime

config = ConfigParser.ConfigParser()
config.read("config.ini")
date = config.get("myvars", "date")
time = config.get("myvars", "time")

a=1
while a>0:
if datetime.now().weekday() == int(date):
str_time=strftime("%H:%M",localtime())
if str_time == time:
print "do it"
time.sleep(58)
 
M

MRAB

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 :

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.

This imports 3 items from the time module, but the name "time" itself
still refers to what was imported by the previous line.
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".
 
M

MRAB

i think this is what you are talking about
except that the time.sleep just does not work.
even changing "from time import strftime,localtime" to "from time
import strftime,localtime,sleep" does not do it.
#python code
import os,string,time,getpass,md5,ConfigParser

At this point, "time" refers to the time module which you have just
imported.
from datetime import *

At this point, "time" refers to the class "time" which you have just
imported from the datetime module.
from os.path import join, getsize
from time import strftime,localtime

config = ConfigParser.ConfigParser()
config.read("config.ini")
date = config.get("myvars", "date")
time = config.get("myvars", "time")

At this point, "time" refers to the value you got from the config.
a=1
while a>0:
if datetime.now().weekday() == int(date):
str_time=strftime("%H:%M",localtime())

At this point, "time" still refers to the value you got from the config.
if str_time == time:
print "do it"

At this point, "time" still refers to the value you got from the config.
It does not have an attribute called "sleep".
 
E

ecu_jon

so then why does this not work ?

from datetime import datetime
from os.path import join, getsize
import time,os,string,getpass,md5,ConfigParser
from time import strftime,localtime

config = ConfigParser.ConfigParser()
config.read("config.ini")
date = config.get("myvars", "date")
time = config.get("myvars", "time")
#print "date is ",date , " time is ",time
a=1
while a>0:
#import time
time.sleep(2)
if datetime.now().weekday() == int(date):
str_time=strftime("%H:%M",localtime())
print "do it"

Traceback (most recent call last):
File "I:\college\spring11\capstone-project\time-test.py", line 25,
in <module>
time.sleep(2)
AttributeError: 'str' object has no attribute 'sleep'
if i uncomment the import time line in the while loop it works.
boggle...
 
U

Ulrich Eckhardt

ecu_jon said:
import time,os,string,getpass,md5,ConfigParser
from time import strftime,localtime


You are importing the time module first, then import some symbols from the
time module. This seems redundant to me. Note that after the "import
time", the name "time" refers to the module you imported...
time = config.get("myvars", "time")

....but after this line it refers to something else, because you rebound it
to the object returned by "config.get(...)".
#print "date is ",date , " time is ",time
a=1
while a>0:

Note: PEP8 actually say you shall use spaces around operators here.
#import time

You're on the right track...
time.sleep(2)

....because this time here doesn't refer to the time module but to the
output of "config.get(...)"! This object is a string object (type "str"),
and Python complains:
AttributeError: 'str' object has no attribute 'sleep'


I Hope that clarifies something! ;)

Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top