class AddressBook(object):
def __init__(self, name = None, address = None, email = None, phone = None):
self.filename = 'addressbook'
return '[Name: {0} | Address: {1} | Email: {2} | Phone: {3}]'.format(self.name, self.address, self.email, self.phone)
return '[Name: {0} | Address: {1} | Email: {2} | Phone: {3}]'.format(self.name, self.address, self.email, self.phone)
if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:
myAddressBook = open(self.filename, 'rb')
data = pickle.load(myAddressBook)
myAddressBook = open(self.filename, 'wb')
contact = self.getDetailsFromUser()
data[contact['Name']] = contact
myAddressBook = open(self.filename, 'wb')
pickle.dump(data, myAddressBook)
print('Contact Added Successfully!')
print('There was an error! Contact was not added.')
def getDetailsFromUser(self):
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: '))
except KeyboardInterrupt as error:
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)
for records in data.values():
print('No Record in database.')
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)
contactToSearch = input('Enter the name of the contact to search: ')
for contact in data.values():
if contactToSearch in contact['Name']:
print(data[contact['Name']])
print('No record found whose name is:', contactToSearch)
print('No Record in database.')
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)
contactToModify = input('Enter the name of the contact to modify (Only enter full name): ')
for contact in data.values():
if contactToModify == contact['Name']:
contact = data[contactToModify]
option = int(input('1. To modify name, 2. To modify address, 3. To modify email, 4. To modify phone: '))
contact['Name'] = input('Enter Name to modify: ')
del data[contactToModify]
data[contact['Name']] = contact
contact['Address'] = input('Enter Address to modify: ')
del data[contactToModify]
data[contactToModify] = contact
contact['Email'] = input('Enter Email to modify: ')
del data[contactToModify]
data[contactToModify] = contact
contact['Phone'] = input('Enter Phone to modify: ')
del data[contactToModify]
data[contactToModify] = contact
print('Incorrect option selected.')
print('Error occured. No such record found. Try Again!')
myAddressBook = open(self.filename, 'wb')
pickle.dump(data, myAddressBook)
print('No Record in database.')
if __name__ == '__main__':
print('Enter 1. To Add Contacts 2. For Searching a Contact 3. For Modifying a Contact 4. To Display Contacts 5. To Exit')
choice = int(input('Enter your choice: '))
print('Invalid Option. Try Again!')