-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_assistant.py
More file actions
128 lines (94 loc) · 2.95 KB
/
Copy pathbot_assistant.py
File metadata and controls
128 lines (94 loc) · 2.95 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
from classes import AddressBook, Record
contacts = AddressBook()
def input_error(func):
def inner(*args, **kwargs):
try:
return func(*args, **kwargs)
except KeyError:
return 'User with this name is not available.'
except ValueError:
return 'Name and phone is not available.'
except IndexError:
return 'Name and phone is not available.'
return inner
def hello():
return 'How can I help you?'
@input_error
def add(contact):
name, phones = split_info(contact)
if name in contacts:
raise ValueError('This contact already exists.')
record = Record(name)
for phone in phones:
record.add_phone(phone)
contacts.add_record(record)
return f'Number {phone} with name {name} was added.'
@input_error
def change(contact):
name, phone = split_info(contact)
record = contacts[name]
record.change_phones(phone)
return f'Number {phone} with name {name} was changed.'
@input_error
def show_phone(contact):
return contacts.search(contact.strip()).get_info()
def show_all():
if contacts:
all_contacts = ''
for key, record in contacts.get_all_record().items():
all_contacts += f'{record.get_info()}\n'
return all_contacts
else:
return 'There are no contacts.'
@input_error
def delete_contact(name):
name = name.strip()
contacts.remove_record(name)
return "The contact was deleted."
def bye():
return 'Good bye!'
def unknown_action():
return 'This command is not available'
COMMANDS = {
'hello': hello,
'add': add,
'change': change,
'phone': show_phone,
'show all': show_all,
'delete': delete_contact,
'good bye': bye,
'exit': bye,
'close': bye,
'.': bye
}
def change_input(user_input):
new_input = user_input
data = ''
for key in COMMANDS:
if user_input.strip().lower().startswith(key):
new_input = key
data = user_input[len(new_input):]
break
if data:
return reaction_func(new_input)(data)
return reaction_func(new_input)()
def reaction_func(reaction):
return COMMANDS.get(reaction, unknown_action)
def split_info(data):
name, *phones = data.strip().split(' ')
if name.isnumeric():
raise ValueError('Wrong name.')
for phone in phones:
if not phone.isnumeric():
raise ValueError('Wrong phones.')
return name, phones
def main():
print('Enter commands: hello, add (name phone), change (name phone), phone (phone), delete(name), show all.')
print('To stop the bot, input good bye, close, exit.')
while True:
user_input = input('Please enter command: ')
result = change_input(user_input)
print(result)
if result == 'Good bye!':
exit()
main()