1############################################################################### 2# 3# timeKeeper.py - lass for creating time stamps 4# 5############################################################################### 6# # 7# This program is free software: you can redistribute it and/or modify # 8# it under the terms of the GNU General Public License as published by # 9# the Free Software Foundation, either version 3 of the License, or # 10# (at your option) any later version. # 11# # 12# This program is distributed in the hope that it will be useful, # 13# but WITHOUT ANY WARRANTY; without even the implied warranty of # 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 15# GNU General Public License for more details. # 16# # 17# You should have received a copy of the GNU General Public License # 18# along with this program. If not, see <http://www.gnu.org/licenses/>. # 19# # 20############################################################################### 21 22import time 23import logging 24 25 26class TimeKeeper: 27 def __init__(self): 28 self.logger = logging.getLogger('timestamp') 29 self.startTime = time.time() 30 self.lastLogTime = self.startTime 31 32 def startTimer(self): 33 """Restart the timer""" 34 self.startTime = time.time() 35 self.lastLogTime = self.startTime 36 37 def getTimeStamp(self): 38 """Make a time stamp""" 39 now = time.time() 40 ret_str = "{ Current stage: %s || Total: %s }" % (self.secondsToStr(now - self.lastLogTime), self.secondsToStr(now - self.startTime)) 41 self.lastLogTime = now 42 return ret_str 43 44 def printTimeStamp(self): 45 self.logger.info(self.getTimeStamp()) 46 47 def secondsToStr(self, t): 48 rediv = lambda ll, b: list(divmod(ll[0], b)) + ll[1:] 49 return "%d:%02d:%02d.%03d" % tuple(reduce(rediv, [[t * 1000, ], 1000, 60, 60])) 50