(Updated the post after a while, so it may show up twice in some feed readers (but with more info), sorry ...)
I had a need to set alarms for myself while working at the computer, to remind me to stop the current task and do some other task. Was using my smartphone's alarm clock app, but found that the volume was a bit too high, even at the lowest volume setting. So I thought of writing a simple alarm clock in Python as a command-line utility. Here it the code for it:
# alarm_clock.py # Description: A simple Python program to make the computer act # like an alarm clock. Start it running from the command line # with a command line argument specifying the duration in minutes # after which to sound the alarm. It will sleep for that long, # and then beep a few times. Use a duration of 0 to test the # alarm immediiately, e.g. for checking that the volume is okay. # Author: Vasudev Ram - http://www.dancingbison.com import sys import string from time import sleep sa = sys.argv lsa = len(sys.argv) if lsa != 2: print "Usage: [ python ] alarm_clock.py duration_in_minutes" print "Example: [ python ] alarm_clock.py 10" print "Use a value of 0 minutes for testing the alarm immediately." print "Beeps a few times after the duration is over." print "Press Ctrl-C to terminate the alarm clock early." sys.exit(1) try: minutes = int(sa) except ValueError: print "Invalid numeric value (%s) for minutes" % sa print "Should be an integer >= 0" sys.exit(1) if minutes < 0: print "Invalid value for minutes, should be >= 0" sys.exit(1) seconds = minutes * 60 if minutes == 1: unit_word = " minute" else: unit_word = " minutes" try: if minutes > 0: print "Sleeping for " + str(minutes) + unit_word sleep(seconds) print "Wake up" for i in range(5): print chr(7), sleep(1) except KeyboardInterrupt: print "Interrupted by user" sys.exit(1) # EOF
Run it without any command line arguments to see how to use it:
C:> python alarm_clock.py
Use the first version of the command above, if your Python interpreter is not registered with the OS as the default application to run .py files, and the second version if it is registered. Usually the latter is the case, so you can omit the word python at the beginning of the command. The square brackets shown in the usage message (see the code) are not to be typed literally, they are there to indicate that the word within them is optional (a standard convention used on UNIX).
You can pass an argument of 0 for the duration, to test the alarm clock immediately, e.g. to see if the volume suits you or not. If not, increase or decrease the volume of your computer speakers.
I find this simple alarm clock program more convenient to use (when at my computer, of course) than either my smartphone's alarm clock app, or my traditional (physical) alarm clock like the one in the image above :-) (*)
I just have to keep one command window open, run that program in it, e.g.:
to remind me to change tasks after 30 minutes. And when I want to set and run the alarm again, I just hit up arrow, change the time at the end of the command if needed, and hit Enter.
In fact, on my machine, I also created a batch file called alarm.bat, which accepts an argument and passes it to alarm_clock.py, so all I really have to type at the command line is:
(*) (Okay, I don't really have one of those old-fashioned alarm clocks; I have a modern plastic one. But I'd like to pick up one of those old-fashioned pocket watches like the one below, at some antique or second-hand shop some day. My grandfather had one of those ...
The tool uses the print chr(7) method to make the beeps. I did this to make it portable between Windows and Linux. It is possible to use the winsound module (only on Windows) for making the sound instead. See my earlier post about Python's winsound module: Try the Python WinSound API. It would also be possible to use other sound libraries instead, of course, on either Linux or Windows. For example, check out my earlier post about PyAudio (which also has a link to an earlier post about pyglet, another sound library option).
I'd also earlier written a digital clock (not an alarm clock, just a clock display) in 3 lines of Delphi code :-)
Of course, there are many variations and improvements possible on an alarm clock utility, such as having a GUI, making it look like a real alarm clock, and others. I may work on some of those at some point later. Meanwhile, this simple one works well enough for my current needs.
- Vasudev Ram - Dancing Bison Enterprises
Read other posts about Python on my blog.
Read other posts about utilities on my blog.