#peopleinteract_query
import shelve
fieldnames=('name', 'age','job','pay')
maxfield=max(len(f) for f in fieldnames)
db=shelve.open('class-shelve')
while True:
key=input('\nKey?=>')
if not key: break
try:
record=db[key]
except:
print('No such key %s!' % key)
else:
for field in fieldnames:
print(field.ljust(maxfield),'=>', getattr(record, field))
#record is class, so we cannot use record[field]); getattr, extracts the content of class
#peopleinteract_update
#interactive updates
import shelve
from person import Person
fieldnames=('name', 'age', 'job','pay')
db=shelve.open('class-shelve')
while True:
key=input('\nKey? =>')
if not key: break
if key in db:
record=db[key]
else:
record=Person(name='?', age='?')
for field in fieldnames:
curval=getattr(record, field)
next=input('\t[%s]=%s\n\tnew?=>' % (field, curval))
if next:
setattr(record, field, eval(next))
#class, assign values
db[key]=record
db.close()