Skip to content

Instantly share code, notes, and snippets.

@tai2
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save tai2/66fb451e2dc952bd8296 to your computer and use it in GitHub Desktop.

Select an option

Save tai2/66fb451e2dc952bd8296 to your computer and use it in GitHub Desktop.
Count down time and notify a message through Notification Center
#!/usr/bin/env python
import sys
import time
import re
import subprocess
if __name__ == '__main__':
period = sys.argv[1]
m = re.match('(.*)([smh])', period)
if m:
t = float(m.group(1))
u = m.group(2)
if u == 'm':
sec = t * 60
elif u == 'h':
sec = t * 60 * 60
else:
sec = t
else:
sec = float(period)
t0 = time.time();
while True:
r = sec - (time.time() - t0)
h = r / (60 * 60)
m = (r % (60 * 60)) / 60
s = r % 60
if 0 < r:
print '\033[2K\033[0G%02d:%02d:%04.1f'%(h,m,s),
sys.stdout.flush()
time.sleep(1.0 / 10.0)
else:
print '\033[2K\033[0G00:00:00.0'
break
title = 'Countdown'
message = 'Finished.'
args = 'osascript', '-e', 'display notification "%s" with title "%s"'%(message, title)
subprocess.Popen(args)
@tai2
Copy link
Author

tai2 commented Jun 12, 2014

Usage:
countdown.py 3.5h # 3 hours and 30 minutes
countdown.py 3m # 3 minutes
countdown.py 1.5s # 1.5 seconds
countdown.py 1.5 # 1.5 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment