-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI Phone book Bot (1).py
More file actions
147 lines (124 loc) · 4.88 KB
/
CLI Phone book Bot (1).py
File metadata and controls
147 lines (124 loc) · 4.88 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import re
exit_command = ["good bye", "close", "exit"]
phone_book = {
"Tatiana Filimonova" : "+380503800803",
"Denys Filimonov": "+380503539526"
}
def format_phone_number(func):
def inner(phone):
result=func(phone)
if len(result) == 12:
result='+'+result
else: result='+38'+result
return result
return inner
@format_phone_number
def sanitize_phone_number(phone):
new_phone = (
phone.strip()
.removeprefix("+")
.replace("(", "")
.replace(")", "")
.replace("-", "")
.replace(" ", "")
)
return new_phone
def is_correct_input(data):
name = ""
phone = ""
message =""
is_correct_input = re.search("[a-z][a-zA-Zа-яА-Я1-9]{1,100} \+?[{0,1}\d \-\(\)]*$",data)
if is_correct_input!=None:
match_ = re.search(" \+?[{0,1}\d \-\(\)]*$", data)
if match_!=None:
phone = sanitize_phone_number(data[match_.start():match_.end()].strip())
name = data[:match_.start()].strip().rstrip()
if len(phone)!=13:
message = "Incorrect telephone number. Use (+)(Country code)(XXX Operator code) XXX XX XX"
else:
message = "Incorrect command format"
return (name, phone, message)
def print_dict(dict):
for key in dict.keys():
print(key," ",dict[key])
def hello_(data):
return "How can I help You?"
def add_(data):
name, phone, message = is_correct_input(data)
if message == "":
if name in phone_book.keys() and phone == phone_book[name]:
print ("Contact is already exist with exactly the same number")
elif name not in phone_book.keys() and phone in phone_book.values():
print ("Another contact has this number")
elif name in phone_book.keys() and phone not in phone_book.values():
choose = ""
while choose not in ["y","Y","n","N"]:
choose = input("Contact already exist with another phone number. Press [Y] if You would like to rewrite it or [N] otherwise: ")
if choose in ["y","Y"]:
phone_book[name] = phone
print ("Contact successfully changed")
elif choose in ["n","N"]:
print ("Contact left unchanged")
return "Please choose command"
else: print ("Choose Y or N")
else:
phone_book[name] = phone
print ("Contact successfully added to phone_book")
else:
print (message)
print ("Please use next format for add comand: ", exec_command["add"][1])
return "Please choose command"
def change_(data):
name, phone, message = is_correct_input(data)
if message == "":
if name in phone_book.keys():
phone_book[name] = phone
print("Contact successfully changed")
else:
print("Contact not in your phone book")
else:
print (message)
print ("Please use next format for add comand: ", exec_command["change"][1])
return "Please choose command"
def phone_(data):
name = data.strip().rstrip().lower()
find_count = 0
for contact in phone_book.keys():
match_ = re.search("\w*"+name+"\w*", contact.lower())
if match_!=None:
print(contact, " ", phone_book[contact])
find_count+=1
if find_count == 0:
print("Couldn't find ", name, " in the phone book")
return "Please choose command"
def show_all(data):
print_dict(phone_book)
return "Please choose command"
def help_(command):
print("List of available commands: ")
for key in exec_command.keys():
print (exec_command[key][1])
return "Please choose command"
# command dictionary {command: [commandhandler_, command description, number of parameters]}
exec_command = {
"hello": [hello_, "hello", 0],
"add": [add_, "add [name] [phone]", 2],
"change": [change_, "change [name] [phone]", 2],
"phone": [phone_, "phone [name]", 1],
"show all": [show_all, "show all", 0],
"help": [help_, "help",0]
}
def handler(command, data):
return exec_command[command][0](data.replace(command+" ",""))
def parser(input_str):
for token in exec_command.keys():
if token in input_str:
return handler(token, input_str.replace(token+" ", ""))
return "Input error, please type 'help' for commands description"
def listener():
command = ""
communication_str = "CLI phone book bot looking for your command"
while (command) not in exit_command:
command = input(communication_str+": ")
communication_str = parser(command)
listener()