Friday, December 30, 2011

Basic timer in Python

#!/usr/bin/env python


import time
import threading

class Timer(threading.Thread):
  def __init__(self, seconds):
   self.runTime = seconds
   threading.Thread.__init__(self)
  def run(self):
   time.sleep(self.runTime)
   print "Buzzzz!! Time's up!"

class CountDownTimer(Timer):
  def run(self):
   counter = self.runTime
   for sec in range(self.runTime):
    print counter
    time.sleep(1.0)
    counter -= 1
   print "Done."


class CountDownExec(CountDownTimer):   
  def __init__(self, seconds, action):  
   self.action = action    
   CountDownTimer.__init__(self, seconds) 
  def run(self):      
   CountDownTimer.run(self)   
   self.action()     
   
def myAction():      
  print "Performing my action..."   



c = CountDownExec(10, myAction)
c.start()
#>./timer.py 
10
9
8
7
6
5
4
3
2
1
Done.
Performing my action...

No comments: