[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Step 3: Putting It All Together—A New Reply Script


There’s one last step on our path to software maintenance nirvana: we must recode the
reply page script itself to import data that was factored out to the common module and
import the reusable form mock-up module’s tools. While we’re at it, we move code
into functions (in case we ever put things in this file that we’d like to import in another
script), and all HTML code to triple-quoted string blocks. The result is Exam-
ple 15-23. Changing HTML is generally easier when it has been isolated in single strings
like this, instead of being sprinkled throughout a program.


Example 15-23. PP4E\Internet\Web\cgi-bin\languages2reply.py


#!/usr/bin/python
"""
Same, but for easier maintenance, use HTML template strings, get the
Language table and input key from common module file, and get reusable
form field mockup utilities module for testing.
"""


import cgi, sys
from formMockup import FieldMockup # input field simulator
from languages2common import hellos, inputkey # get common table, name
debugme = False


hdrhtml = """Content-type: text/html\n


Languages

Figure 15-26. A response page with simulated inputs


Refactoring Code for Maintainability | 1199
Free download pdf