-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_assistant.py
119 lines (106 loc) · 3.56 KB
/
bot_assistant.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
import sys
CONTACTS_DICT = {}
def parse(user_input):
"""
This function parse user input into command and arguments
:param user_input: user input -> str
:return: command -> str, args -> list
"""
user_input_list = user_input.split(' ')
command = user_input_list[0]
args = user_input_list[1:]
return (command, args)
def input_error(func):
"""
This is a decorator function that catches errors that may occur when calling a function given as a parameter
:param func -> function
:return func if no error, str if there's an error
"""
def inner(*args):
try:
return func(*args)
except KeyError:
return 'The name is not in contacts. Enter user name please'
except ValueError:
return 'ValueError: Give me name and phone please'
except IndexError:
return 'IndexError: Give me name and phone please'
except TypeError:
return 'You entered invalid numbers of arguments for this command'
return inner
@input_error
def add_contact(name, phone):
"""
This function add the name with the phone in parameters into the CONTACTS_DICT
:param name -> str
phone -> str
:return str
"""
CONTACTS_DICT[name] = phone
return f'Contact {name}: {phone} successfully added'
@input_error
def change_contact(name, phone):
"""
This function change the phone for contact with the name that are given as parameters in the CONTACTS_DICT
:param name -> str
phone -> str
:return str
"""
CONTACTS_DICT.update({name: phone})
return f'Contact {name}: {phone} successfully changed'
@input_error
def get_phone(name):
"""
This function change the phone for contact with the name that are given as parameters in the CONTACTS_DICT
:param name -> str
:return phone -> str
"""
phone = CONTACTS_DICT[name]
return f'For {name} the phone is {phone}'
def show_all():
"""
This function returns all contact from the CONTACTS_DICT
:param: None
:return: phone_book -> str
"""
phone_book = ''
for name, contact in CONTACTS_DICT.items():
phone_book += f'{name} : {contact}\n'
return phone_book
def greeting():
return 'How can I help you?'
def end():
return 'Good bye!'
def main():
"""
This function implements all the logic of interaction with the user, all 'print' and 'input' takes place here
:param: None
:return: None
"""
handler_commands = {'hello': greeting,
'hi': greeting,
'add': add_contact,
'change': change_contact,
'phone': get_phone,
'show all': show_all,
'.': end,
'good bye': end,
'close': end,
'exit': end}
while True:
user_input = input('>>>:')
if user_input.lower() in handler_commands.keys():
output = handler_commands[user_input.lower()]()
print(output)
if output == 'Good bye!':
sys.exit()
else:
command, args = parse(user_input.lower())
if command in handler_commands.keys():
print(handler_commands[command](*args))
else:
print(
"You entered an invalid command, please enter one of the next commands: "
"'hello', 'hi', 'show all', 'add', 'change', 'phone', '.', 'good bye', 'close', 'exit'")
if __name__ == '__main__':
main()