本文共 2873 字,大约阅读时间需要 9 分钟。
- ''
- import cPickle as p
- tbDict={}
- tbdat = 'tb.dat'
- class Person:
- def __init__(self, name, phone, email, address):
- self.name = name
- self.phone = phone
- self.email = email
- self.address = address
- def tbAdd(a):
- while True:
- name = raw_input('Enter a name for Add:')
- if name == 'Q':
- break
- if tbDict.has_key(name):
- print '%s already exists.' % name
- continue
- phone = raw_input('phone:')
- email = raw_input('email:')
- address = raw_input('address:')
- person = Person(name = name, /
- phone = phone, /
- email = email, /
- address = address)
- tbDict[name] = person
- print '%s added, total: %d.' % (name, len(tbDict))
- if a != 'A':
- break
- def tbRemove(r):
- while True:
- name = raw_input('Enter a name for Delete:')
- if name == 'Q':
- break
- if not tbDict.has_key(name):
- print '%s not exist.' % name
- else:
- del tbDict[name]
- print '%s removed.' % name
- if r != 'R':
- break
- def tbSearch(s):
- while True:
- name = raw_input('Enter a name for Search:')
- if name == 'Q':
- break
- if tbDict.has_key(name):
- print 'name:'.rjust(12),tbDict[name].name
- print 'phone:'.rjust(12),tbDict[name].phone
- print 'email:'.rjust(12),tbDict[name].email
- print 'address:'.rjust(12),tbDict[name].address
- else:
- print '%s not found.' % name
- if s != 'S':
- break
-
- def tbList(l):
- if l == 'l':
- for key in tbDict.keys():
- print key
- elif l == 'L':
- for name in tbDict.keys():
- print 'name:'.rjust(12),tbDict[name].name
- print 'phone:'.rjust(12),tbDict[name].phone
- print 'email:'.rjust(12),tbDict[name].email
- print 'address:'.rjust(12),tbDict[name].address
- print 'total: %d.' % len(tbDict)
- def tbModify(m):
- while True:
- name = raw_input('Enter a name for Modify:')
- if name == 'Q':
- break
- if not tbDict.has_key(name):
- print '%s not exist.' % name
- return
- phone = raw_input('new phone:')
- email = raw_input('new email:')
- address = raw_input('new address:')
- person = Person(name = name, /
- phone = phone, /
- email = email, /
- address = address)
- tbDict[name] = person
- print '%s modified.' % (name)
- if m != 'M':
- break
- def tbUsage():
- print ''
- def tbQuit(q):
- if q == 'q':
- p.dump(tbDict, file(tbdat, 'w+'))
-
- def tbLoop():
- while True:
- c = raw_input('Ready to work:')
- if c == 'a' or c == 'A':
- tbAdd(c)
- elif c == 'l' or c == 'L':
- tbList(c)
- elif c == 'm' or c == 'M':
- tbModify(c)
- elif c == 'r' or c == 'R':
- tbRemove(c)
- elif c == 's' or c == 'S':
- tbSearch(c)
- elif c == 'q' or c == 'Q':
- tbQuit(c)
- break
- else:
- tbUsage()
- try:
- tbDict = p.load(file(tbdat))
- except IOError:
- pass
- tbLoop()
转载地址:http://ywtai.baihongyu.com/