-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressBook.py
More file actions
304 lines (224 loc) · 8.17 KB
/
Copy pathAddressBook.py
File metadata and controls
304 lines (224 loc) · 8.17 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from collections import UserDict
from datetime import date, timedelta, datetime
from collections import defaultdict
import calendar
class Field:
"""
Base class for field
"""
def __init__(self, value):
self._value = None
self._value = value
def __str__(self):
return str(self._value)
@property
def value(self) -> str:
return self._value
class Name(Field):
"""
Represent name field
"""
def __init__(self, name: str):
"""
Constructor
Args:
name (str): name
Raises:
ValueError: Name cannot be empty
"""
if not name or len(name) == 0:
raise ValueError("Name cannot be empty")
super().__init__(name)
class Phone(Field):
"""
Phone field
"""
def __init__(self, phone: str):
"""
Constructor
Args:
phone (str): phone number
"""
if not phone.isdigit() or len(phone) != 10:
raise ValueError("Invalid phone number format. It should contain 10 digits.")
super().__init__(phone)
@property
def value(self) -> str:
return self._value
@value.setter
def value(self, value: str):
"""
Value property setter
Args:
input_value (str): phone number
Raises:
ValueError: Invalid phone number format. It should contain 10 digits.
"""
if not value.isdigit() or len(value) != 10:
raise ValueError("Invalid phone number format. It should contain 10 digits.")
self._value = value
class Birthday(Field):
"""
Birthday field
"""
def __init__(self, date: date):
"""
Constructor
Args:
date (date): birthday date
"""
super().__init__(date)
class Record:
"""
Class represent address book record
Attributes
----------
name : str
Name of the address book record
phones : str
List of the phones
"""
def __init__(self, name: str):
"""
Constructor of class
Args:
name (str): name of the record
"""
self.name = Name(name)
self.birthday = None
self.phones = []
def add_phone(self, phone: str):
"""
Add phone to the record
Args:
phone (str): phone number
"""
self.phones.append(Phone(phone))
def add_birthday(self, birthday: date):
"""
Add birthday to the record
Args:
birthday (datetime): birthday
"""
self.birthday = Birthday(birthday)
def remove_phone(self, phone: str):
"""
Remove phone number from the record
Args:
phone (str): phone number
"""
self.phones = [p for p in self.phones if p.value != phone]
def edit_phone(self, old_phone: str, new_phone: str):
"""
Change phone number
Args:
old_phone (str): Old phone number
new_phone (str): New phone number
"""
for phone in self.phones:
if phone.value == old_phone:
phone.value = new_phone
break
def find_phone(self, phone: str) -> str:
"""
Search by phone number
Args:
phone (str): phone number
Returns:
str: phone number
"""
for p in self.phones:
if p.value == phone:
return p.value
return None
def __str__(self) -> str:
"""
String representation of record
Returns:
str: String
"""
return f"Contact name: {self.name.value}, phones: {'; '.join(str(p) for p in self.phones)}"
class AddressBook(UserDict):
"""
Class address book to store Records
Args:
UserDict ([Records]): records
"""
def add_record(self, record: Record):
"""
Add record to address book
Args:
record (Record): record to add
"""
self.data[record.name.value] = record
def find(self, name: str) -> Record:
"""
Search record in address book by name
Args:
name (str): name
Returns:
Record: record by name
"""
return self.data.get(name)
def delete(self, name: str):
"""
Delete record by name
Args:
name (str): name of record
"""
if name in self.data:
del self.data[name]
def get_birthdays_per_week(self) -> None:
"""
Based on input dictionary print peoples who's birthday is within next week
Logic:
In case today is Monday - we print all birthdays for next 7 days ignoring weekends
In case today is not Monday - we print all birthdays for next 7 days moving weekend birthday to Monday
Args:
users (dictionary): Birthday catalog (name and date)
Format:
{ "name": "<name>", "birthday": datetime() },
Sample:
{ "name": "Bill Foolkland", "birthday": datetime(1955, 10, 28) },
Output:
Print in console who's birthday within this week
Sample:
Monday: Bob, Lily Gates, Lily Evans
Tuesday: Geremy Evans
Wednesday: Geremy Gates
Thursday: Karter Gates
Friday: Karter Gates
"""
# const
DAY_OF_WEEK_CODE_FRIDAY = 4
DAY_OF_WEEK_CODE_MONDAY = 0
today_date = datetime.today().date()
is_monday = today_date.weekday()
date_range_upper_limit = today_date + timedelta(days=7)
birthdays_todo = defaultdict(list)
for name, record in self.data.items():
if name == "":
print(f"Warning: name is empty. Birthday: {str(birthday)}")
birthday = record.birthday.value
birthday_this_year = birthday.replace(year=today_date.year)
# case: today_date is monday
# calculate all birthday up to Friday (ignore Sat,Sun)
if is_monday and (today_date <= birthday_this_year
and birthday_this_year < date_range_upper_limit):
weekday_code = birthday_this_year.weekday()
if weekday_code <= DAY_OF_WEEK_CODE_FRIDAY:
weekday_name = calendar.day_name[weekday_code]
birthdays_todo[weekday_name].append(name)
# case: today_date is not monday
# calculate all birthday for 7 days (Sat,Sun move to monday)
if (not is_monday) and (today_date <= birthday_this_year
and birthday_this_year < date_range_upper_limit):
weekday_code = birthday_this_year.weekday()
if weekday_code <= DAY_OF_WEEK_CODE_FRIDAY:
weekday_name = calendar.day_name[weekday_code]
else:
# move birthday to Monday
weekday_name = calendar.day_name[DAY_OF_WEEK_CODE_MONDAY]
birthdays_todo[weekday_name].append(name)
# print results
return birthdays_todo