#!/usr/bin/python
#Filename:myAddressBook.py
import os
#used cPlickle store
import cPickle as p
########class Person##############
class Person:
def __init__(self,name,email):
self.name = name
self.email = email
def __del__(self):
pass
################################
#######class AddressBook#######
class AddressBook:
#the long name of data file
addressFile = 'Address.data'
#dictionary
addressList ={}
def __init__(self):
#If the data file of addressbook is not exists,then create it.
if not os.path.isfile(AddressBook.addressFile):
f = file(AddressBook.addressFile,'w')
p.dump(AddressBook.addressList,f)
f.close()
print 'Created file successfull!'
else:
#If the data file of addressbook is exists,then list all.
f = file(AddressBook.addressFile)
AddressBook.addressList = p.load(f)
print 'Current address list:'
f.close()
self.List()#call List function
#When destroy object,save data to file.
def __del__(self):
try:
try:
f = file(AddressBook.addressFile,'w')
p.dump(AddressBook.addressList,f)
print 'Save data successfull!'
except:
print 'Save data fail!'
finally:
f.close()
#purpose:Add new record
def Add(self,pepole):
if len(pepole.name)==0:
print 'The length of name must longer than zero.'
elif len(pepole.email)==0:
print 'The length of emial must longer than zero.'
else:
pr = Person(pepole.name,pepole.email)
AddressBook.addressList[pepole.name] = pr
print 'Add new person successfull!'
#purpose:Delete a record by name
def Del(self,name):
if AddressBook.addressList.has_key(name):
del AddressBook.addressList[name]
print 'Deleted %s successfull!' % name
else:
print 'The name is not exists.'
#purpose:Modify a record by name
def Edit(self,name,people):
if AddressBook.addressList.has_key(name):
AddressBook.addressList[name] = people
print 'Edit %s\'s information successfull!' % name
else:
print 'The name is not exists.'
#purpose:Find a record by name
def Find(self,name):
if AddressBook.addressList.has_key(name):
print '%s\'s information is :\nName:%s\nEmail:%s'\
% (name,\
AddressBook.addressList[name].name,\
AddressBook.addressList[name].email)
else:
print 'The name is not exists.'
#purpose:List all record
def List(self):
print '\n' + '*'*10 + 'Address List' + '*'*10 + '\n'
for name,people in AddressBook.addressList.items():
print '%s\'s information is :\nName:%s\nEmail:%s' % (name,people.name,people.email)
print '-'*30
################main####################################
os.system('clear')
ad = AddressBook()
while True:
print '\n' + '*'*10 + 'Main menu' + '*'*10 +'''
1.Add new address
2.Delete an address by name
3.Edit an address by name
4.Find an address by name
5.List all record
6.Exit'''+\
'\n' + '*'*30
try:
choice = int(raw_input('Please choice:'))
except:
os.system('clear')
print 'Error:Please enter a number from 1 to 6,enter again.'
continue
if choice == 1:
name = raw_input('Please input name:')
email = raw_input('Please input email:')
people = Person(name,email)
ad.Add(people)
elif choice == 2:
name = raw_input('Please input name:')
ad.Del(name)
elif choice == 3:
name = raw_input('Please input name:')
newemail = raw_input('Please input new email:')
people = Person(name,newemail)
ad.Edit(name,people)
elif choice == 4:
name = raw_input('Please input name:')
ad.Find(name)
elif choice == 5:
ad.List()
elif choice == 6:
break
else:
os.system('clear')