-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
executable file
·65 lines (50 loc) · 1.95 KB
/
timer.py
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
#!/usr/bin/python
import time
import argparse
import os
import sys
import signal
# The default amount of time for pizza cooking (13mins)
DEFAULT_PIZZA_TIME = 780
def sigint_handler(signal, frame):
sys.exit(0)
def my_usage(progname):
return progname + ' [-h] [([-H H] [-m m] [-s s]) | --pizza]'
def main():
parser = argparse.ArgumentParser(
description='This is a timer, which makes beeping sound after a' +
' configured time', usage=my_usage(os.path.basename(__file__)))
group_time = parser.add_argument_group('Time options')
group_time.add_argument('-H', type=int, nargs=1,
help='number of hours', default=[0])
group_time.add_argument('-m', type=int, nargs=1,
help='number of mintes', default=[0])
group_time.add_argument('-s', type=int, nargs=1,
help='number of seconds', default=[0])
parser.add_argument('--pizza', action='store_true',
help='I want to make a pizza!', default=False)
args = parser.parse_args()
more_than_one_sec = False
if args.pizza:
# The pizza parameter is set, so we ignore every given value, and we
# wait default_pizza amount of time
time.sleep(DEFAULT_PIZZA_TIME)
more_than_one_sec = True
else:
# We are not cooking a pizza, so we wait the defiend amount of time
hours_in_seconds = args.H[0] * 60 * 60
minutes_in_seconds = args.m[0] * 60
sum_of_secs = hours_in_seconds + minutes_in_seconds + args.s[0]
time.sleep(sum_of_secs)
if sum_of_secs > 0:
more_than_one_sec = True
if more_than_one_sec:
for i in range(50):
print('\r\a', end='')
sys.stdout.flush()
time.sleep(0.2)
else:
print('No timer was set, thus at least one sec must be defined')
if __name__ == '__main__':
signal.signal(signal.SIGINT, sigint_handler)
main()