Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # timer.py
- ##
- ## 2008-05-29 added line 67, time.sleep(0.001), to prevent
- ## 100% use of CPU
- import time, winsound
- def exit():
- print("Thank you for using Timer. Timer will now close")
- time.sleep(2.1) # to provide the user with enough time to read the message
- def hms(seconds):
- """Convert seconds to tuple (hours, minutes, seconds)"""
- hours, minutes = 0, 0
- if 60 <= seconds < 3600:
- minutes, seconds = divmod(seconds,60)
- else: # i.e., seconds >= 3600
- hours, seconds = divmod(seconds,3600)
- minutes, seconds = divmod(seconds,60)
- return hours, minutes, seconds # returns a tuple, e.g. (1, 40, 8.5)
- def HMS2seconds():
- print("""
- enter times as h:m:s 3:34:2
- If no seconds, h:m: is OK 11:34:
- If no minutes, h::s is OK 5::10
- If no hours, m:s is OK 4:23
- If no minutes or seconds, h:: is OK 3::
- If no hours or seconds, m: is OK 32:
- If no hours or minutes, s is OK 17
- """)
- while True:
- hr = 0
- minute = 0
- sec = 0
- while True:
- t = input("Enter hr:minute:sec ")
- if t == "":
- continue
- if t.count(":") == 0:
- try:
- sec = int(t)
- except ValueError:
- print("Incorrect entry; try again")
- continue
- return sec
- elif t.count(":") == 1:
- try:
- m, s = t.split(':')
- if s == '':
- s = '0'
- minute, sec = int(m.strip()), int(s.strip())
- except ValueError:
- print("Incorrect entry; try again")
- continue
- return minute*60 + sec
- elif t.count(":") == 2:
- try:
- h, m, s = t.split(':')
- if m == '':
- m = '0'
- if s == '':
- s = '0'
- hr, minute, sec = int(h.strip()), int(m.strip()), int(s.strip())
- except ValueError:
- print("Incorrect entry; try again")
- continue
- return hr*3600 + minute*60 + sec
- else:
- print("Incorrect entry; try again")
- continue
- def secsToHMS(seconds):
- """
- Convert seconds to hours:minutes:seconds, with seconds rounded to hundredths of a second.
- """
- minutes, seconds = divmod(seconds, 60)
- hours, minutes = divmod(minutes, 60)
- return "%02d:%02d:%02d" % (hours, minutes, seconds)
- #hours = input("Enter the number of hours to time ")
- #if hours == "":
- #h_seconds = 0
- #else:
- #h_seconds = 3600 * int(hours)
- #"""
- #Alan Gauld's suggestion for getting h_seconds:
- #h_seconds = (hours and 3600*int(hours)) or 0
- #Works like this:
- #Only evaluate 3600*hours if hours NOT ""
- #If result is False(ie hours was "") then evaluate
- #second part of OR - ie 0
- #"""
- #minutes = input("And the number of minutes ")
- #m_seconds = (minutes and 60*int(minutes)) or 0 # see above
- #seconds = input("And the number of seconds ")
- #if seconds == "":
- #seconds = 0
- seconds = HMS2seconds()
- print("You have set the timer to", hms(seconds))
- #r = input("Enter number of seconds between reports ")
- t0 = time.time() # this gets time timing is started
- #if r == "":
- #r = 10
- #else:
- #r = int(r)
- #print("Beep will sound after %d hours %d minutes %d seconds\n" \
- #% hms(seconds))
- # get report of time-passed and time-left every r seconds
- k = r = 10
- while True:
- time.sleep(0.001)
- t1 = time.time() # when break triggered this will be the time timing stopped
- seconds_passed = t1 - t0
- seconds_left = seconds - int(seconds_passed)
- if seconds_passed >= seconds:
- break
- if seconds_passed > k:
- p = hms(seconds_passed)
- l = hms(seconds_left)
- if p[0] == 0 and p[1] == 0:
- print("%d seconds have passed" % p[2])
- elif p[0] > 0 and p[1] > 0:
- print("%d hours %d minutes %d seconds have passed" \
- % hms(seconds_passed))
- elif p[0] == 0 and p[1] > 0:
- print("%d minutes %d seconds have passed" % (p[1], p[2]))
- else:
- print("%d hours %d seconds have passed" % (p[0], p[2]))
- if l[0] == 0 and l[1] == 0:
- print("%d seconds are left" % l[2])
- elif l[0] > 0 and l[1] > 0:
- print("%d hours %d minutes %d seconds are left" \
- % hms(seconds_left))
- elif l[0] == 0 and l[1] > 0:
- print("%d minutes %d seconds are left" % (l[1], l[2]))
- else:
- print("%d hours %d seconds are left" % (l[0], l[2]))
- time.sleep(0)
- print("\n")
- k += r
- print("seconds elapsed:", (t1 - t0)) # as a check on accuracy; no formatting
- print("%d hours %d minutes %d seconds have passed" \
- % hms(seconds))
- print("TIME'S UP!")
- #winsound.Beep(500,500) # Beep(frequency, duration in milliseconds) - only on Windows
- winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
- exit()
Advertisement
Add Comment
Please, Sign In to add comment