pyzuki

alarm_clock.py v1.0

Sep 26th, 2011
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1. # alarm_clock.py
  2.  
  3. from time import ctime, clock, sleep
  4. import winsound
  5. import sys
  6. import msvcrt
  7.  
  8. def get_num_tadas_from_user():
  9.     while True:
  10.         n = input("\nHow many tadas?> ")
  11.         if n == '':
  12.             return 1
  13.         else:
  14.             try:
  15.                 n = int(n)
  16.                 return n
  17.             except ValueError:
  18.                 continue
  19.        
  20. def get_message_from_user():
  21.     """
  22.    The message is printed when the alarm rings.
  23.    The idea is that the message will tell the user the reason
  24.    for the alarm. Examples are, "Feed the cat", "Go to bed", "Write to Nick".
  25.    """
  26.     print("""
  27.    Enter a message about what the alarm ringing means,
  28.    in case you forget, or have several alarms set.
  29.    """)
  30.     message = input("\nMessage?> ")
  31.     if message == '':
  32.         return "no message"
  33.     else:
  34.         return message
  35.  
  36. def get_alarm_time_from_user(num_tadas, message):
  37.     while True:
  38.         print("""
  39.        Enter alarm time in 24-hour time.
  40.        Both hours and minutes should be in 2 digits, separated by a colon.
  41.        13:00, 22:45, 02:30, 00:05
  42.        Military time will be converted to 24-hour time
  43.        1300 -> 13:00, 0005 -> 00:05
  44.        """)
  45.        
  46.         alarm_time = input("alarm time?> ")
  47.         if ':' not in alarm_time:
  48.             if len(alarm_time) == 4:
  49.                 alarm_time = alarm_time[:2] + ':' + alarm_time[2:]
  50.             else:
  51.                 continue
  52.         alarm_time_list = alarm_time.split(':')
  53.         hour = alarm_time_list[0]
  54.         if len(hour) != 2 or hour[0] not in '012' or hour[1] not in '0123456789':
  55.             continue
  56.         minute = alarm_time_list[1]
  57.         if len(minute) != 2 or minute[0] not in '012345' or minute[1] not in '0123456789':
  58.             continue
  59.         print("\nIs", alarm_time, "the time you want? Hit enter for 'yes'; enter any character for 'no'")
  60.         ans = input("\nanswer?> ")
  61.         if ans == '':
  62.             return alarm_time
  63.         else:
  64.             continue
  65.        
  66. def today_or_tomorrow(alarm_time):
  67.     now = ctime()[11:16]
  68.     if time2seconds(now) > time2seconds(alarm_time):
  69.         return " tomorrow"
  70.     else:
  71.         return " today"
  72.    
  73. def tada_or_tadas(num_tadas):
  74.     """
  75.    Make "tada" plural when appropriate.
  76.    
  77.    "tada" is one of the sounds that come with Windows: taDAH.
  78.    """
  79.     if num_tadas == 1:
  80.         return "tada"
  81.     else:
  82.         return "tadas"
  83.    
  84. def time2seconds(time):
  85.     """
  86.    Given 24-hour time as string, return seconds as int.
  87.    """
  88.     time = time.replace(':', '')
  89.     hour_string = time[:2]
  90.     minute_string = time[2:]
  91.     if hour_string[0] == '0':
  92.         hour = int(hour_string[1])
  93.     else:
  94.         hour = int(hour_string)
  95.        
  96.     if minute_string[0] == '0':
  97.         minute = int(minute_string[1])
  98.     else:
  99.         minute = int(minute_string)
  100.     return 3600*hour + 60*minute
  101.  
  102. def display_current_time():
  103.     """
  104.    Prints the current time as 24-hour time -- but no seconds.
  105.    13:07, 05:00, 23:59, 00:02, etc.
  106.    Each printing overwrites the previous one.
  107.    """
  108.     sys.stdout.flush()
  109.     print("The time now is", ctime()[11:16], "\r", end='')
  110.  
  111. def print_summary(alarm_time, today_tomorrow, num_tadas, message):
  112.     print("\nThe alarm is set to ring at ", alarm_time, today_tomorrow, ",", sep='')
  113.     print(" with ", num_tadas, " ", tada_or_tadas(num_tadas), " and message, '", message, "'", sep='')
  114.    
  115. def ring_alarm_when_present_time_equals_alarm_time(alarm_time, num_tadas, message):
  116.     print()
  117.     while alarm_time != ctime()[11:16]:
  118.         sleep(0.1)
  119.         display_current_time()
  120.     for tada in range(0, num_tadas):
  121.         winsound.PlaySound("/P31Working/Sounds/TaDa", winsound.SND_ALIAS)
  122.    
  123. def main():
  124.     print("\n" * 9)
  125.     num_tadas = get_num_tadas_from_user()
  126.     message = get_message_from_user()
  127.     alarm_time = get_alarm_time_from_user(num_tadas, message)
  128.     today_tomorrow = today_or_tomorrow(alarm_time)
  129.     print_summary(alarm_time, today_tomorrow, num_tadas, message)
  130.     ring_alarm_when_present_time_equals_alarm_time(alarm_time, num_tadas, message)
  131.     print("\nmessage:", message, "\n")
  132.     print()
  133.     print("Hit Enter to close Alarm Clock")
  134.     input()
  135.    
  136.    
  137. if __name__ == '__main__':
  138.     main()
  139.    
  140.    
  141.        
  142.    
  143.        
  144.  
  145.  
  146.        
  147.    
  148.    
  149.  
Advertisement
Add Comment
Please, Sign In to add comment