Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79874
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2014-10-28 20:51 -0700 |
| References | <b80ee29c-e23a-4f11-9c65-746f7d74c347@googlegroups.com> <mailman.15276.1414547634.18130.python-list@python.org> |
| Message-ID | <9a7bada3-6a2c-49c1-9806-cbf690a350b8@googlegroups.com> (permalink) |
| Subject | Re: % symbol in python |
| From | satishmlmlml@gmail.com |
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 = """
<html>
<title>People Input Form</title>
<body>
<form method=POST action="peoplecgi.py">
<table>
<tr><th>key<td><input type=text name=key value="%(key)s">
$ROWS$
</table>
<p>
<input type=submit value="Fetch", name=action>
<input type=submit value="Update", name=action>
</form>
</body></html>
"""
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):
try:
key = form['key'].value
record = db[key]
fields = record.__dict__
fields['key'] = key
except:
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!'
else:
key = form['key'].value
if key in db:
record = db[key]
else:
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)
else:
fields = dict.fromkeys(fieldnames, '?')
fields['key'] = 'Missing or invalid action!'
db.close()
print(replyhtml % htmlize(fields))
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
% symbol in python satishmlmlml@gmail.com - 2014-10-28 14:41 -0700
Re: % symbol in python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-28 22:17 +0000
Re:% symbol in python Dave Angel <davea@davea.name> - 2014-10-28 21:57 -0400
Re: % symbol in python satishmlmlml@gmail.com - 2014-10-28 20:51 -0700
Re: % symbol in python Chris Angelico <rosuav@gmail.com> - 2014-10-29 15:02 +1100
Re: % symbol in python satishmlmlml@gmail.com - 2014-10-28 21:41 -0700
Re: % symbol in python alex23 <wuwei23@gmail.com> - 2014-10-29 15:25 +1000
Re: % symbol in python satishmlmlml@gmail.com - 2014-10-28 22:48 -0700
Re: % symbol in python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-29 08:40 +0000
csiph-web