Advertisement
Guest User

Untitled

a guest
Feb 11th, 2012
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # for http://www.reddit.com/r/dailyprogrammer/comments/pjsdx/difficult_challenge_2/
  5.  
  6. import time, math, sys
  7.  
  8. class Timer:
  9.     running = False
  10.     start = 0
  11.     hour = 60*60
  12.     minute = 60
  13.     log = None
  14.     def start_stop(self):
  15.         if self.running:
  16.             i = self.format_time(self.get_time_diff())
  17.             self.running = False
  18.             self.log.write('Finished: %s\n' % i)
  19.             self.log.close()
  20.             print('Clock stopped. Time is: %s' % i)
  21.             return
  22.         self.start = time.time()
  23.         self.running = True
  24.         self.log = open('timer.log', mode='w')
  25.         print('Clock started.')
  26.    
  27.     def lap(self):
  28.         if not self.running:
  29.             print('You need to start the clock before you can get lap times.')
  30.             return
  31.         i = self.format_time(self.get_time_diff())
  32.         self.log.write('Lap: %s\n' % i)
  33.         print('Lap time is: %s' % i)
  34.    
  35.     def quit(self):
  36.         if self.running:
  37.             self.start_stop()
  38.         sys.exit()
  39.    
  40.     def get_time_diff(self):
  41.         return time.time() - self.start
  42.    
  43.     def format_time(self, seconds):
  44.         h = '00'
  45.         m = '00'
  46.         s = '00'
  47.         ms = '000'
  48.         if (seconds >= self.hour):
  49.             hours = int(math.floor(seconds/self.hour))
  50.             h = '%02d' % hours
  51.             seconds = seconds % self.hour
  52.         if (seconds >= self.minute):
  53.             minutes = int(math.floor(seconds/self.minute))
  54.             m = '%02d' % minutes
  55.             seconds = seconds % self.minute
  56.         if (seconds > 0):
  57.             secs = int(math.floor(seconds))
  58.             s = '%02d' % secs
  59.             seconds = seconds % 1
  60.         if (seconds > 0):
  61.             ms = '%s' % seconds
  62.             ms = ms[2:5]
  63.         return '%s:%s:%s.%s' % (h, m, s, ms)
  64.  
  65. print('Welcome to timer!\nAvailable commands are (s)tart/(s)top, (l)ap time and (q)uit.')
  66. t = Timer();
  67. while True:
  68.     try:
  69.         command = input('Command: ').strip()
  70.     except EOFError:
  71.         print('')
  72.         continue
  73.     if not len(command):
  74.         continue
  75.     command = command[0]
  76.     if 'l' ==  command:
  77.         t.lap()
  78.     elif 's' == command:
  79.         t.start_stop()
  80.     elif 'q' == command:
  81.         t.quit()
  82.     else:
  83.         print('invalid command "%s", plese try again.' % command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement