% symbol in python

satishmlmlml at gmail.com satishmlmlml at gmail.com
Wed Oct 29 05:41:29 CET 2014

import cgi, shelve, sys, os 
shelvename = 'class-shelve' 
fieldnames = ('name', 'age', 'job', 'pay') 
form = cgi.FieldStorage() 
print('Content-type: text/html') 
sys.path.insert(0, os.getcwd()) 
replyhtml = """ 
<title>People Input Form</title> 
<form method=POST action="peoplecgi.py"> 
 <tr><th>key<td><input type=text name=key value="%(key)s"> 
 <input type=submit value="Fetch", name=action> 
 <input type=submit value="Update", name=action> 
rowhtml = '<tr><th>%s<td><input type=text name=%s value="%%(%s)s">\n' 
rowshtml = '' 
for fieldname in fieldnames: 
 rowshtml += (rowhtml % ((fieldname, ) * 3)) 
 replyhtml = replyhtml.replace('$ROWS$', rowshtml) 
def htmlize(adict): 
 new = adict.copy() 
 for field in fieldnames: 
  value = new[field] 
  new[field] = cgi.escape(repr(value)) 
 return new 

def fetchRecord(db, form): 
  key = form['key'].value 
  record = db[key] 
  fields = record.__dict__ 
  fields['key'] = key 
  fields = dict.fromkeys(fieldnames, '?') 
  fields['key'] = 'Missing or invalid key!' 
 return fields 
def updateRecord(db, form): 
 if not 'key' in form: 
  fields = dict.fromkeys(fieldnames, '?') 
  fields['key'] = 'Missing key input!' 
  key = form['key'].value 
  if key in db: 
   record = db[key] 
   from person import Person 
   record = Person(name='?', age='?') 
 for field in fieldnames: 
   setattr(record, field, eval(form[field].value)) 
  db[key] = record 
  fields = record.__dict__ 
  fields['key'] = key 
 return fields 
db  =  shelve.open(shelvename) 
action = form['action'].value if 'action' in form else None 
if action == 'Fetch': 
 fields = fetchRecord(db, form) 
elif action == 'Update': 
 fields = updateRecord(db, form) 
 fields = dict.fromkeys(fieldnames, '?') 
 fields['key'] = 'Missing or invalid action!' 
print(replyhtml % htmlize(fields)) 

kindly let me know what is $ROWS$ along with % symbol's meaning

More information about the Python-list mailing list