-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
151 lines (123 loc) · 4.01 KB
/
bot.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
from address_book import AddressBook, Record, Name, Phone, Birthday
import csv
address_book = AddressBook()
def input_error(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except IndexError:
print("Please provide the required input")
except ValueError:
print("Please enter valid input")
except KeyError:
print("Please enter valid name")
return wrapper
@input_error
def add_contact(name, phone, birthday=None):
name = Name(name.capitalize())
phone = Phone(phone)
if birthday:
birthday = Birthday(birthday)
rec = Record(name, phone, birthday)
address_book.add_record(rec)
return f"Add success {name} {phone}"
@input_error
def change_phone(name, old_phone, new_phone):
name = Name(name.capitalize())
record: Record = address_book.get(name.phone)
if record is None:
raise KeyError(f"{name} does not exist in phone book")
old_phone = Phone(old_phone)
new_phone = Phone(new_phone)
record.update_phone(old_phone, new_phone)
return f"Change success {name} {old_phone.phone} to {new_phone.phone}"
@input_error
def get_phone(*args):
name = str.capitalize(args[0])
user_phones_list = []
for phone in address_book[name].phones:
user_phones_list.append(phone)
phone_list = ', '.join(user_phones_list).strip()
return f"Name: {name} \tPhone numbers: {phone_list}"
@input_error
def remove_phone(*args):
name = Name(args[0])
phone = Phone(args[1])
rec: Record = address_book.get(name.phone)
if rec:
return rec.remove_phone(phone)
else:
raise ValueError
def show_all(*args):
if address_book.data:
return "\n".join(str(r) for r in address_book.data.values())
else:
return "Contact list is empty"
def exit(*args):
address_book.save_to_file('data.csv')
address_book.save_to_file_pickle('data.pickle')
return "Good bye!"
def hello(*args):
return "How can I help you?"
@input_error
def search_address_book(search_text: str):
results = []
for record in address_book.data.values():
name_str = str(record.name).lower()
phone_str = ";".join(str(phone) for phone in record.phones)
if search_text.lower() in name_str or search_text in phone_str:
results.append(str(record))
if results:
return "Results:\n" + "\n".join(results)
else:
return "No results."
def load_address_book(filename):
with open(filename, "r") as f:
reader = csv.reader(f)
for row in reader:
name = Name(row[0])
phones = [Phone(phone) for phone in row[1].split("|")]
birthday = Birthday(row[2]) if row[2] else None
record = Record(name)
record.add_phones(phones)
record.add_birthday(birthday)
address_book.add_record(record)
return address_book
def main():
filename = 'data.csv'
load_address_book(filename)
while True:
user_input = input(">>> ")
if user_input:
command, data = parser(user_input)
result = command(*data)
if result:
if result == "Good bye!":
print(result)
break
print(result)
continue
else:
print(no_command())
def no_command(*args, **kwargs):
return "Unknown command"
COMMANDS = {
hello: ("hello",),
add_contact: ("add",),
get_phone: ("phone",),
change_phone: ("change",),
remove_phone: ("remove",),
search_address_book: ("search",),
show_all: ("show all",),
exit: ("close", "exit", "good bye"),
}
@input_error
def parser(text: str) -> tuple[callable, tuple[str] | None]:
for cmd, keywords in COMMANDS.items():
for keyword in keywords:
if text.lower().startswith(keyword):
data = text[len(keyword):].strip().split()
return cmd, data
return no_command, []
if __name__ == "__main__":
main()