import pickle
class AddressBook:
dict = {}
def addphone(self, name, phone):
if name not in AddressBook.dict.keys():
AddressBook.dict[name] = phone
else:
print('{0} at that moment in dictionary'.format(name))
def printbook(self):
if not all(keys == 0 for keys in AddressBook.dict.keys()):
for name, phone in AddressBook.dict.items():
print('name: {0}\tphone: {1}'.format(name, phone))
else:
print('Dictionary is empty.')
def deletePhone(self, name):
if name in AddressBook.dict.keys():
del AddressBook.dict[name]
else:
print('No {0} in dictionary'.format(name))
def changephone(self, name, newphone):
if name in AddressBook.dict.keys():
AddressBook.dict[name] = newphone
else:
print('No {0} in dictionary'.format(name))
def searchphone(self, name):
if name in AddressBook.dict:
print(AddressBook.dict[name])
else:
print('No {0} in dictionary'.format(name))
def savebook(self):
pickle.dump(AddressBook.dict, open('dict.p', 'wb'))
def loadbook(self):
dict2 = pickle.load( open('dict.p', 'rb'))
print(dict2)
aW1wb3J0IHBpY2tsZQoKY2xhc3MgQWRkcmVzc0Jvb2s6CgogICAgZGljdCA9IHt9CgogICAgZGVmIGFkZHBob25lKHNlbGYsIG5hbWUsIHBob25lKToKICAgICAgICBpZiBuYW1lIG5vdCBpbiBBZGRyZXNzQm9vay5kaWN0LmtleXMoKToKICAgICAgICAgICAgQWRkcmVzc0Jvb2suZGljdFtuYW1lXSA9IHBob25lCiAgICAgICAgZWxzZToKICAgICAgICAgICAgcHJpbnQoJ3swfSBhdCB0aGF0IG1vbWVudCBpbiBkaWN0aW9uYXJ5Jy5mb3JtYXQobmFtZSkpCgogICAgZGVmIHByaW50Ym9vayhzZWxmKToKICAgICAgICBpZiBub3QgYWxsKGtleXMgPT0gMCBmb3Iga2V5cyBpbiBBZGRyZXNzQm9vay5kaWN0LmtleXMoKSk6CiAgICAgICAgICAgIGZvciBuYW1lLCBwaG9uZSBpbiBBZGRyZXNzQm9vay5kaWN0Lml0ZW1zKCk6CiAgICAgICAgICAgICAgICBwcmludCgnbmFtZTogezB9XHRwaG9uZTogezF9Jy5mb3JtYXQobmFtZSwgcGhvbmUpKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHByaW50KCdEaWN0aW9uYXJ5IGlzIGVtcHR5LicpCgogICAgZGVmIGRlbGV0ZVBob25lKHNlbGYsIG5hbWUpOgogICAgICAgIGlmIG5hbWUgaW4gQWRkcmVzc0Jvb2suZGljdC5rZXlzKCk6CiAgICAgICAgICAgIGRlbCBBZGRyZXNzQm9vay5kaWN0W25hbWVdCiAgICAgICAgZWxzZToKICAgICAgICAgICAgcHJpbnQoJ05vIHswfSBpbiBkaWN0aW9uYXJ5Jy5mb3JtYXQobmFtZSkpCgogICAgZGVmIGNoYW5nZXBob25lKHNlbGYsIG5hbWUsIG5ld3Bob25lKToKICAgICAgICBpZiBuYW1lIGluIEFkZHJlc3NCb29rLmRpY3Qua2V5cygpOgogICAgICAgICAgICBBZGRyZXNzQm9vay5kaWN0W25hbWVdID0gbmV3cGhvbmUKICAgICAgICBlbHNlOgogICAgICAgICAgICBwcmludCgnTm8gezB9IGluIGRpY3Rpb25hcnknLmZvcm1hdChuYW1lKSkKCiAgICBkZWYgc2VhcmNocGhvbmUoc2VsZiwgbmFtZSk6CiAgICAgICAgaWYgbmFtZSBpbiBBZGRyZXNzQm9vay5kaWN0OgogICAgICAgICAgICBwcmludChBZGRyZXNzQm9vay5kaWN0W25hbWVdKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHByaW50KCdObyB7MH0gaW4gZGljdGlvbmFyeScuZm9ybWF0KG5hbWUpKQoKICAgIGRlZiBzYXZlYm9vayhzZWxmKToKICAgICAgICBwaWNrbGUuZHVtcChBZGRyZXNzQm9vay5kaWN0LCBvcGVuKCdkaWN0LnAnLCAnd2InKSkKCiAgICBkZWYgbG9hZGJvb2soc2VsZik6CiAgICAgICAgZGljdDIgPSBwaWNrbGUubG9hZCggb3BlbignZGljdC5wJywgJ3JiJykpCiAgICAgICAgcHJpbnQoZGljdDIpCg==