- #!/usr/bin/python
- '''''
- Compiler: Python 2.6
- Filename: tb.py
- Version: 1.00 @20081205
- Author: t0nsha
- (liaodunxia{at}gmail.com)
- Remark: Just a simple telephone book program
- for my one week python study.
- '''
- 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 '''''
- Usage:
- a/A --> add a new person/loop add
- l/L --> list all names/list all details
- r/S --> remove one/loop remove
- s/S --> search/loop search
- q/Q --> quit & save/quit (loop) but not save
- '''
- 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()
- #While first run, file "tb.dat" doesn't exist, this
- #will raise an IOError exception, but we igored it.
- try:
- tbDict = p.load(file(tbdat))
- except IOError:
- pass
- tbLoop()
简易Python电话本(Simple Python Telephone Book)
最新推荐文章于 2023-03-02 08:45:32 发布