pyzuki

timer.py

Sep 13th, 2011
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.28 KB | None | 0 0
  1. # timer.py
  2. ##
  3. ## 2008-05-29 added line 67, time.sleep(0.001), to prevent
  4. ## 100% use of CPU
  5.  
  6. import time, winsound
  7.  
  8. def exit():
  9.     print("Thank you for using Timer. Timer will now close")
  10.     time.sleep(2.1) # to provide the user with enough time to read the message
  11.  
  12. def hms(seconds):
  13.     """Convert seconds to tuple (hours, minutes, seconds)"""
  14.     hours, minutes = 0, 0
  15.          
  16.     if 60 <= seconds < 3600:
  17.         minutes, seconds = divmod(seconds,60)
  18.     else:    # i.e., seconds >= 3600
  19.         hours, seconds = divmod(seconds,3600)
  20.         minutes, seconds = divmod(seconds,60)
  21.        
  22.     return hours, minutes, seconds # returns a tuple, e.g. (1, 40, 8.5)
  23.  
  24. def HMS2seconds():
  25.     print("""
  26.    enter times as h:m:s                 3:34:2
  27.    If no seconds, h:m: is OK            11:34:
  28.    If no minutes, h::s is OK            5::10
  29.    If no hours, m:s is OK               4:23
  30.    If no minutes or seconds, h:: is OK  3::
  31.    If no hours or seconds, m: is OK     32:
  32.    If no hours or minutes, s is OK      17
  33.    """)
  34.     while True:
  35.         hr = 0
  36.         minute = 0
  37.         sec = 0
  38.         while True:
  39.             t = input("Enter hr:minute:sec ")
  40.             if t == "":
  41.                 continue
  42.             if t.count(":") == 0:
  43.                 try:
  44.                     sec = int(t)
  45.                 except ValueError:
  46.                     print("Incorrect entry; try again")
  47.                     continue
  48.                 return sec
  49.             elif t.count(":") == 1:
  50.                 try:
  51.                     m, s = t.split(':')
  52.                     if s == '':
  53.                         s = '0'
  54.                     minute, sec = int(m.strip()), int(s.strip())
  55.                 except ValueError:
  56.                     print("Incorrect entry; try again")
  57.                     continue
  58.                 return minute*60 + sec
  59.             elif t.count(":") == 2:
  60.                 try:
  61.                     h, m, s = t.split(':')
  62.                     if m == '':
  63.                         m = '0'
  64.                     if s == '':
  65.                         s = '0'
  66.                     hr, minute, sec = int(h.strip()), int(m.strip()), int(s.strip())
  67.                 except ValueError:
  68.                     print("Incorrect entry; try again")
  69.                     continue
  70.                 return hr*3600 + minute*60 + sec
  71.             else:
  72.                 print("Incorrect entry; try again")
  73.                 continue
  74. def secsToHMS(seconds):
  75.     """
  76.    Convert seconds to hours:minutes:seconds, with seconds rounded to hundredths of a second.
  77.    """
  78.     minutes, seconds = divmod(seconds, 60)
  79.     hours, minutes = divmod(minutes, 60)
  80.     return "%02d:%02d:%02d" % (hours, minutes, seconds)
  81.  
  82. #hours = input("Enter the number of hours to time ")
  83.  
  84. #if hours == "":
  85.     #h_seconds = 0
  86. #else:
  87.     #h_seconds = 3600 * int(hours)
  88.  
  89. #"""
  90. #Alan Gauld's suggestion for getting h_seconds:
  91. #h_seconds = (hours and 3600*int(hours)) or 0
  92.  
  93. #Works like this:
  94. #Only evaluate 3600*hours if hours NOT ""
  95. #If result is False(ie hours was "") then evaluate
  96. #second part of OR - ie 0
  97. #"""
  98. #minutes = input("And the number of minutes ")
  99. #m_seconds = (minutes and 60*int(minutes)) or 0 # see above
  100.  
  101. #seconds = input("And the number of seconds ")
  102.  
  103. #if seconds == "":
  104.     #seconds = 0
  105. seconds = HMS2seconds()
  106.  
  107. print("You have set the timer to",  hms(seconds))
  108.      
  109.  
  110. #r = input("Enter number of seconds between reports ")
  111. t0 = time.time()  # this gets time timing is started
  112. #if r == "":
  113.     #r = 10
  114. #else:
  115.     #r = int(r)
  116.  
  117. #print("Beep will sound after %d hours %d minutes %d seconds\n"  \
  118.       #% hms(seconds))
  119.    
  120. # get report of time-passed and time-left every r seconds
  121. k = r = 10
  122. while True:
  123.     time.sleep(0.001)
  124.     t1 = time.time() # when break triggered this will be the time timing stopped
  125.     seconds_passed = t1 - t0
  126.     seconds_left = seconds - int(seconds_passed)
  127.    
  128.     if seconds_passed >= seconds:
  129.         break
  130.    
  131.     if seconds_passed > k:
  132.         p = hms(seconds_passed)
  133.         l = hms(seconds_left)
  134.         if p[0] == 0 and p[1] == 0:
  135.             print("%d seconds have passed" % p[2])
  136.         elif p[0] > 0 and p[1] > 0:
  137.             print("%d hours %d minutes %d seconds have passed" \
  138.               % hms(seconds_passed))
  139.         elif p[0] == 0 and p[1] > 0:
  140.             print("%d minutes %d seconds have passed" % (p[1], p[2]))
  141.         else:
  142.             print("%d hours %d seconds have passed" % (p[0], p[2]))
  143.          
  144.         if l[0] == 0 and l[1] == 0:
  145.             print("%d seconds are left" % l[2])
  146.         elif l[0] > 0 and l[1] > 0:
  147.             print("%d hours %d minutes %d seconds are left" \
  148.               % hms(seconds_left))
  149.         elif l[0] == 0 and l[1] > 0:
  150.             print("%d minutes %d seconds are left" % (l[1], l[2]))
  151.         else:
  152.             print("%d hours %d seconds are left" % (l[0], l[2]))
  153.         time.sleep(0)
  154.         print("\n")
  155.  
  156.         k += r
  157.    
  158. print("seconds elapsed:", (t1 - t0)) # as a check on accuracy; no formatting
  159.  
  160. print("%d hours %d minutes %d seconds have passed" \
  161.               % hms(seconds))
  162.  
  163.        
  164. print("TIME'S UP!")
  165. #winsound.Beep(500,500) # Beep(frequency, duration in milliseconds) - only on Windows
  166. winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
  167. exit()
Advertisement
Add Comment
Please, Sign In to add comment