-
Notifications
You must be signed in to change notification settings - Fork 17
/
address-book.py
178 lines (163 loc) · 5.99 KB
/
address-book.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
#!/usr/bin/python
#filename address-book.py
import pickle
import os
class Contact:
def __init__(self,name,email,phone):
self.name=name
self.email=email
self.phone=phone
def __str__(self):
return "Name:{0}\nEmail address:{1}\nPhone:{2}".format(self.name,self.email,self.phone)
def change_name(self,name):
self.name=name
def change_email(self,email):
self.email=email
def change_phone(self,phone):
self.phone=phone
def add_contact():
address_book_file=open("address_book_file","r")
is_file_empty=os.path.getsize("address_book_file")==0
if not is_file_empty:
list_contacts=pickle.load(address_book_file)
else:
list_contacts=[]
try:
contact=get_contact_info_from_user()
address_book_file=open("address_book_file","w")
list_contacts.append(contact)
pickle.dump(list_contacts,address_book_file)
print "Contact added"
except KeyboardInterrupt:
print "Contact not added"
except EOFError:
print "Contact not added"
finally:
address_book_file.close()
def get_contact_info_from_user():
try:
contact_name=input("Enter contact name\n")
contact_email=input("Enter contact email\n")
contact_phone=input("Enter contact phone number\n")
contact=Contact(contact_name,contact_email,contact_phone)
return contact
except EOFError as e:
#print "You entered end of file. Contact not added"
raise e
except KeyboardInterrupt as e:
#print "Keyboard interrupt. Contact not added"
raise e
def display_contacts():
address_book_file=open("address_book_file","r")
is_file_empty=os.path.getsize("address_book_file")==0
if not is_file_empty:
list_contacts=pickle.load(address_book_file)
for each_contact in list_contacts:
print each_contact
else:
print "No contacts in address book"
return
address_book_file.close()
def search_contact():
#search_name=input("Enter the name\n")
address_book_file=open("address_book_file","r")
is_file_empty=os.path.getsize("address_book_file")==0
if not is_file_empty:
search_name=input("Enter the name\n")
is_contact_found=False
list_contacts=pickle.load(address_book_file)
for each_contact in list_contacts:
contact_name=each_contact.name
search_name=search_name.lower()
contact_name=contact_name.lower()
if(contact_name==search_name):
print each_contact
is_contact_found=True
break
if not is_contact_found:
print "No contact found with the provided search name"
else:
print "Address book empty. No contact to search"
address_book_file.close()
def delete_contact():
#name=input("Enter the name to be deleted\n")
address_book_file=open("address_book_file","r")
is_file_empty=os.path.getsize("address_book_file")==0
if not is_file_empty:
name=input("Enter the name to be deleted\n")
list_contacts=pickle.load(address_book_file)
is_contact_deleted=False
for i in range(0,len(list_contacts)):
each_contact=list_contacts[i]
if each_contact.name==name:
del list_contacts[i]
is_contact_deleted=True
print "Contact deleted"
address_book_file=open("address_book_file","w")
if(len(list_contacts)==0):
address_book_file.write("")
else:
pickle.dump(list_contacts,address_book_file)
break
if not is_contact_deleted:
print "No contact with this name found"
else:
print "Address book empty. No contact to delete"
address_book_file.close()
def modify_contact():
address_book_file=open("address_book_file","r")
is_file_empty=os.path.getsize("address_book_file")==0
if not is_file_empty:
name=input("Enter the name of the contact to be modified\n")
list_contacts=pickle.load(address_book_file)
is_contact_modified=False
for each_contact in list_contacts:
if each_contact.name==name:
do_modification(each_contact)
address_book_file=open("address_book_file","w")
pickle.dump(list_contacts,address_book_file)
is_contact_modified=True
print "Contact modified"
break
if not is_contact_modified:
print "No contact with this name found"
else:
print "Address book empty. No contact to delete"
address_book_file.close()
def do_modification(contact):
try:
while True:
print ("Enter 1 to modify email and 2 to modify address and 3 to quit without modifying")
choice=input()
if(choice=="1"):
new_email=input("Enter new email address\n")
contact.change_email(new_email)
break
elif(choice=="2"):
new_phone=input("Enter new phone number\n")
contact.change_phone(new_phone)
break
else:
print "Incorrect choice"
break
except EOFError:
print "EOF Error occurred"
except KeyboardInterrupt:
print "KeyboardInterrupt occurred"
print "Enter 'a' to add a contact, 'b' to browse through contacts, 'd' to delete a contact, 'm' to modify a contact, 's' to search for contact and 'q' to quit"
while True:
choice=input("Enter your choice\n")
if choice == 'q':
break
elif(choice=='a'):
add_contact()
elif(choice=='b'):
display_contacts()
elif(choice=='d'):
delete_contact()
elif(choice=='m'):
modify_contact()
elif(choice=='s'):
search_contact()
else:
print "Incorrect choice. Need to enter the choice again"