pyzuki

alarm_clock62a.py

Sep 28th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.47 KB | None | 0 0
  1. # alarm_clock62a.py  need to incorporate Nick's refactorization https://gist.github.com/1246851
  2.  
  3. # in alarm_clock61a.py changed get_num_tadas_from_user () and get_message_from_user()
  4.  
  5. # completed through print_summary()
  6.  
  7. from time import ctime, clock, sleep
  8. import winsound
  9. import sys
  10.  
  11.  
  12. def get_num_tadas_from_user():
  13.     try:
  14.         n = input("\nHow many tadas?> ")
  15.         if not n:
  16.             return 1
  17.         else:
  18.             return int(n)
  19.     except ValueError:
  20.         return get_num_tadas_from_user()
  21.        
  22. def get_message_from_user():
  23.     """
  24.    The message is printed when the alarm rings.
  25.    The idea is that the message will tell the user the reason
  26.    for the alarm. Examples are, "Feed the cat", "Go to bed", "Write to Nick".
  27.    """
  28.     print("""
  29.    Enter a message about what the alarm ringing means,
  30.    in case you forget, or have several alarms set.
  31.    Enter nothing if no message.
  32.    """)
  33.     message = input("\nMessage?> ")
  34.     if message == '':
  35.         print("no message entered")
  36.         return None
  37.     else:
  38.         return message
  39.    
  40. def convert_from_military_time(time):
  41.     """Converts military time to hh:mm, raises ValueError if invalid."""
  42.     if len(time) == 4 and time.isdigit():
  43.         return time[:2] + ':' + time[2:]
  44.     else:
  45.         raise ValueError
  46.  
  47. def get_alarm_time_from_user():
  48.     print("""
  49.    Enter an alarm time in 24-hour time.
  50.    Both hours and minutes should be in 2 digits, separated by a colon.
  51.    13:00, 22:45, 02:30, 00:05
  52.    Military time will be converted to 24-hour time
  53.    1300 -> 13:00, 0005 -> 00:05
  54.    """)
  55.    
  56.     alarm_time = input("alarm time?> ")
  57.    
  58.     if ':' not in alarm_time:
  59.         alarm_time = convert_from_military_time(alarm_time)
  60.    
  61.     hour, minute = alarm_time.split(':')
  62.    
  63.     try:
  64.         if ':' not in alarm_time:
  65.             alarm_time = convert_from_military_time(alarm_time)
  66.            
  67.         if not (alarm_time.replace(':', '').isdigit()):
  68.             raise ValueError
  69.        
  70.         if len(hour) != 2 or len(minute) != 2:
  71.             raise ValueError
  72.        
  73.         if not 0 <= int(hour) < 24 or 0 <= int(minute) < 60:
  74.             raise ValueError
  75.    
  76.         print("\nIs", alarm_time, "the time you want? Hit enter for 'yes'; enter any character for 'no'")
  77.         if input("\nanswer?> "):
  78.             raise ValueError
  79.         return alarm_time
  80.    
  81.     except ValueError:
  82.         return get_alarm_time_from_user
  83.        
  84. def today_or_tomorrow(alarm_time):
  85.     now = ctime()[11:16]
  86.     if time2seconds(now) > time2seconds(alarm_time):
  87.         return "tomorrow"
  88.     else:
  89.         return "today"
  90.    
  91. def tada_or_tadas(num_tadas):
  92.     """
  93.    Make "tada" plural when appropriate.
  94.    
  95.    "tada" is one of the sounds that come with Windows: taDAH.
  96.    """
  97.     if num_tadas == 1:
  98.         return "taDAH"
  99.     else:
  100.         return "taDAHs"
  101.    
  102. def time2seconds(hour, minute):
  103.     """Given hour and minute as strings, return seconds as int."""
  104.     return int(hour)*3600 + int(minute)*60
  105.    
  106. def display_current_time():
  107.     """
  108.    Prints the current time as 24-hour time -- but no seconds.
  109.    13:07, 05:00, 23:59, 00:02, etc.
  110.    Each printing overwrites the previous one.
  111.    """
  112.     sys.stdout.flush()
  113.     print("The time now is", ctime()[11:16], "\r", end='')
  114.  
  115. def print_summary(alarm_time, today_tomorrow, num_tadas, message):
  116.     print("\nThe alarm is set to ring at", alarm_time, today_tomorrow)
  117.     if message:
  118.         print(" with ", num_tadas, " ", tada_or_tadas(num_tadas), " and message, '", message, "'", sep='')
  119.     else:
  120.         print("with", num_tadas, tada_or_tadas(num_tadas), "and no message.")
  121.    
  122. def ring_alarm_when_present_time_equals_alarm_time(alarm_time, num_tadas):
  123.     print()
  124.     while alarm_time != ctime()[11:16]:
  125.         sleep(0.1)
  126.         display_current_time()
  127.     for tada in range(0, num_tadas):
  128.         winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
  129.    
  130. def main():
  131.     print("\n" * 9)
  132.     num_tadas = get_num_tadas_from_user()
  133.     message = get_message_from_user()
  134.     alarm_time = get_alarm_time_from_user()
  135.     today_tomorrow = today_or_tomorrow(alarm_time)
  136.     print_summary(alarm_time, today_tomorrow, num_tadas, message)
  137.     ring_alarm_when_present_time_equals_alarm_time(alarm_time, num_tadas)
  138.     if message:
  139.         print("\nmessage:", message, "\n")
  140.     print()
  141.     print("Hit Enter to close Alarm Clock")
  142.     input()
  143.    
  144.    
  145. if __name__ == '__main__':
  146.     main()
  147.    
  148. """
  149. ERROR:
  150.  
  151. How many tadas?> 2
  152.  
  153.    Enter a message about what the alarm ringing means,
  154.    in case you forget, or have several alarms set.
  155.    Enter nothing if no message.
  156.    
  157.  
  158. Message?> Feed Toby
  159.  
  160.    Enter an alarm time in 24-hour time.
  161.    Both hours and minutes should be in 2 digits, separated by a colon.
  162.    13:00, 22:45, 02:30, 00:05
  163.    Military time will be converted to 24-hour time
  164.    1300 -> 13:00, 0005 -> 00:05
  165.    
  166. alarm time?> 1500
  167. Traceback (most recent call last):
  168.  File "C:\P32Working\alarm_clock62a.py", line 146, in <module>
  169.    main()
  170.  File "C:\P32Working\alarm_clock62a.py", line 135, in main
  171.    today_tomorrow = today_or_tomorrow(alarm_time)
  172.  File "C:\P32Working\alarm_clock62a.py", line 86, in today_or_tomorrow
  173.    if time2seconds(now) > time2seconds(alarm_time):
  174. TypeError: time2seconds() takes exactly 2 arguments (1 given)
  175. Process terminated with an exit code of 1
  176. """
  177.    
  178.        
  179.    
  180.        
  181.  
  182.  
  183.        
  184.    
  185.    
  186.  
Advertisement
Add Comment
Please, Sign In to add comment