-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
333 lines (268 loc) · 7.63 KB
/
main.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import functools
import os
import re
import datetime
import sys
from classes.addressbook import AddressBook
from classes.record import Record
from collections import defaultdict
from pathlib import Path
import pickle
def input_error(func):
"""
:param func:
:return:
"""
@functools.wraps(func)
def inner(*args, **kwargs):
"""
:param args:
:param kwargs:
:return:
"""
try:
return func(*args, **kwargs)
except (ValueError, IndexError) as er:
return er
return inner
def parse_input(command: str):
"""
:param command:
:return:
"""
cmd, *args = re.split(r"\s+", command)
cmd = cmd.lower()
return cmd, args
@input_error
def add_contact(args, book: AddressBook) -> str:
"""
:param args:
:param book:
:return:
"""
try:
name, phone, *_ = args
except ValueError:
raise ValueError('Input name and phone number')
record = book.find(name)
message = "Contact updated."
if record is None:
record = Record(name)
record.add_phone(phone)
book.add_record(record)
message = "Contact added."
else:
record.add_phone(phone)
return message
def show_all(book: AddressBook) -> str:
"""
:param book:
:return:
"""
result = ""
for name, record in book.data.items():
result += f"\n{record}"
return result
@input_error
def change_contact(args, book: AddressBook) -> str:
"""
:param args:
:param book:
:return:
"""
try:
name, phone, *_ = args
except ValueError:
raise ValueError('Input name and phone number')
record = book.find(name)
record.edit_phone(phone)
return 'Contact changed.'
@input_error
def show_contact(args, book: AddressBook) -> str:
"""
:param args:
:param book:
:return:
"""
try:
name, *_ = args
except ValueError:
raise ValueError('Input name')
record = book.find(name)
return record
@input_error
def add_birthday(args, book) -> str:
"""
:param args:
:param book:
"""
try:
name, birthday, *_ = args
except ValueError:
raise ValueError('Input name and birthday')
record = book.find(name)
message = "Contact not found."
if isinstance(record, Record):
record.add_birthday(birthday)
message = "Birthday added."
return message
@input_error
def show_birthday(args, book) -> str:
"""
:param args:
:param book:
:return:
"""
try:
name, *_ = args
except ValueError:
raise ValueError('Input name')
record = book.find(name)
message = "Contact not found."
if isinstance(record, Record):
message = record.birthday.value.strftime("%d.%m.%Y") \
if record.birthday is not None \
else f"{name} birthday not found."
return message
def next_monday(_datetime) -> datetime:
"""
:param _datetime:
:return:
"""
days_ahead = 0 - _datetime.weekday()
if days_ahead <= 0:
days_ahead += 7
return _datetime + datetime.timedelta(days_ahead)
def birthdays(book: AddressBook):
"""
:param book:
:return:
"""
result = ""
for date, records in get_upcoming_birthdays(book).items():
result += f"\n{date}"
for record in records:
result += f"\n{record}"
return result
def get_upcoming_birthdays(book: AddressBook):
"""
:param book:
:return:
"""
current_year = datetime.datetime.now().year
today_date = datetime.datetime.today().date()
weekdays = [today_date + datetime.timedelta(days=x) for x in range(0, 7)]
result = defaultdict(list)
for name, record in book.data.items():
date_birthday = record.birthday.value.replace(year=current_year) if record.birthday is not None else None
if date_birthday in weekdays:
if date_birthday.weekday() in (5, 6):
date_birthday = next_monday(date_birthday)
result[date_birthday.strftime('%d.%m.%Y')].append(record)
return result
STORAGE_PATH = './storage/addressbook.pkl'
def save_data(book: AddressBook) -> str:
"""
:param book:
:return:
"""
with open(Path(STORAGE_PATH), 'wb') as f:
pickle.dump(book, f)
return "Contacts saved!"
def load_data() -> AddressBook:
"""
:return:
"""
try:
with open(Path(STORAGE_PATH), 'rb') as file:
return pickle.load(file)
except FileNotFoundError:
return AddressBook()
def main():
book = load_data()
print("Welcome to the assistant bot!")
while True:
try:
user_input = input("Enter a command: ")
command, args = parse_input(user_input)
if command in ["close", "exit"]:
save_data(book)
print("Good bye!")
break
elif command == "hello":
result = "How can I help you?"
elif command == "add":
result = add_contact(args, book)
elif command == "change":
result = change_contact(args, book)
elif command == "phone":
result = show_contact(args, book)
elif command == "all":
result = show_all(book)
elif command == "add-birthday":
result = add_birthday(args, book)
elif command == "show-birthday":
result = show_birthday(args, book)
elif command == "birthdays":
result = birthdays(book)
else:
result = "Invalid command."
print(result)
except KeyboardInterrupt:
save_data(book)
print("Good bye!")
break
def test():
book = load_data()
print(add_contact(['Tim', '0961296501'], book))
print(add_contact(['Mike', '0971456509'], book))
print(add_contact(['Mike', '0501111111'], book))
print(add_contact(['Lisa', '096129650'], book))
print(add_contact(['Peter', '0380031234'], book))
print(add_contact(['Janet', '0730231771'], book))
print(add_contact(['Victor', '0115567948'], book))
print(show_birthday(['Ken'], book))
print(show_birthday(['Tim'], book))
print(show_birthday([], book))
print(show_contact(['Lisa'], book))
print(show_contact(['Mike'], book))
print(add_contact(['Kenny'], book))
print(show_all(book))
print(change_contact(['Tim', '0505555555'], book))
print(add_birthday(['Tim', '21.11.1982'], book))
print(add_birthday(['Mike', '09.04.1981'], book))
print(add_birthday(['Mike', '09.04.198'], book))
print(add_birthday(['Janet', '07.04.1999'], book))
print(add_birthday(['John'], book))
print(show_birthday(['Mike'], book))
print(add_contact(['Jonny', '0389912345'], book))
print(add_contact(['Miriam', '0385912111'], book))
print(add_birthday(['Miriam', '11.04.2009'], book))
print(add_birthday(['Peter', '13.04.2001'], book))
print(add_birthday(['Victor', '13.04.2001'], book))
print(show_all(book))
print(birthdays(book))
save_data(book)
def show():
if load_data():
print(show_all(load_data()))
else:
print('No data saved!')
def clear():
try:
os.remove(Path(STORAGE_PATH))
print("File removed!")
except OSError as error:
print(error)
if __name__ == '__main__':
if len(sys.argv) < 2:
main()
else:
if sys.argv[1] == 'test':
test()
elif sys.argv[1] == 'clear':
clear()
elif sys.argv[1] == 'show':
show()
else:
print("Invalid parameter.")