This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.py
167 lines (153 loc) · 6.24 KB
/
general.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright (C) 2022-2023 EZCampus
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
General computation code.
"""
from . import constants
def encode_days_of_week(data: dict[str, bool]) -> int:
"""Encodes standard dictionary of days to integer representation.
Examples:
>>> encode_days_of_week({"monday": False, "tuesday": False, "wednesday": False, "thursday": False, "friday": False, "saturday": False, "sunday": False})
0
>>> encode_days_of_week({"monday": True, "tuesday": False, "wednesday": False, "thursday": False, "friday": False, "saturday": False, "sunday": False})
1
>>> encode_days_of_week({"monday": False, "tuesday": True, "wednesday": False, "thursday": False, "friday": False, "saturday": False, "sunday": False})
2
>>> encode_days_of_week({"monday": False, "tuesday": False, "wednesday": True, "thursday": False, "friday": False, "saturday": False, "sunday": False})
4
>>> encode_days_of_week({"monday": False, "tuesday": False, "wednesday": False, "thursday": False, "friday": False, "saturday": False, "sunday": True})
64
>>> encode_days_of_week({"monday": True, "tuesday": True, "wednesday": False, "thursday": False, "friday": False, "saturday": False, "sunday": False})
3
>>> encode_days_of_week({"monday": True, "tuesday": True, "wednesday": True, "thursday": True, "friday": True, "saturday": True, "sunday": True})
127
"""
value = 0
if data.get("monday", False):
value |= constants.MONDAY
if data.get("tuesday", False):
value |= constants.TUESDAY
if data.get("wednesday", False):
value |= constants.WEDNESDAY
if data.get("thursday", False):
value |= constants.THURSDAY
if data.get("friday", False):
value |= constants.FRIDAY
if data.get("saturday", False):
value |= constants.SATURDAY
if data.get("sunday", False):
value |= constants.SUNDAY
return value
def decode_days_of_week(value: None | int) -> None | dict[str, bool]:
"""Decodes integer representation of days to standard dictionary.
Examples:
>>> decode_days_of_week(0)
{'monday': False, 'tuesday': False, 'wednesday': False, 'thursday': False, 'friday': False, 'saturday': False, 'sunday': False}
>>> decode_days_of_week(1)
{'monday': True, 'tuesday': False, 'wednesday': False, 'thursday': False, 'friday': False, 'saturday': False, 'sunday': False}
>>> decode_days_of_week(2)
{'monday': False, 'tuesday': True, 'wednesday': False, 'thursday': False, 'friday': False, 'saturday': False, 'sunday': False}
>>> decode_days_of_week(4)
{'monday': False, 'tuesday': False, 'wednesday': True, 'thursday': False, 'friday': False, 'saturday': False, 'sunday': False}
>>> decode_days_of_week(64)
{'monday': False, 'tuesday': False, 'wednesday': False, 'thursday': False, 'friday': False, 'saturday': False, 'sunday': True}
>>> decode_days_of_week(3)
{'monday': True, 'tuesday': True, 'wednesday': False, 'thursday': False, 'friday': False, 'saturday': False, 'sunday': False}
>>> decode_days_of_week(127)
{'monday': True, 'tuesday': True, 'wednesday': True, 'thursday': True, 'friday': True, 'saturday': True, 'sunday': True}
>>> decode_days_of_week(None)
"""
if value is None:
return None
return {
"monday": bool(value & constants.MONDAY),
"tuesday": bool(value & constants.TUESDAY),
"wednesday": bool(value & constants.WEDNESDAY),
"thursday": bool(value & constants.THURSDAY),
"friday": bool(value & constants.FRIDAY),
"saturday": bool(value & constants.SATURDAY),
"sunday": bool(value & constants.SUNDAY),
}
def encode_weekday_ints(data: list[int]) -> int:
"""Encodes list of weekday ints to integer representation.
Examples:
>>> encode_weekday_ints([])
0
>>> encode_weekday_ints([0])
1
>>> encode_weekday_ints([1])
2
>>> encode_weekday_ints([2])
4
>>> encode_weekday_ints([6])
64
>>> encode_weekday_ints([0, 1])
3
>>> encode_weekday_ints([0, 1, 2, 3, 4, 5, 6])
127
"""
value = 0
if 0 in data:
value |= constants.MONDAY
if 1 in data:
value |= constants.TUESDAY
if 2 in data:
value |= constants.WEDNESDAY
if 3 in data:
value |= constants.THURSDAY
if 4 in data:
value |= constants.FRIDAY
if 5 in data:
value |= constants.SATURDAY
if 6 in data:
value |= constants.SUNDAY
return value
def decode_weekday_ints(value: None | int) -> None | list[int]:
"""Decode integer representation of days to list of weekday ints.
Examples:
>>> decode_weekday_ints(0)
[]
>>> decode_weekday_ints(1)
[0]
>>> decode_weekday_ints(2)
[1]
>>> decode_weekday_ints(4)
[2]
>>> decode_weekday_ints(64)
[6]
>>> decode_weekday_ints(3)
[0, 1]
>>> decode_weekday_ints(127)
[0, 1, 2, 3, 4, 5, 6]
>>> decode_weekday_ints(None)
"""
if value is None:
return None
data_list = []
if value & constants.MONDAY:
data_list.append(0)
if value & constants.TUESDAY:
data_list.append(1)
if value & constants.WEDNESDAY:
data_list.append(2)
if value & constants.THURSDAY:
data_list.append(3)
if value & constants.FRIDAY:
data_list.append(4)
if value & constants.SATURDAY:
data_list.append(5)
if value & constants.SUNDAY:
data_list.append(6)
return data_list