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, ‘[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, [])}
>>>

时间: 2024-10-10 01:46:45

What next? ---class, def, pickle, dictionary, make a address book的相关文章

[转]python数据持久存储:pickle模块的基本使用

python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象. 基本接口: pickle.dump(obj, file, [,protocol]) 注解:将对象obj保存到文件file中去. protocol为序列化使用的协议版本,0:ASCII协议,所序列化的对象使用可打印的ASCII码表示:1:老式的二进制协议:2:2.3版本引

python 序列化 json pickle

python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象. 基本接口: pickle.dump(obj, file, [,protocol]) 注解:将对象obj保存到文件file中去. protocol为序列化使用的协议版本,0:ASCII协议,所序列化的对象使用可打印的ASCII码表示:1:老式的二进制协议:2:2.3版本引

pickle模块的基本使用

python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象. 基本接口: pickle.dump(obj, file, [,protocol]) 注解:将对象obj保存到文件file中去. protocol为序列化使用的协议版本,0:ASCII协议,所序列化的对象使用可打印的ASCII码表示:1:老式的二进制协议:2:2.3版本引

python模块 - pickle模块

http://blog.csdn.net/pipisorry python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象. 基本接口: pickle.dump(obj, file, [,protocol]) 注解:将对象obj保存到文件file中去. protocol为序列化使用的协议版本,0:ASCII协议,所序列化的对象使用可

Python中pickle模块的使用方法详解

python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象.本文和大家分享的就是python开发中pickle模块的相关使用,一起来看看吧. 基本接口: pickle.dump(obj, file, [,protocol]) 注解:将对象obj保存到文件file中去. protocol为序列化使用的协议版本,0:ASCII协议,所

The Flat Dictionary

The Flat Dictionary 原来的代码没处理dict为空的情况 1 def flatten(dictionary): 2 #[] is a list 3 #() is a tuple 4 stack = [((), dictionary)] 5 6 result = {} #result is a dict 7 8 while stack: 9 path, current = stack.pop() #get a tuple 10 11 for k, v in current.ite

『Pickle』数据结构持久化模块_常用方法记录

可以把数据结构保存进文件并直接读出, 不论读取或者是保存,我们都需要借助open()函数,并且是二进制方式('wb','rb') json模块可以把字典结构改写为string然后保存,并可以反向读取字典,但是即使是字典数据结构,两个包也是有差别的 json字典value不支持其他对象只支持python原有的结构,但是json由于是转换为string,所以保存的文件是可以使用文本查看器去读取的 pickle包则支持各种python的对象,但它写入的是二进制文件,并有自己独特的编码方式,所以是不可以

python中的pickle模块

python的pickle模块实现了基本的数据序列和反序列化. 通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储. 通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象. 对象——>文件 import pickle # 使用pickle模块将数据对象保存到文件 data1 = {'a': [1, 2.0, 3, 4+6j], 'b': ('string', u'Unicode string'), 'c': None} selfref_

python3_pickle模块详解

python3 pickle持久化的储存数据. python程序运行中得到了一些字符串,列表,字典等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就丢失数据.python模块大全中pickle模块就排上用场了, 他可以将对象转换为一种可以传输或存储的格式. pickle对象串行化 pickle模块将任意一个python对象转换成一系统字节的这个操作过程叫做串行化对象: pickle与cpickle比较 pickle完全用python来实现的,cpickle用C来实现的,c