-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (101 loc) · 3.14 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
from address_book import AddressBook
from record import Record
from storage import save_data, load_data
not_found_message = "Contact does not exist, you can add it"
def input_error(func):
def inner(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as error:
return str(error)
return inner
@input_error
def add_contact(args, book: AddressBook):
name, phone = args
record = book.find(name)
message = "Contact updated."
if record is None:
record = Record(name)
book.add_record(record)
message = "Contact added."
if phone:
record.add_phone(phone)
return message
@input_error
def change_contact(args, book: AddressBook):
if len(args) != 3:
return "Invalid number of arguments. Usage: change [name] [old_number] [new_number]"
name, old_number, new_number = args
record = book.find(name)
if record is None:
return not_found_message
else:
record.edit_phone(old_number, new_number)
return "Phone changed"
@input_error
def show_phone(args, book: AddressBook):
if len(args) != 1:
return "Invalid number of arguments. Usage: phone [name]"
name = args[0]
record = book.find(name)
if record is None:
return not_found_message
return record
@input_error
def add_birthday(args, book: AddressBook):
if len(args) != 2:
return "Invalid number of arguments. Usage: add-birthday [name] [date]"
name, date = args
record = book.find(name)
if record:
record.add_birthday(date)
return "Birthday added."
else:
return not_found_message
@input_error
def show_birthday(args, book: AddressBook):
if len(args) != 1:
return "Invalid number of arguments. Usage: show-birthday [name]"
name = args[0]
record = book.find(name)
if record:
if record.birthday:
return record.birthday
else:
return "Birthday not added to this contact."
else:
return not_found_message
def parse_input(user_input):
cmd, *args = user_input.split()
cmd = cmd.strip().lower()
return cmd, *args
def main():
book = load_data()
print("Welcome to the assistant bot!")
while True:
user_input = input("Enter a command: ")
command, *args = parse_input(user_input)
if command == "hello":
print("How can I help you?")
elif command in ["close", "exit"]:
save_data(book)
print("Good bye!")
break
elif command == "add":
print(add_contact(args, book))
elif command == "change":
print(change_contact(args, book))
elif command == "phone":
print(show_phone(args, book))
elif command == "all":
print(book)
elif command == "add-birthday":
print(add_birthday(args, book))
elif command == "show-birthday":
print(show_birthday(args, book))
elif command == "birthdays":
print(book.get_upcoming_birthdays())
else:
print("Invalid command.")
if __name__ == "__main__":
main()