def htmlize(adict)new adict copy(for field in fieldnamesvalue new[fieldnew[fieldcgi escape(repr(value)return new def fetchrecord(dbform)trykey form['key'value record db[keyfields record __dict__ fields['key'key exceptfields dict fromkeys(fieldnames'?'fields['key''missing or invalid key!return fields values may have &>etc display as codequoted html-escape special chars use attribute dict to fill reply string def updaterecord(dbform)if not 'keyin formfields dict fromkeys(fieldnames'?'fields['key''missing key input!elsekey form['key'value if key in dbrecord db[keyupdate existing record elsefrom person import person make/store new one for key record person(name='?'age='?'evalstrings must be quoted for field in fieldnamessetattr(recordfieldeval(form[fieldvalue)db[keyrecord fields record __dict__ fields['key'key return fields db shelve open(shelvenameaction form['action'value if 'actionin form else none if action ='fetch'fields fetchrecord(dbformelif action ='update'fields updaterecord(dbformelsefields dict fromkeys(fieldnames'?'bad submit button value fields['key''missing or invalid action!db close(print(replyhtml htmlize(fields)fill reply from dict this is fairly large scriptbecause it has to handle user inputsinterface with the databaseand generate html for the reply page its behavior is fairly straightforwardthoughand similar to the gui of the prior section sneak preview