-
-
Save lxneng/314286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Depends on the OS X "say" command | |
import time, datetime, subprocess, math, sys | |
def say(s): | |
subprocess.call(['say', str(s)]) | |
def seconds_until(dt): | |
return time.mktime(dt.timetuple()) - time.time() | |
def countdown_to(target_time, only_if_below=10, end_statement=None): | |
said = set() | |
while True: | |
i = int(math.ceil(seconds_until(target_time))) | |
if i < 0: | |
if end_statement: | |
say(end_statement) | |
break | |
if i <= only_if_below and i not in said: | |
said.add(i) | |
say(i) | |
sys.stdout.write('%s. ' % i) | |
sys.stdout.flush() | |
time.sleep(0.1) | |
if __name__ == '__main__': | |
countdown_to( | |
datetime.datetime(2010, 1, 1, 0, 0, 0), | |
10, | |
'Happy new year!', | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment