Created
September 22, 2016 23:05
-
-
Save iamvee/5f02cb61970d2b2cd3d28f4124ad7809 to your computer and use it in GitHub Desktop.
a simple timer :D
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
usage: | |
$ python timer.py -t 00:01:20 | |
""" | |
import sys | |
import time | |
import math | |
import datetime | |
def timer(period=(0, 1, 0), err=0.01): | |
start = time.time() | |
duration = sum(map(lambda (i, v): v*60**(2-i), enumerate(period))) | |
end = start + duration | |
if start > end: | |
raise Exception, "waaat ??" | |
sleep_time = err/2. | |
last = start | |
while last < end: | |
curtime = time.time() | |
acc = curtime - last | |
if acc > err: | |
sys.stdout.write("\r%s" % str(datetime.timedelta(0, end-curtime))) | |
sys.stdout.flush() | |
last = curtime | |
else: | |
time.sleep(err-acc) | |
if __name__ == '__main__': | |
flags = sys.argv[1:] | |
t = flags[flags.index('-t') + 1] | |
t = tuple([int(i) for i in t.split(':')]) | |
try: | |
timer(period=t) | |
except KeyboardInterrupt: | |
sys.stdout.flush() | |
sys.stdout.write("\r[X] terminated\r\n") | |
sys.stdout.flush() | |
sys.stdout.write("\r[+] FINISHED\r\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment