This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPillData.py
71 lines (61 loc) · 2.34 KB
/
PillData.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
71
import datetime, yaml
class YAMLSafeDefaultListDict(yaml.YAMLObject):
yaml_tag = "!defaultdict"
yaml_loader = yaml.SafeLoader
def __init__(self):
self._dict = {}
def __getitem__(self, key):
res = self._dict.get(key, None)
if res is None:
res = set()
self._dict[key] = res
return self._dict[key]
def __setitem__(self, key, value):
self._dict[key] = value
def __len__(self):
return len(self._dict)
class Time(yaml.YAMLObject):
yaml_tag = "!Time"
yaml_loader = yaml.SafeLoader
PMOffset = 12 # hours
def __init__(self, name, hour, minute, notifications = True):
# hour is 24 hour representation, 0-23
self.name = name
self.hour = hour
self.minute = minute
self.notifications = notifications
def get_hour_minute(self):
"""
:return: "hour:minute" representation
"""
return "{}:{:02d}".format(self.hour, self.minute)
def get_datetime_time(self):
"""
:return: datetime.time representation of this Time
"""
return datetime.time(hour = self.hour, minute = self.minute)
@staticmethod
def from_time_editor(time_editor):
# Python doesn't have function overloading, so we can't use __init__(self, time_editor).
# Well, we could use __init__(self, hour_or_time_editor, minute = 0), but the implementation would be awkward.
name = time_editor.name_entry.get_text()
hour = int(time_editor.select_hour.get_value())
# convert from 12 hour to 24 hour
if time_editor.select_ampm.get_active_id() == "pm":
hour += Time.PMOffset
minute = int(time_editor.select_minute.get_value())
notifications = time_editor.notification_checkbox.get_active()
return Time(name, hour, minute, notifications)
class Pill(yaml.YAMLObject):
yaml_tag = "!Pill"
yaml_loader = yaml.SafeLoader
def __init__(self, name, times):
self.name = name
self.times = times
self.dates_taken = YAMLSafeDefaultListDict() # (hour, minute): [date, date, date...]
@staticmethod
def from_pill_editor(pill_editor):
name = pill_editor.name_entry.get_text()
times = list(map(Time.from_time_editor, pill_editor.time_editors))
res = Pill(name, times)
return res