[Tutor] Python Timer

Christian Wyglendowski Christian.Wyglendowski at greenville.edu
Tue Nov 16 18:05:43 CET 2004


> -----Original Message-----
> [mailto:tutor-bounces at python.org] On Behalf Of Joe Chiocchi
> Subject: [Tutor] Python Timer
> 
> Hello, I was wondering if it were possible to make a timer in 
> the console. It would be a simple timer just to count down seconds.
> The only timers I found were made with Tkinter and I am in 
> need of one that prints to the console. Thanks 

Hi Joe,

With all the great talk about threads going on, I thought it might be
fun to make a simple threaded timer.  Here is what I typed into the
Pythonwin shell:

>>> import time
>>> import threading
>>> class Timer(threading.Thread):			#subclass Thread
... 	def __init__(self, seconds):			#make it
possible to pass the time in seconds that we want the timer to run
... 		self.runTime = seconds			#set our runTime
in seconds
... 		threading.Thread.__init__(self)	#call the Thread's
constructor
... 	def run(self):					#define what we
want our Timer thread to do
... 		time.sleep(self.runTime)		#have it sleep
for runTime seconds
... 		print "Buzzzz!! Time's up!"		#print a message
when done
... 		
>>> t = Timer(10)
>>> t.start()						#ten seconds go
by ...
>>> Buzzzz!! Time's up!

There is a very simple (and pretty useless) timer.  I reread your
question though and noticed that you want the timer to "print to the
console".  I don't know if this is the best way, but we can use the
simple Timer class above to build a CountDownTimer.

>>> class CountDownTimer(Timer):			#inherit from
our simple Timer which initializes all that we need
... 	def run(self):					#setup our new
action, overriding the simple action in Timer
... 		counter = self.runTime			#inititialize a
counter variable
... 		for sec in range(self.runTime):	#start a loop over a
range that is self.runTime elements long
... 			print counter			#output value of
counter
... 			time.sleep(1.0)			# ZZZZzzzz...
(sleep for one second)
... 			counter -= 1			#decrement our
counter variable by 1
... 		print "Done."				#print this once
the loop above is complete
... 		
>>> c = CountDownTimer(10)
>>> c.start()
10
>>> 9
8
7
6
5
4
3
2
1
Done.

So there we have a timer that counts down the seconds and prints them to
the screen.  More interesting than the first timer, but not by much.
Here is a final implementation that allows you to pass an action to
execute at the end of the timer run.

>>> class CountDownExec(CountDownTimer):			#now we
subclass CountDownTimer
... 	def __init__(self, seconds, action):		#we need to
accept seconds as well as an action as arguments
... 		self.action = action				#set our
timer's action
... 		CountDownTimer.__init__(self, seconds)	#setup the rest
of the timer from CountDownTimer
... 	def run(self):						#we need
to modify run to perform the action
... 		CountDownTimer.run(self)			#first
we run the standard CountDownTimer routine, as it still works
... 		self.action()					#then we
do our action
... 		
>>> def myAction():						#we need
an action, right?
... 	print "Performing my action..."			#boring, but
simple
... 	
>>> c = CountDownExec(10, myAction)
>>> c.start()
10
>>> 9
8
7
6
5
4
3
2
1
Done.
Performing my action...

CountDownExec could be expanded to allow for arguments to be passed to
the action, but unfortunately I am out of time on this email!

HTH,

Christian
http://www.dowski.com



More information about the Tutor mailing list