Thursday, November 7, 2013

A simple alarm clock in Python (command-line)

By Vasudev Ram



(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[1])
except ValueError:
    print "Invalid numeric value (%s) for minutes" % sa[1]
    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

or

C:> 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.:

alarm_clock.py 30

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:

alarm 30

(*) (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 ...

old-fashioned pocket watch

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.

Contact me





O'Reilly 50% Ebook Deal of the Day

4 comments:

Anonymous said...

FYI, Unfortunately, print(chr(7)) does not create a sound on some computers. In my case, it works fine on a Dell deskside, but does nothing on my Thinkpad.

DanZ

Vasudev Ram said...

> Unfortunately, print(chr(7)) does not create a sound on some computers.

That could be for any one of a number of reasons. One reason that I have come across:

- if you are running the program from an IDE such as PyCharm, IDLE or other, it may not beep.

chr(7) acting as the ASCII BEL character (which causes the console to beep) is only guaranteed to work on real consoles, i.e. old-style dumb terminals like VT100 (that were used with Unix, VMS, etc.), or close software emulations of a dumb terminal, like a Linux terminal program (xterm/konsole/...) or a Windows DOS command prompt. Don't know about Max OS X since I don't use it, but probably works with Terminal.) The same may hold true for some other control characters (characters with ASCII codes between 0 and 31 inclusive), of which BEL is one, as are form feed (FF), VT (vertical tab), etc. ... i.e. they will not have the same effect in non-terminal environments that they do in terminal environments. They might just show up as some graphic character, or have no effect, depending on how the IDE/etc. handles them.

Google ASCII table or see ascii-table.com.



Vasudev Ram said...

>Max OS X

Typo - I meant Mac OS X :)

Vasudev Ram said...


Pocket watch image attribution:

https://en.wikipedia.org/wiki/File:MontreGousset001.jpg