clock in Python (Python recipe)


SUBMITTED BY: yppah

DATE: Jan. 24, 2016, 5:31 a.m.

FORMAT: Text only

SIZE: 1.7 kB

HITS: 4795

  1. Forked from Recipe 579117 (testing)
  2. # alarm_clock.py
  3. # Description: A simple Python program to make the computer act
  4. # like an alarm clock. Start it running from the command line
  5. # with a command line argument specifying the duration in minutes
  6. # after which to sound the alarm. It will sleep for that long,
  7. # and then beep a few times. Use a duration of 0 to test the
  8. # alarm immediiately, e.g. for checking that the volume is okay.
  9. # Author: Vasudev Ram - http://www.dancingbison.com
  10. import sys
  11. import string
  12. from time import sleep
  13. sa = sys.argv
  14. lsa = len(sys.argv)
  15. if lsa != 2:
  16. print "Usage: [ python ] alarm_clock.py duration_in_minutes"
  17. print "Example: [ python ] alarm_clock.py 10"
  18. print "Use a value of 0 minutes for testing the alarm immediately."
  19. print "Beeps a few times after the duration is over."
  20. print "Press Ctrl-C to terminate the alarm clock early."
  21. sys.exit(1)
  22. try:
  23. minutes = int(sa[1])
  24. except ValueError:
  25. print "Invalid numeric value (%s) for minutes" % sa[1]
  26. print "Should be an integer >= 0"
  27. sys.exit(1)
  28. if minutes < 0:
  29. print "Invalid value for minutes, should be >= 0"
  30. sys.exit(1)
  31. seconds = minutes * 60
  32. if minutes == 1:
  33. unit_word = " minute"
  34. else:
  35. unit_word = " minutes"
  36. try:
  37. if minutes > 0:
  38. print "Sleeping for " + str(minutes) + unit_word
  39. sleep(seconds)
  40. print "Wake up"
  41. for i in range(5):
  42. print chr(7),
  43. sleep(1)
  44. except KeyboardInterrupt:
  45. print "Interrupted by user"
  46. sys.exit(1)
  47. # EOF
  48. ----------------------------------------------------------------------------------------
  49. There you go :)

comments powered by Disqus