#!/usr/bin/env python
#coding=utf-8
#电话本管理,可以进行添加,删除,修改,查询用户信息
import cpickle as p
class telephone:
def __init__(self):
”’构造方法 ”’
def addpeople(self,name,email,telephone):
“””添加用户”””
teledict = self.getdictdata()
if teledict:
infolist = [name,email,telephone]
teledict[name] = infolist
self.writefile(teledict)
else:
teledict = {}
infolist = [name,email,telephone]
teledict[name] = infolist
self.writefile(teledict)
def delpeople(self,name):
“””删除用户”””
teledict = self.getdictdata()
if name in teledict.keys():
del teledict[name]
self.writefile(teledict)
else:
print name,’is not in dict’
def editpeople(self,name,emial,telephone):
“”” 修改信息”””
teledict = self.getdictdata()
if name in teledict.keys():
infolist = [name,email,telephone]
teledict[name] = infolist
self.writefile(teledict)
print name+’edit success’
else:
print name,’is not in dict’
def getpeople(self,name):
“””获取用户信息”””
teledict = self.getdictdata()
if teledict:
if name in teledict.keys():
people = teledict[name]
print people
else:
print name,’is not in dict’
else:
print ‘people is empty’
def writefile(self, dictdata):
“”” 写入文件”””
f = file(‘dict.data’,’w’)
p.dump(dictdata,f)
f.close()
def getdictdata(self):
“”” 获取文件内容”””
filename = ‘dict.data’
try:
f = file(filename)
teledict = p.load(f)
return teledict
except:
print ‘open file error’
# 提示 信息
def notice():
print “please enter 1-get people 2-add people 3-edit pelole 4-del people 5-get all people 0-break”
if __name__ == “__main__”:
while(true):
notice()
userinput = int(raw_input())
people = telephone()
if userinput == 1:
name = raw_input(“please enter user name:”)
people.getpeople(name)
elif userinput == 2:
name = raw_input(“enter name:”)
email = raw_input(“enter emai:”)
telephone = raw_input(“enter telephone:”)
people.addpeople(name,email,telephone)
elif userinput == 3:
name = raw_input(“enter name:”)
email = raw_input(“enter emai:”)
telephone = raw_input(“enter telephone:”)
people.editpeople(name,email,telephone)
elif userinput == 4:
name = raw_input(“enter del people name:”)
people.delpeople(name)
elif userinput == 5:
allpeople = people.getdictdata()
if allpeople:
for key in allpeople:
print key,allpeople[key]
else:
print ‘there is no people’
elif userinput == 0:
break
else:
print ‘you select number is wrong’
raw_input(‘press enter’)