006 a database app
8045 ワード
use shelve
import sys,shelve
width=77
def store_person(db):
"""
Query user for data and store it in the shelf object
"""
pid=input('Enter unique ID number:')
if pid in db:
print('The id exist')
print_person(db[pid])
lookup_main(db,pid)
else:
db[pid]=input_person()
def lookup_person(db):
"""
Query user for ID and desired field. and fetch the corresponding
data from the shelf object
"""
if len(db.keys())==0:
print('The database is empty! Please store some data.')
print('You can try store command.')
return
while True:
pid=input('Enter ID number:')
if pid in db:
lookup_person_attr(db,pid)
lookup_main(db,pid)
return
print('No this key,Please try again')
def lookup_person_attr(db,pid):
"""
look up a person attributes by pid
"""
while True:
cmd=enter_lookup_attr_cmd()
if cmd=='?':
print_attr_help()
else:
fields=cmd.split(',')
if len(fields)==1:
if fields[0]=='all':
print_person(db[pid])
return
message=[]
for field in fields:
if field in db[pid]:
message.append(print_person_info(db[pid],field))
else:
message.append('No '+field+' attribute')
beauti_print('Person '+pid+'\'s Information',*message)
return
def modify_person(dp,pid):
"""
modify a person
"""
person=dp[pid]
print_person(person)
while True:
cmd=enter_modify_attr_cmd()
if cmd=='name' or cmd=='age' or cmd=='phone':
input_person_info(person,cmd)
break
elif cmd=='all':
input_person()
break
elif cmd=='?':
print_attr_help()
else:
cmds=cmd.split(",")
for cmd in cmds:
input_person_info(person,cmd)
break
dp[pid]=person
print_person(person)
def input_person():
"""
accept user input in a person object
"""
person={}
input_person_info(person,'name')
input_person_info(person,'age')
input_person_info(person,'phone')
return person
def print_person(p):
"""
print a person information
"""
message=[]
message.append(print_person_info(p,'name'))
message.append(print_person_info(p,'age'))
message.append(print_person_info(p,'phone'))
beauti_print('Person Information',*message)
def input_person_info(p,field):
p[field]=input('Enter '+field+':')
def print_person_info(p,field):
return field.capitalize()+': '+p[field]
def del_person(db):
"""
delete user for ID
"""
pid=input('Enter ID number:')
message=[]
if pid in db:
del db[pid]
message.append('Delete success')
else:
message.append('No this id!')
beauti_print('About Delete',*message)
def print_attr_help():
message=[]
message.append('You can try enter name,age,phone')
message.append('all : Prints person all information')
message.append('?(h) : Prints this message')
beauti_print('About Person',*message,flag='=')
def print_lookup_help():
message=[]
message.append('del(d) : Delete information about a person by ID number')
message.append('modify(m): Modify information about a person by ID number')
message.append('back(b) : Back to upper list')
message.append('?(h) : Prints this message')
beauti_print('The available commands are',*message,flag='+')
def print_help():
message=[]
message.append('store(s) : Store information about a person')
message.append('del(d) : Delete information about a person by ID number')
message.append('lookup(l): Looks up a person from ID number')
message.append('quit(q) : Save changes and exit')
message.append('?(h) : Prints this message')
beauti_print('The available commands are',*message)
def beauti_print(head,*message,flag='*'):
flag=flag
if len(flag)>1:
flag='*'
mess_format=flag+' '+'%-'+str(width-5)+'s'+flag
left_width=(width-len(head)-2)//2
right_width=width-len(head)-left_width-2
print(flag*left_width,head,flag*right_width)
for mess in message:
print(mess_format % mess)
print(flag*width)
def enter_modify_attr_cmd():
return base_command('What would you like to modify(? for help):')
def enter_lookup_attr_cmd():
return base_command('What would you like to know(? for help):')
def enter_lookup_cmd():
return base_command('what would you like to do about this person(? for help):')
def enter_command():
return base_command('Enter command(? for help):')
def base_command(message):
while True:
cmd=input(message)
cmd=cmd.strip().lower()
if cmd!='':
return cmd
def lookup_main(db,pid):
while True:
cmd=enter_lookup_cmd()
if cmd=='del' or cmd=='d':
del db[pid]
print('Delete success!')
break
elif cmd=='modify' or cmd=='m':
modify_person(db,pid)
break
elif cmd=='back' or cmd=='b':
break
elif cmd=='?' or cmd=='h':
print_lookup_help()
def top_main(db):
try:
while True:
cmd=enter_command()
if cmd=='store' or cmd=='s':
store_person(db)
elif cmd=='lookup' or cmd=='l':
lookup_person(db)
elif cmd=='del' or cmd=='d':
del_person(db)
elif cmd=='?' or cmd=='h':
print_help()
elif cmd=='quit' or cmd=='q':
return
finally:
db.close()
def main():
database=shelve.open(r'E:/tunie/usePython/003oop/database.dat')
top_main(database)
if __name__=='__main__':
main()