[Tutor] Help with a cgi example

Lloyd Kvam pythontutor at venix.com
Sat Aug 23 14:33:54 EDT 2003


Justin Heath wrote:
> All,
> 
> I am currently working thru some of the cgi examples in "Programming
> Python". I am stuck on how one of the examples works. Here is the example:
>        if type(form[field]) != type([]):
>            data[field] = form[field].value
>        else:
>            values = map(lambda x: x.value, form[field])
>            data[field] = string.join(values, ' and ')
> print html % data
> 
It is possible for multiple values to be supplied for a field.  In that
case form.field will return a list of FieldStorage (or MiniFieldStorage)
instances.  The funny looking code takes the list of FieldStoage instances
and extracts the values which are strings.  The lambda expression simply
calls the FieldStorage instance's value method to get the string that
was entered through the HTML form.  Map applies that value call to each
of the FieldStorage instances in the form[field] list.  We are
transforming a list of FieldStorage instances into a list of strings.
The join expression that conbines these strings could have been written:
	' and '.join(values)
The resulting string is then assigned into the dictionary named data using
field as the key.


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582




More information about the Tutor mailing list