-
Notifications
You must be signed in to change notification settings - Fork 595
/
P61_AddressBook.py
150 lines (138 loc) · 6.08 KB
/
P61_AddressBook.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
# Author: OMKAR PATHAK
# In this small mini project we will be creating a simple address book application that will store, search and
# delete records
import pickle, os
class AddressBook(object):
def __init__(self, name = None, address = None, email = None, phone = None):
self.name = name
self.address = address
self.email = email
self.phone = phone
self.contacts = {}
self.filename = 'addressbook'
def __str__(self):
return '[Name: {0} | Address: {1} | Email: {2} | Phone: {3}]'.format(self.name, self.address, self.email, self.phone)
def __repr__(self):
return '[Name: {0} | Address: {1} | Email: {2} | Phone: {3}]'.format(self.name, self.address, self.email, self.phone)
# Adding details provided by the user in our Address Book
def addContacts(self):
try:
if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:
myAddressBook = open(self.filename, 'rb')
data = pickle.load(myAddressBook)
myAddressBook.close()
else:
myAddressBook = open(self.filename, 'wb')
data = {}
contact = self.getDetailsFromUser()
data[contact['Name']] = contact
myAddressBook = open(self.filename, 'wb')
pickle.dump(data, myAddressBook)
myAddressBook.close()
print('Contact Added Successfully!')
except:
print('There was an error! Contact was not added.')
finally:
myAddressBook.close()
# Getting the details from the user to adding the Address Book
def getDetailsFromUser(self):
try:
self.contacts['Name'] = str(input('Enter Contact\'s Full Name: '))
self.contacts['Address'] = str(input('Enter Contact\'s Address: '))
self.contacts['Email'] = str(input('Enter Contact\'s Email Address: '))
self.contacts['Phone'] = int(input('Enter Contact\'s Phone Number: '))
return self.contacts
except KeyboardInterrupt as error:
raise error
# To display ALL the contact in our Address Book
def displayContacts(self):
if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:
myAddressBook = open(self.filename, 'rb')
data = pickle.load(myAddressBook)
myAddressBook.close()
if data:
for records in data.values():
print(records)
myAddressBook.close()
else:
print('No Record in database.')
# To search for a specific contact in our Address Book
def searchContacts(self):
if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:
myAddressBook = open(self.filename, 'rb')
data = pickle.load(myAddressBook)
myAddressBook.close()
try:
contactToSearch = input('Enter the name of the contact to search: ')
counter = 0
for contact in data.values():
if contactToSearch in contact['Name']:
print(data[contact['Name']])
counter += 1
if counter == 0:
print('No record found whose name is:', contactToSearch)
except:
print('Error occured!')
else:
print('No Record in database.')
# For modifying contacts
def modifyContacts(self):
if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:
myAddressBook = open(self.filename, 'rb')
data = pickle.load(myAddressBook)
myAddressBook.close()
try:
contactToModify = input('Enter the name of the contact to modify (Only enter full name): ')
# Search for the record to update
for contact in data.values():
if contactToModify == contact['Name']:
contact = data[contactToModify]
break
option = int(input('1. To modify name, 2. To modify address, 3. To modify email, 4. To modify phone: '))
if option == 1:
contact['Name'] = input('Enter Name to modify: ')
del data[contactToModify]
data[contact['Name']] = contact
print('Successful')
elif option == 2:
contact['Address'] = input('Enter Address to modify: ')
del data[contactToModify]
data[contactToModify] = contact
print('Successful')
elif option == 3:
contact['Email'] = input('Enter Email to modify: ')
del data[contactToModify]
data[contactToModify] = contact
print('Successful')
elif option == 4:
contact['Phone'] = input('Enter Phone to modify: ')
del data[contactToModify]
data[contactToModify] = contact
print('Successful')
else:
print('Incorrect option selected.')
except:
print('Error occured. No such record found. Try Again!')
finally:
myAddressBook = open(self.filename, 'wb')
pickle.dump(data, myAddressBook)
myAddressBook.close()
else:
print('No Record in database.')
if __name__ == '__main__':
myBook = AddressBook()
print('Enter 1. To Add Contacts 2. For Searching a Contact 3. For Modifying a Contact 4. To Display Contacts 5. To Exit')
while True:
choice = int(input('Enter your choice: '))
if choice == 1:
myBook.addContacts()
elif choice == 2:
myBook.searchContacts()
elif choice == 3:
myBook.modifyContacts()
elif choice == 4:
myBook.displayContacts()
elif choice == 5:
exit()
else:
print('Invalid Option. Try Again!')