Lecture 61:

posted May 21, 2013, 6:14 AM by Samuel Konstantinovich   [ updated May 21, 2013, 7:11 AM ]
Demo1:  Self referencing forms, and multiple buttons.

print 'content-type: text/html'
#Content types are case-insensitive.

import cgi,cgitb
form = cgi.FieldStorage()

<head><title>self reference!</title></head>

if len(form)==0:
    page+='''<form action="selfreference.py" method="get">
text:<input type="text" name="textfield1" value="default string here">
<input type="submit" name="submitbutton1">
<input type="submit" name="submitbutton2">
<input type="submit" value="click">
    page+='notice the default value for submit buttons are "Submit", but you can have different '
    page+='names or different values to identify which button was pressed.<br> also notice that '
    page+='a submit button with no name does not show up in the field storage.'
print page

Demo 2:  Printing a list in order...

Use the sorted command:

Pay attention to how to sort in different ways:
>>> students = [
        ['john', 'A', 15],
        ['jane', 'B', 12],
        ['dave', 'B', 10],
>>> sorted(students, key=lambda f: f[2])   # sort by age
[['dave', 'B', 10], ['jane', 'B', 12], ['john', 'A', 15]]
key = lambda f: f[2] is defining what must be done to each element f of the list students. f is a function name being defined (remember lambda in scheme)    This says sort the list of lists, by the value at index 2.

1: you could compare a regular list of strings: ['abby', 'dave','harry','amy']
key=lambda f: f.upper()    #ignore case
key=lambda f: f.upper().strip(' ')  #ignore case and strip spaces

2: You could compare lists of lists, where the inner lists have strings:  [ ['123', 'bob','30'], ['125', 'baby','20'], ['113', 'amy','10'] ]
A: to ignore case:
f: f[1].upper()    #sort the list of lists, by the value at index 1, and make them upper before you try sorting

B. or list of string like numbers:
f: int(f[3])     #sort the list of lists by the value at index 3, but convert them to integers before you compare