Python面向对象与pickle对象持久化示例
Posted ShenLiang2025
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python面向对象与pickle对象持久化示例相关的知识,希望对你有一定的参考价值。
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)
# 追加通讯信息到地址簿
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(data)
print('Contact Added Successfully!')
except:
print('There was an error! Contact was not added.')
finally:
myAddressBook.close()
# 初始化地址簿各元素的输入
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
# 显示地址簿里的所有地址信息
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.')
# 按照姓名搜索地址薄,返回地址薄对象信息
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.')
# 修改通讯薄信息,支持按照姓名、地址、邮箱、电话等信息更新
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!')
以上是关于Python面向对象与pickle对象持久化示例的主要内容,如果未能解决你的问题,请参考以下文章
Python pickle模块:实现Python对象的持久化存储