首页 > 代码库 > What next? ---class, def, pickle, dictionary, make a address book
What next? ---class, def, pickle, dictionary, make a address book
<A Byte of Python> Chapter 16. What next?
Problem: create your own command-line address-book program using which you can add, modify, delete or search for your contacts such as friends, family and colleagues and their information such as email address and/or phone number. Details must be stored for later retrieval.
Hint. Create a class to represent the person‘s information. Use a dictionary to store person objects with their name as the key. Use the cPickle module to store the objects persistently on your hard disk. Use the dictionary built-in methods to add, delete and modify the persons.
ab = {‘Xiaopeng Yang‘: (18000001219, ‘18700000019@139.com‘)} del ab[‘Xiaopeng Yang‘] class AddressBook: ‘‘‘Represents any information.‘‘‘ def __init__(self, name, phonenumber): self.name = name self.phonenumber = phonenumber print ‘(Initialized AddressBook: %s)‘ %self.name def tell(self): ‘‘‘Tell my details.‘‘‘ print ‘Name:"%s" Phone number:"%s"‘ % (self.name, self.phonenumber), class Friend(AddressBook): ‘‘‘Represents a teacher.‘‘‘ def __init__(self, name, phonenumber, emailaddress): AddressBook.__init__(self, name, phonenumber) self.emailaddress = emailaddress print ‘(Initialized Friend: %s)‘ % self.name def tell(self): AddressBook.tell(self) print ‘Email address: "%s"‘ %self.emailaddress ab[self.name] = (self.phonenumber, self.emailaddress) # add friend information to dictionary class Family(AddressBook): def __init__(self, name, phonenumber): AddressBook.__init__(self, name, phonenumber) print ‘(Initialized Family: %s)‘ % self.name def tell(self): AddressBook.tell(self) ab[self.name] = (self.phonenumber,[]) print #print s blank line class Colleague(AddressBook): def __init__(self, name, phonenumber, emailaddress): AddressBook.__init__(self, name, phonenumber) self.emailaddress = emailaddress print ‘(Initialized Colleague: %s)‘ % self.name def tell(self): AddressBook.tell(self) print ‘Email address: "%s"‘ %self.emailaddress ab[self.name] = (self.phonenumber, self.emailaddress) print #print s blank line f = Friend(‘Qiping Kong‘, 1590100007, ‘000004369@qq.com‘) a = Family(‘Haixia Li‘, 13522000000) c = Colleague(‘Maggie Zhang‘, 13000000827,‘MZhang@mmmmmm.org‘) print #print s blank line members = [f, a, c] for member in members: member.tell() # works for Friend, Family and Colleague if ‘Qiping Kong‘ in ab: print ‘I am Qiping Kong, my phone numberis %d email is %s‘ % ab[‘Qiping Kong‘] for name, (phone, email) in ab.items(): print ‘Contact %s phone number is %d, email address is %s‘ % (name, phone, email) import cPickle as p #import pickle as p AddressListfile = ‘AddressList.data‘ # the name of the file where we store the information AddressList = ab # Write to the file f = file(AddressListfile, ‘w‘) c = p.dump(AddressList, f) #dump the object to a file f.close() del AddressList # remove the AddressList #Read back from the storage d = file(AddressListfile) shoredlist = p.load(d) print shoredlist
Output:
================ RESTART: /Users/zhouxin/Desktop/What next.py ================
(Initialized AddressBook: Qiping Kong)
(Initialized Friend: Qiping Kong)
(Initialized AddressBook: Haixia Li)
(Initialized Family: Haixia Li)
(Initialized AddressBook: Maggie Zhang)
(Initialized Colleague: Maggie Zhang)
Name:"Qiping Kong" Phone number:"1590100007" Email address: "000004369@qq.com"
Name:"Haixia Li" Phone number:"13522000000"
Name:"Maggie Zhang" Phone number:"13000000827" Email address: "MZhang@mmmmmm.org"
I am Qiping Kong, my phone numberis 1590100007 email is 000004369@qq.com
Contact Maggie Zhang phone number is 13000000827, email address is MZhang@mmmmmm.org
Contact Qiping Kong phone number is 1590100007, email address is 000004369@qq.com
Contact Haixia Li phone number is 13522000000, email address is []
{‘Maggie Zhang‘: (13000000827L, ‘MZhang@mmmmmm.org‘), ‘Qiping Kong‘: (1590100007, ‘000004369@qq.com‘), ‘Haixia Li‘: (13522000000L, [])}
>>>
What next? ---class, def, pickle, dictionary, make a address book