-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.py
70 lines (55 loc) · 2.04 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
66
67
68
69
70
from datetime import datetime
import time
from task import Task
from task_queue import TaskQueue
from dispatcher import Dispatcher
# note: we do not really need a Timer instance when queue is empty
class Timer:
def __init__(self, task_queue):
self.dispatcher = Dispatcher()
self.task_q = task_queue
def get_curr_datetime():
return { # convert str to int and then again to str
'time': ':'.join(list(map(
lambda x: str(x),
list(map(
lambda x: int(x),
str(datetime.now().time()).split(':')[:2]
))
))),
'date': '-'.join(list(map(
lambda x: str(x),
list(map(
lambda x: int(x),
str(datetime.now().date()).split('-')
))
)))
}
# note:
# the final int to str conversion might not be needed
# if the Task objects remind_time dictionary has int values
def start(self):
print("Timer started.")
while True:
# the timer stops as soon as the above while loop is exited
# and can be restarted again using start()
# if self.task_q.isEmpty():
# break
if not self.task_q.isEmpty():
# get a dictionary containing current datetime
curr = Timer.get_curr_datetime()
remind_time = self.task_q.peek().remind_time
if (curr['date'] == remind_time['date'] and
curr['time'] == remind_time['time']):
self.dispatcher.dispatch_reminder(self.task_q.dequeue())
# the number of seconds to sleep (could be user specifiable)
time.sleep(10)
print("Timer stopped.")
if __name__ == "__main__":
timer = Timer()
q = TaskQueue()
q.insert(Task("20:28", "T 1"))
# q.insert(Task("16:43", "T 2"))
# q.insert(Task("16:50", "T 3"))
# q.insert(Task("16:40", "T 4"))
timer.start(q)