pyzuki

alarm_clock.py v1.1

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