| #! /Usr/bin/python Import sys, shelve Def store_person (db ): """ Query user for data and store it in the shelf object """ Pid = raw_input ('enter unique ID number :') Person = {} Person ['name'] = raw_input ('enter name :') Person ['age'] = raw_input ('enter age :') Person ['phone'] = raw_input ('enter phone number :') Db [pid] = person Def lookup_person (db ): """ Query user for ID and desired field, And fetch the corresponding data From the shelf object """ Pid = raw_input ('enter unique ID number :') Temp = db [pid] Field = raw_input ('Please enter name, age or phone :') Field. strip (). lower () Print field. capitalize () + ':', temp [field] Def print_help (): Print 'the avaliable commands are :' Print 'store: Stores infomation about a person' Print 'lookup: Looks up a person form ID number' Print 'Quit: Save changes and exit' Print '? : Prints this message' Def enter_command (): Cmd = raw_input ('enter command (? For help ):') Cmd = cmd. strip (). lower () Return cmd Def main (): Database = shelve. open ('database ') # Database stores in current directory Try: While True: Cmd = enter_command () If cmd = 'store ': Store_person (database) Elif cmd = 'lookup ': Lookup_person (database) Elif cmd = '? ': Print_help () Elif cmd = 'quit ': Return Finally: Database. close () # Close database in any condition If _ name _ = '_ main __': Main () |