python time

Discussion in 'Python' started by ecu_jon, Mar 21, 2011.

  1. ecu_jon

    ecu_jon Guest

    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
     
    ecu_jon, Mar 21, 2011
    #1
    1. Advertisements

  2. ecu_jon

    Dave Angel Guest

    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
     
    Dave Angel, Mar 21, 2011
    #2
    1. Advertisements

  3. ecu_jon

    ecu_jon Guest

    On Mar 20, 10:09 pm, Dave Angel <> 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.
    also the sleep command does not like me :
    >>> from datetime import *
    >>> from time import strftime,localtime,sleep
    >>> 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'
    >>>


    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"
     
    ecu_jon, Mar 21, 2011
    #3
  4. ecu_jon

    ecu_jon Guest

    On Mar 20, 10:48 pm, ecu_jon <> wrote:
    > On Mar 20, 10:09 pm, Dave Angel <> 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.
    > also the sleep command does not like me :
    >  >>> from datetime import *
    >
    > >>> from time import strftime,localtime,sleep
    > >>> 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'
    >
    >
    >
    > 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)
     
    ecu_jon, Mar 21, 2011
    #4
  5. ecu_jon

    MRAB Guest

    On 21/03/2011 02:48, ecu_jon wrote:
    > On Mar 20, 10:09 pm, Dave Angel<> 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"
     
    MRAB, Mar 21, 2011
    #5
  6. ecu_jon

    MRAB Guest

    On 21/03/2011 03:29, ecu_jon wrote:
    > On Mar 20, 10:48 pm, ecu_jon<> wrote:
    >> On Mar 20, 10:09 pm, Dave Angel<> 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.
    >> also the sleep command does not like me :
    >> >>> from datetime import *

    >>
    >>>>> from time import strftime,localtime,sleep
    >>>>> 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'
    >>
    >>
    >>
    >> 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


    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".

    > time.sleep(58)
    >
     
    MRAB, Mar 21, 2011
    #6
  7. ecu_jon

    ecu_jon Guest

    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...
     
    ecu_jon, Mar 21, 2011
    #7
  8. ecu_jon wrote:
    > 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
     
    Ulrich Eckhardt, Mar 21, 2011
    #8
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. engsol
    Replies:
    2
    Views:
    1,080
    Dan Bishop
    Jan 26, 2004
  2. Replies:
    8
    Views:
    621
    Magnus Lycka
    Aug 5, 2005
  3. Peter Hansen
    Replies:
    0
    Views:
    791
    Peter Hansen
    Feb 22, 2006
  4. Peter Hansen
    Replies:
    0
    Views:
    673
    Peter Hansen
    Feb 22, 2006
  5. flamesrock
    Replies:
    8
    Views:
    681
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page