-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
201 lines (170 loc) · 8.55 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import ast
import os
import time
def convert_list_2_bin(contact_list):
file = open('data.bin', 'wb')
file.write(str(contact_list).encode('ascii'))
file.close()
def convert_bin_2_list():
file = open('data.bin', 'rb')
line = file.read()
if line:
return ast.literal_eval(line.decode('ascii'))
else:
return []
def len_fit(text, length, char=' '):
return str(text + char * (length - len(text)))
def header(title):
os.system('cls' if os.name == 'nt' else 'clear')
print('\n\n\n\n')
print(' ╔══════════════════════════════════════════════════╗')
print(' ║ {}║'.format(len_fit(title, 46)))
print(' ╠══════════════════════════════════════════════════╣')
def menu_generator(message_text=None):
header('Contact Book')
print(' ║ 1. Add ║')
print(' ║ 2. List ║')
print(' ║ 3. Search ║')
print(' ║ 4. Sort ║')
print(' ║ 5. Delete ║')
print(' ║ 6. Exit ║')
print(' ╚══════════════════════════════════════════════════╝')
if message_text:
print(' {}'.format(message_text))
print()
input_choice = input(' Please enter your choice: ')
return input_choice
def vcard(title, contact_info):
first_name = contact_info['first_name']
last_name = contact_info['last_name']
phone = contact_info['phone']
address = contact_info['address']
postal_code = contact_info['postal_code']
header(title)
print(' ║ First name: {}║'.format(len_fit(first_name, 37)))
print(' ║ Last name: {}║'.format(len_fit(last_name, 38)))
print(' ║ Phone number: {}║'.format(len_fit(phone, 35)))
print(' ║ Address: {}║'.format(len_fit(address, 40)))
print(' ║ Postal code: {}║'.format(len_fit(postal_code, 36)))
print(' ╚══════════════════════════════════════════════════╝')
def add():
contact = {
'first_name': '',
'last_name': '',
'phone': '',
'address': '',
'postal_code': '',
}
vcard('Add Contact', contact)
contact['first_name'] = input(' Please enter First name: ')
vcard('Add Contact', contact)
contact['last_name'] = input(' Please enter Last name: ')
vcard('Add Contact', contact)
contact['phone'] = input(' Please enter Phone number: ')
vcard('Add Contact', contact)
contact['address'] = input(' Please enter Address: ')
vcard('Add Contact', contact)
contact['postal_code'] = input(' Please enter Postal code: ')
vcard('Add Contact', contact)
confirmation = input(' Do you confirm the information [Y/n]: ')
if confirmation == '' or confirmation == 'Y' or confirmation == 'y':
list_contact = convert_bin_2_list()
list_contact.append(contact)
convert_list_2_bin(list_contact)
def vcard_list(contact_list):
for count, contact in enumerate(contact_list):
first_name = contact['first_name']
last_name = contact['last_name']
phone = contact['phone']
address = contact['address']
postal_code = contact['postal_code']
print(' ║ First name: {}║'.format(len_fit(first_name, 37)))
print(' ║ Last name: {}║'.format(len_fit(last_name, 38)))
print(' ║ Phone number: {}║'.format(len_fit(phone, 35)))
print(' ║ Address: {}║'.format(len_fit(address, 40)))
print(' ║ Postal code: {}║'.format(len_fit(postal_code, 36)))
if count + 1 == len(contact_list):
print(' ╚══════════════════════════════════════════════════╝')
else:
print(' ╠══════════════════════════════════════════════════╣')
def list_of_contact():
list_contact = convert_bin_2_list()
header('List of Contacts')
vcard_list(list_contact)
input(' Press Enter to return ')
def search():
list_contact = convert_bin_2_list()
len_list_contact = str(len(list_contact))
header('Search in Contacts')
print(' ║ Number of contacts: {}║'.format(len_fit(len_list_contact, 29)))
print(' ║ Search based on First name ║')
print(' ╚══════════════════════════════════════════════════╝')
query = input(' Enter Fist name: ')
try:
result = next(item for item in list_contact if item["first_name"] == query)
vcard('Contact Information', result)
input(' Press Enter to return ')
except:
print(' Contact not found!')
input(' Press Enter to return ')
def sort():
list_contact = convert_bin_2_list()
newlist = sorted(list_contact, key=lambda k: k['first_name'])
header('Search in Contacts')
vcard_list(newlist)
input(' Press Enter to return ')
def delete():
list_contact = convert_bin_2_list()
len_list_contact = str(len(list_contact))
header('Search in Contacts')
print(' ║ Number of contacts: {}║'.format(len_fit(len_list_contact, 29)))
print(' ║ Search based on First name ║')
print(' ╚══════════════════════════════════════════════════╝')
query = input(' Enter Fist name: ')
try:
result = next(item for item in list_contact if item["first_name"] == query)
vcard('Delete Contact', result)
confirmation = input(' Are you sure to delete this contact? [y/N] ')
if confirmation == 'Y' or confirmation == 'y':
for count, contact in enumerate(list_contact):
if result['first_name'] == contact['first_name']:
list_contact.pop(count)
convert_list_2_bin(list_contact)
except:
print(' Contact not found!')
input(' Press Enter to return ')
def about():
header('Contact Book')
print(' ║ Lesson: Save and recover data ║')
print(' ║ Home work: HW1 ║')
print(' ║ Teacher: Pr. Rasekh ║')
print(' ║ Student: Amin Hemmati ║')
print(' ║ Student number: 9672193 ║')
print(' ╚══════════════════════════════════════════════════╝')
time.sleep(5)
def menu():
should = True
message = None
while should:
choice = menu_generator(message)
message = None
if choice == '1':
add()
elif choice == '2':
list_of_contact()
elif choice == '3':
search()
elif choice == '4':
sort()
elif choice == '5':
delete()
elif choice == '6':
should = False
else:
message = 'You enter a wrong character!'
about()
menu()
os.system('cls' if os.name == 'nt' else 'clear')
print('\n\n\n\n\n github: https://github.com/AminHCE/Contact-Book')
input(' Press Enter to Exit ')
os.system('cls' if os.name == 'nt' else 'clear')