[Tutor] Python Timer

Dick Moores rdm at rcblue.com
Thu Nov 18 03:01:21 CET 2004

Joe, if you rely on time.sleep() in counting seconds, check out the 
inaccuracy that builds up:

from time import sleep, time

def ansi(str):
         return chr(27)+'['+str
x = 0
startTime = time()
while 1:
         print x,
         currentTime = time()
         print ansi('0E')+ansi('K'),
         print "%.3f" % (currentTime - startTime),


Joe Chiocchi wrote at 12:07 11/17/2004:
>Ok, so right now I am in the progress of writing it. I'm using ansi 
>characters so that I can clear the line and stuff. Here is an example of 
>just a counter..(counts up every second until u stop it)
>You have to make sure you run it python unbuffered mode.
>#!/usr/bin/python -u
>from time import sleep
>def ansi(str):
>         return chr(27)+'['+str
>x = 0
>while 1:
>         x=x+1
>         print x,
>         sleep(1)
>         print ansi('0E')+ansi('K'),
>Ok so the plan is to integrate this into my timer. I just need to figure 
>out how to compare it the time right now, and the time at which I want 
>it to end.
>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list