-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_hw09_my.py
More file actions
117 lines (90 loc) · 2.81 KB
/
main_hw09_my.py
File metadata and controls
117 lines (90 loc) · 2.81 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
COMMAND_EXIT = ['good bye', 'close', 'exit']
contacts_dict = [{"name": "Artem", "phone": "+380504848484"},
{"name": "Ira", "phone": "+380501111111"},
{"name": "Diana", "phone": "+380502222222"},]
def format_phone_number(func):
def wrapper(x):
result = func(x)
if len(result) == 12:
result = '+' + result
else:
result = '+38' + result
return result
return wrapper
@format_phone_number
def sanitize_phone_number(phone):
new_phone = (
phone.strip()
.removeprefix("+")
.replace("(", "")
.replace(")", "")
.replace("-", "")
.replace(" ", "")
)
return new_phone
def hello_func(text=None):
return f'How can I help you?'
def add_func(text):
global contacts_dict
if len(text) < 2:
return f'name or phone unknow format'
name = text[0].capitalize()
phone = sanitize_phone_number(text[1])
new_contact = {"name": name, "phone": phone}
contacts_dict.append(new_contact)
return f'add contact {name} - {phone}'
def show_all(text=None):
result = 'Contacts list \n'
for i in contacts_dict:
result += 'name ' + i["name"] + ' phone ' + i["phone"] + "\n"
return result
def change_contact(list_contacts):
global contacts_dict
if len(list_contacts) < 2:
return f'name or phone unknow format'
name = list_contacts[0].capitalize()
phone = sanitize_phone_number(list_contacts[1])
for i in contacts_dict:
if i["name"] == name:
i["phone"] = phone
return f'contast {i["name"]} take new phone {i["phone"]}'
return f'Not {name} in contact list'
def phone_print(name):
contact = name[0].capitalize().strip()
for i in contacts_dict:
if i["name"] == contact:
return f'name: {contact} phone: {i["phone"]}'
return f'Not {contact} in contact list'
COMMAND_DICT = {
hello_func: "hello",
add_func: "add",
show_all: "show all",
change_contact: "change",
phone_print: "phone"
}
def select_command(text):
for key, val in COMMAND_DICT.items():
if val == text:
return key
return f'non command in list'
def parser_text(text):
text = text.lower().strip()
if text.startswith("show all"):
return "show all", None
else:
user_text = text.split()
return user_text[0], user_text[1:]
def main():
while True:
user_input = input('>>input>>')
if user_input.lower().strip() in COMMAND_EXIT:
print('Good bye!')
break
user_command, other_text = parser_text(user_input)
result = select_command(user_command)
if type(result) == str:
print(result)
else:
print(result(other_text))
if __name__ == "__main__":
main()