Python phone book management example
#! /Usr/bin/env python # coding = UTF-8 # manage the phone book. you can add, delete, modify, and query user information. import cPickle as p class Telephone: def _ init _ (self): ''' constructor ''' def addPeople (self, name, email, telephone ): "add user" 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): "delete user" 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): "" modify information "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 ): "get user information" "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 ): "Write file" "f = file ('dict. data ', 'w') p. dump (dictData, f) f. close () def getDictData (self): "Get file content" "fileName = 'dict. data 'try: f = file (fileName) teleDict = p. load (f) return teleDict failed t: print 'open file error' # message 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 ')