15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

55 else:<br />

56 showForm()<br />

57<br />

58 if __name__ == '__main__':<br />

59 process()<br />

So what did we change in our script? Let's take a look at some of the blocks of code in this script.<br />

Line-by-Line Explanation<br />

Lines 15<br />

In addition to the usual startup and module import lines, we separate the HTTP MIME header from the<br />

rest of the HTML body because we will use it for both types of pages (form page and results page)<br />

returned and we don't want to duplicate the text. We will add this header string to the corresponding<br />

HTML body when it comes time for output to occur.<br />

Lines 729<br />

All of this code is related to the now-integrated friends.htm form page in our CGI script. We have a<br />

variable for the form page text, formhtml, and we also have a string to build the list of radio buttons,<br />

fradio. We could have duplicated this radio button HTML text as it is in friends.htm, but we wanted to<br />

show how we could use <strong>Python</strong> to generate more dynamic outputsee the for-loop on Lines 2227.<br />

The showForm() function has the responsibility of generating a form for user input. It builds a set of text<br />

for the radio buttons, merges those lines of HTML into the main body of formhtml, prepends the header<br />

to the form, and then returns the entire wad of data back to the client by sending the entire string to<br />

standard output.<br />

There are a couple of interesting things to note about this code. The first is the "hidden" variable in the<br />

form called action, containing the value "edit" on line 12. This field is the only way we can tell which<br />

screen to display (i.e., the form page or the results page). We will see this field come into play in Lines<br />

5356.<br />

Also, observe that we set the 0 radio button as the default by "checking" it within the loop that<br />

generates all the buttons. This will also allow us to update the layout of the radio buttons and/or their<br />

values on a single line of code (line 18) rather than over multiple lines of text. It will also offer some<br />

more flexibility in letting the logic determine which radio button is checkedsee the next update to our<br />

script, friends3.py coming up.<br />

Now you may be thinking, "Why do we need an action variable when I could just as well be checking for<br />

the presence of person or howmany?" That is a valid question because yes, you could have just used<br />

person or howmany in this situation.<br />

However, the action variable is a more conspicuous presence, insofar as its name as well as what it<br />

doesthe code is easier to understand. The person and howmany variables are used for their values while<br />

the action variable is used as a flag.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!