zhasm
12/28/2010 - 5:25 AM

a not very accurate timer in python

a not very accurate timer in python

from time import time

class Timer():
    def __init__(self):
        self.begin=0
        self.secs=0 

    def start(self):
        self.begin=int(time())

    def stop(self):
        self.secs=int(time())-self.begin

    def __repr__(self):
        secs=self.secs
        result=""
        if secs>3600:
            result+="%d hours " % int(secs/3600)
            secs=secs % 3600
        if secs>60:
            result+="%d mins " % int (secs/60)
            secs=secs % 60           
        if result:
            result+=" and %d secs, or %d secs" % ( secs, secs)
        else:
            result="%d seconds" % secs
        return result