tags:

views:

123

answers:

1

Hi guys,

can somebody tell me how to use this class timers from python in my code more than one time.

import MOD

class timer:
    def __init__(self, seconds):
         self.start(seconds)
    def start(self, seconds):
         self.startTime = MOD.secCounter()
         self.expirationTime = self.startTime + seconds
         if seconds != 0:
            self.running = 1
            self.expired = 0
         else:  
            self.running = 0  
            self.expired = 0
    def stop(self):
         self.running = 0
         self.expired = 0
    def isexpired(self):
         if self.running == 1:  
            timeNow = MOD.secCounter()  
            if timeNow > self.expirationTime:    
               self.running = 0    
               self.expired = 1  
            else:    
               self.expired = 0
         return self.expired
    def isrunning(self):
         if self.running == 1:  
             timeNow = MOD.secCounter()  
             if timeNow > self.expirationTime:    
                self.running = 0    
                self.expired = 1  
             else:    
                self.expired = 0
         return self.running
    def change(self, seconds):
         self.expirationTime = self.startTime + seconds
    def count(self):
         if self.running == 1:  
            timeNow = MOD.secCounter()  
            return (timeNow - self.startTime)
         else:  
            return -1

they write this comment:

The following is a simple example about how to use this class:

    import timers
    timerA = timers.timer(0)
    timerA.start(15)
        while 1:  
            if timerA.isexpired():    
               print 'timerA expired'   
               break

but I don't know how to use it more than one time in my code, because I need to use more than one timer in my code,

should I write

    timerB = timers.timer(1)
    timerB.start(1800)
    while 1:  
        if timerB.isexpired():    
        print 'timerA expired'  
        break

any help, please

+1  A: 

Close - the argument to timers.timer is the number of seconds that the timer should time for at first. But every time you call timers.timer(), you'll get a new timer instance.

So your code could look more like:

  timerB = timers.timer(1800)
    while 1:  
        if timerB.isexpired():    
        print 'timerA expired'  
        break

Except that this is misleading - timerA and timerB are separate timers, so timerB.isexpired() won't tell you anything about timerA. Maybe you meant it to read "timerB expired"?

And I would also advise against polling timerB.isexpired() so rapidly. Maybe sleep for a second after each check?

danben
do you mean i shouldn't write timerB.start(1800) when i write timerB = timer.timer(1800)
mahdi86
Right, you don't need to because the timer constructor calls timer.start() with the argument that you pass it. If you want to reset the timer, then you can call start() explicitly. Also, if this answer works for you, it would be nice if you could accept it tomorrow since I am maxed out for today.
danben
thank you very much, it works fine with me
mahdi86