<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, ‘[email protected]‘)} 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, ‘[email protected]‘) a = Family(‘Haixia Li‘, 13522000000) c = Colleague(‘Maggie Zhang‘, 13000000827,‘[email protected]‘) 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: "00000[email protected]"
Name:"Haixia Li" Phone number:"13522000000"
Name:"Maggie Zhang" Phone number:"13000000827" Email address: "[email protected]"
I am Qiping Kong, my phone numberis 1590100007 email is [email protected]
Contact Maggie Zhang phone number is 13000000827, email address is [email protected]
Contact Qiping Kong phone number is 1590100007, email address is [email protected]
Contact Haixia Li phone number is 13522000000, email address is []
{‘Maggie Zhang‘: (13000000827L, ‘[email protected]‘), ‘Qiping Kong‘: (1590100007, ‘[email protected]‘), ‘Haixia Li‘: (13522000000L, [])}
>>>