import cPickle as P
import os
class Contact: #define a class of contact person ,including name ,telnum,email
def __init__(self,name,telnum,mailaddr):
self.name=name
self.telnum=telnum
self.mailaddr=mailaddr
def display(self):
print '%s \'s telnum is %s and the addr is %s' %(self.name ,self.telnum, self.mailaddr)
p=Contact('jemmy','48844','shanghai@163.com')
p2=Contact('Tommy','77777','hangzhou@163.com')
notebook={p.name : p, # creat a original dictionary include two person
p2.name:p2}
print'the original notebook was as follows'
for name ,p3 in notebook.items():
print '%s \'s telnum is %s and the addr is %s' % (name,p3.telnum,p3.mailaddr) #$$$$$ display the dict which is
#$$$$$ consisted of a Class
if not os.path.isfile('dictnote.txt'): #if the code is to run for the first time ,then creat the diction file ,
dictfile = file('dictnote.txt','w') # copy the original two person to the file
P.dump(notebook,dictfile)
dictfile.close() #need to close the file before the opreration of (P.load)
#for test # notebook= P.load(dictfile)
# for name ,p3 in notebook.items():
# print '%s \'s telnum is %s and the addr is %s' % (name,p3.telnum,p3.mailaddr)
# dictfile.close()
else: #if the diction file has already exist ,copy the content to the notebook
dictfile=file('dictnote.txt')
notebook=P.load(dictfile)
dictfile.close()
# give user some commonds to operate the notebook
running = True
while running:
commond=raw_input('inquiry(I),displayall(P),addone(A),delone(D),modify(M),exit(E)\n')
if commond=='P':
for name ,p3 in notebook.items():
print '%s \'s telnum is %s and the addr is %s' % (name,p3.telnum,p3.mailaddr)
continue
if commond=='I':
while True:
comname=raw_input('whose information you need,press q to quit\n')
if comname in notebook:
#$$$$$$ display the content of object in the notebook
print '%s \'s telnum is %s and the addr is %s' %(comname,notebook[comname].telnum,notebook[comname].mailaddr)
break
elif comname=='q':
break
else:
print 'the name wasnt in the notebook,try again'
continue
if commond=='A':
addname=raw_input('add a person\'s name')
addtel=raw_input('add a person\'s telnum')
addaddr=raw_input('add a person\'s mailaddr')
addPer=Contact(addname,addtel,addaddr)
notebook[addPer.name]=addPer #$$$$ opreation of adding a key and a value of (Class)
if addname in notebook:
print '%s \'s information add successfully ' %addname
continue
if commond=='D':
while True:
delname=raw_input('whose information do you want to delete\n')
if delname in notebook:
break
elif delname=='q':
break
else:
print 'the name wasnt in the notebook,try again'
continue
if delname=='q':
continue
del notebook[delname] #$$$$$ delete a key
if delname not in notebook:
print '%s \'s information del successfully '%delname
continue
if commond=='M': #modify a key
while True:
modname=raw_input('whose information do you want to modify,press q to quit\n')
if modname in notebook:
break
elif modname=='q':
break
else:
print 'the name wasnt in the notebook,try again'
continue
if modname=='q':
continue
modtel=raw_input('input the new telnum')
modaddr=raw_input('input the new mailaddr')
if modname in notebook:
notebook[modname].telnum=modtel
notebook[modname].mailaddr=modaddr
print '%s \'s information modify successfully '%modname
continue
if commond=='E':
break
else:
print'It\'s an illeagal commond ,please input again'
continue
dictfile = file('dictnote.txt','w') #save the new notebook to the dictfile.txt
P.dump(notebook,dictfile)
dictfile.close()
dictfile=file('dictnote.txt') #display the content of the dictfile.txt
notebook= P.load(dictfile)
for name ,p3 in notebook.items():
print '%s \'s telnum is %s and the addr is %s' % (name,p3.telnum,p3.mailaddr)
dictfile.close()