简明python上尾声,作者给了读者一道题,闲着没事,整了整(还是有很多问题的,基本功能是没问题的),诸多问题有时间再来解~这次让我最大的体会还是书中那着诗:When the work is done,but if you wanna make work also fun,using python.....作个笔记,上code吧。
#!/usr/bin/python
#import sys
import cPickle as pic
'''class person to represent each contaction'''
class con_person:
#init a person basic info
def __init__(self,name,email,phone):
self.name=name
self.email=email
self.phone=phone
print 'Initialize a person for contaction succeed'
def person_info(self):
print 'Name:%s \temail:%s \tphone:%s' % (self.name,self.email,self.phone)
print '''\
*************** Command address book v1.0 *************
If it's the first time you use it,U'd better
type help option to get some tips.....
Valid options:
add,delete,search,print,exit,help
For more details,please refer to help option
*******************************************************
'''
'''Load the address list data from file'''
def load_data_from_file(data,filename):
if len(filename)==0:
print 'Error,Invalid filename'
return
f=file(filename)
data=pic.load(f)
f.close()
'''Write the address list data to file for permanent saving'''
def write_to_file(data, filename, mode):
if len(filename)==0 or mode=='r':
return
f=file(filename, mode)
pic.dump(data,f)
f.close()
'''print the info of a member'''
def print_members():
print '************** all members info *************'
for name_tag in contact_list_info:
contact_list_info.get(name_tag).person_info()
print '************ end all members info ***********'
contact_list_info={}
addr_list_info='addr_list.data'
#contact_list_info.setdefault('','')
#load initial data from exist file
#TODO....
while True:
option=raw_input('Please input your option:')
if option=='add':
#add a contact
data={'name':'','email':'','phone':''}
data['name']=raw_input("Name:")
data['email']=raw_input("Email:")
data['phone']=raw_input("Phone:")
new=con_person(data['name'],data['email'],data['phone'])
#print new
#new.person_info()
if len(contact_list_info)==0:
contact_list_info={data['name']:new}
elif data['name'] in contact_list_info:
#TODO...
print '##### Record exist,May be need to update it #####'
continue
else:
cont_tmp={data['name']:new}
contact_list_info.update(cont_tmp)
#write to file
write_to_file(contact_list_info,addr_list_info,'a')
#f=file(addr_list_info,'a')
#pic.dump(contact_list_info,f)
#f.close()
#f=file(addr_list_info)
#storedcontact=pic.load(f)
#print_members()
elif option=='delete':
print '-------------delete from addrlist-----------'
key_input=raw_input('Please input the del-member\'s name:')
find=False
for key_tmp in contact_list_info:
if key_input==key_tmp:
find=True
print 'Member %s found,See details below:' % key_input
y_not=raw_input('Are you sure to delete it ? Y/N')
if y_not=='y' or y_not=='Y':
del contact_list_info[key_tmp]
write_to_file(contact_list_info,addr_list_info,'w')
print 'Delete member %s finished.' % key_input
else:
print 'Operation cancelled by user'
break
if find == False:
print 'Delete Error,Cannot find the member from the list'
elif option=='search':
print '-------------search from addrlist-----------'
key_input=raw_input('Please input the member\'s name:')
find=False
for key_tmp in contact_list_info:
if key_input==key_tmp:
print 'Member %s found,See details below:' % key_input
contact_list_info.get(key_input).person_info()
find=True
break
if find == False:
print 'Find Error,Cannot find the member from the list'
elif option=='print':
print_members()
elif option=='help':
print '''\
********************************************************
It's a simple tip for the cmd address book,Hope it can
be helpful.
Here are the options:add,delete,search,exit
add,
For this option,you can add a member to list
delete,
For delete a member from list
search,
Search a known member from list
exit,
quit this program
print,
print the whole members info of the list
help,
some useful tips,Good luck!
********************************************************
'''
elif option=='exit':
print 'You will leave from address book,Bye!'
break
else:
print 'Unknown option,Please try again!'
continue
print 'Done'
cmd addrbook v1.0.1
#增加了初始化
#支持更新记录
#其他一些修正
#!/usr/bin/python
#import sys
import cPickle as pic
'''class person to represent each contaction'''
class con_person:
#init a person basic info
def __init__(self,name,email,phone):
self.name=name
self.email=email
self.phone=phone
print 'Initialize a person for contaction succeed'
def person_info(self):
print 'Name:%s \temail:%s \tphone:%s' % (self.name,self.email,self.phone)
print '''\
************** Command address book v1.0.1 *************
If it's the first time you use it,U'd better
type help option to get some tips.....
Valid options:
add,delete,search,print,exit,help
For more details,please refer to help option
********************************************************
'''
'''Load the address list data from file'''
def load_data_from_file(data,filename):
if len(filename)==0:
print 'Error,Invalid filename'
return
f=file(filename)
tmp_data=pic.load(f)
data.update(tmp_data)
f.close()
'''Write the address list data to file for permanent saving'''
def write_to_file(data, filename, mode):
if len(filename)==0 or mode=='r':
return
f=file(filename, mode)
pic.dump(data,f)
f.close()
'''print the info of a member'''
def print_members():
print '************* current members info ************'
for name_tag in contact_list_info:
contact_list_info.get(name_tag).person_info()
print '************ end all members info ***********'
contact_list_info={}
addr_list_info='addr_list.data'
#contact_list_info.setdefault('','')
#load initial data from exist file
print 'Initialize data from file..............'
load_data_from_file(contact_list_info,addr_list_info)
print '##########Loading data done########'
while True:
option=raw_input('Please input your option:')
if option=='add':
#add a contact
data={'name':'','email':'','phone':''}
data['name']=raw_input("Name:")
data['email']=raw_input("Email:")
data['phone']=raw_input("Phone:")
new=con_person(data['name'],data['email'],data['phone'])
#print new
#new.person_info()
if len(contact_list_info)==0:
contact_list_info={data['name']:new}
elif data['name'] in contact_list_info:
#TODO...
#print '##### Record exist,May be need to update it #####'
#new.person_info()
contact_list_info[data['name']]=new
#contact_list_info[data['name']].person_info()
write_to_file(contact_list_info,addr_list_info, 'w')
print 'Member %s info has been update.' % data['name']
continue
else:
cont_tmp={data['name']:new}
contact_list_info.update(cont_tmp)
#write to file
write_to_file(contact_list_info,addr_list_info,'a')
#f=file(addr_list_info,'a')
#pic.dump(contact_list_info,f)
#f.close()
f=file(addr_list_info)
storedcontact=pic.load(f)
print_members()
elif option=='delete':
print '-------------delete from addrlist-----------'
key_input=raw_input('Please input the del-member\'s name:')
find=False
for key_tmp in contact_list_info:
if key_input==key_tmp:
find=True
print 'Member %s found,See details below:' % key_input
y_not=raw_input('Are you sure to delete it ? Y/N')
if y_not=='y' or y_not=='Y':
del contact_list_info[key_tmp]
write_to_file(contact_list_info,addr_list_info,'w')
print 'Delete member %s finished.' % key_input
else:
print 'Operation cancelled by user'
break
if find == False:
print 'Delete Error,Cannot find the member from the list'
elif option=='search':
print '-------------search from addrlist-----------'
key_input=raw_input('Please input the member\'s name:')
find=False
for key_tmp in contact_list_info:
if key_input==key_tmp:
print 'Member %s found,See details below:' % key_input
contact_list_info.get(key_input).person_info()
find=True
break
if find == False:
print 'Find Error,Cannot find the member from the list'
elif option=='print':
print_members()
elif option=='help':
print '''\
********************************************************
It's a simple tip for the cmd address book,Hope it can
be helpful.
Here are the options:add,delete,search,exit
add,
For this option,you can add a member to list
delete,
For delete a member from list
search,
Search a known member from list
exit,
quit this program
print,
print the whole members info of the list
help,
some useful tips,Good luck!
********************************************************
'''
elif option=='exit':
print 'You will leave from address book,are u sure?Y/N'
input=raw_input('>')
if input == 'Y' or input== 'y':
print 'Exiting....Bye!'
break
else:
print 'User cancelled...'
else:
print 'Unknown option,Please try again!'
continue
print 'Done'