Welcome, guest | Sign In | My Account | Store | Cart

A simple script that works as a pomodoro timer. Useful to enhance ones focus while working on something. The Pomodoro technique is described at http://www.pomodorotechnique.com/

This script is an offline version of http://www.focusboosterapp.com/live.cfm

It requires the Mpg123 program to play the audio and I obtained the ticking sounds from http://www.soundjay.com/clock-sounds-1.html

Python, 73 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
"""
Emulates a pomodoro timer.

The program is invoked with a "work duration" and a "rest duration".
It plays a ticking sound for the work duration (specified in minutes) and then an alarm.
After that it plays another tick for the rest duration (also specified in minutes).

If the PyOSD library is available, it will use it to display messages
on the screen. Otherwise, it simply prints them out to stdout.

This is usually invoked as
./ticker.py 30 5
This means that you work for 30 minutes and then rest for 5.

For details on the Pomodoro technique, please refer http://www.pomodorotechnique.com/
For the sound files, please check out http://www.soundjay.com/clock-sounds-1.html

This script is an attempt to create an offline version of http://www.focusboosterapp.com/live.cfm

"""

import sys
import time
import subprocess

pyosd = False
try:
    import pyosd
    osd = pyosd.osd()
    osd.set_align(pyosd.ALIGN_CENTER)
    osd.set_pos(pyosd.POS_MID)
    display = osd.display
except:
    display = lambda x: sys.stdout.write(str(x)+"\n")
    
WORK_TICK = "/home/noufal/scratch/clock-ticking-4.mp3"
REST_TICK = "/home/noufal/scratch/clock-ticking-5.mp3"
ALARM     = "/home/noufal/scratch/alarm-clock-1.mp3"
DEV_NULL  = open("/dev/null","w")

def tick(duration, tick):
    "Plays a the ticking sound specified by tick for duration time"
    cmd = ["mpg123", "--loop", "-1" , tick]
    p = subprocess.Popen(cmd, stdout = DEV_NULL, stderr = subprocess.PIPE)
    try:
        time.sleep(duration)
    except KeyboardInterrupt:
        display("Interrupting")
    p.kill()
    
def alarm(alarm):
    "Plays the alarm sound specified by alarm"
    cmd = ["mpg123", alarm]
    p = subprocess.Popen(cmd, stdout = DEV_NULL, stderr = subprocess.PIPE)
    p.wait()
    
def main(args):
    if len(args[1:]) != 2:
        print "Usage : %s work_time rest_time"%args[0]
        return -1
    twork, trest = args[1:]
    display("Work now")
    tick(int(twork)*60, WORK_TICK)
    alarm(ALARM)
    display("Rest now")
    tick(int(trest)*60, REST_TICK)
    alarm(ALARM)
    display("Cycle complete")
    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv))

I've found this an effective way to focus.

1 comment

RyuDahyun 6 years, 10 months ago  # | flag

Hello, I am a begineer in Programming and Python language,and I was looking up to make timers in Python. What's the '/dev/null' supposed to direct? What's its role?

Created by Noufal Ibrahim on Sat, 14 Aug 2010 (MIT)
Python recipes (4591)
Noufal Ibrahim's recipes (10)

Required Modules

  • (none specified)

Other Information and Tasks