laptimer.py
# -*- encoding: utf-8 -*-
__version__ = '0.0.1'
from time import time, sleep
#---------------------------------------------------------------
# LapTimer CLASS
#---------------------------------------------------------------
class LapTimer(object):
"""
For example:
from laptimer import LapTimer
timer = LapTimer("TEST SCRIPT")
timer.lap("start script")
for i in xrange(3):
sleep(i+1)
timer.lap('sleep time %d sec' % (i+1))
timer.get_lap()
Returns:
-------------------------
TEST SCRIPT
Lap : Total : message
-------------------------
0.000 : 0.000 : start script
1.002 : 1.002 : sleep time 1 sec
2.003 : 3.005 : sleep time 2 sec
3.004 : 6.009 : sleep time 3 sec
"""
def __init__(self, name = None):
self.start_time = self.now_time = self.last_time = time()
self.name = name
self.time_list = []
def lap(self, message=None):
self.now_time = time()
self.lap_time = self.now_time - self.last_time
self.total_time = self.now_time - self.start_time
time_list = (self.lap_time, self.total_time, message)
self.time_list.append(time_list)
self.last_time = self.now_time
return ' %5.3f - %5.3f - %s' % time_list
def get_lap(self):
print '-' * 25
print '%s' % self.name
print ' %5s : %5s : message' % ('Lap', 'Total')
print '-' * 25
for i in self.time_list:
print ' %5.3f : %5.3f : %s' % i
#---------------------------------------------------------------
# TEST
#---------------------------------------------------------------
def test():
'''
TEST
'''
timer = LapTimer("TEST SCRIPT")
timer.lap("start script")
for i in xrange(3):
sleep(i+1)
timer.lap('sleep time %d sec' % (i+1))
timer.get_lap()
if __name__ == '__main__': test()
Recommended Posts