diff --git a/dip2 b/dip2 index 0f4549b..933204f 100644 --- a/dip2 +++ b/dip2 @@ -5,10 +5,10 @@

Dive Into Python

-

20 May 2004 -

20 May 2004 +

This book lives at http://diveintopython3.org/. If you're reading it somewhere else, you may not have the latest version. -

+

Table of Contents

- - -
+

Chapter 1. Installing Python

Welcome to Python. Let's dive in. In this chapter, you'll install the version of Python that's right for you.

1.1. Which Python is right for you?

@@ -304,7 +301,7 @@ of other platforms you've probably never even heard of.

ActivePython is freely downloadable, although it is not open source. It is the IDE I used to learn Python, and I recommend you try it unless you have a specific reason not to. One such reason might be that ActiveState is generally several months behind in updating their ActivePython installer when new version of Python are released. If you absolutely need the latest version of Python and ActivePython is still a version behind as you read this, you'll want to use the second option for installing Python on Windows.

The second option is the “official” Python installer, distributed by the people who develop Python itself. It is freely downloadable and open source, and it is always current with the latest version of Python. -

+

Procedure 1.1. Option 1: Installing ActivePython

Here is the procedure for installing ActivePython:

    @@ -328,12 +325,12 @@ several months behind in updating their ActivePython installer when new version

    After the installation is complete, close the installer and choose Start->Programs->ActiveState ActivePython 2.2->PythonWin IDE. You'll see something like the following:

-
-PythonWin 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32.
+
+PythonWin 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32.
 Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) -
 see 'Help/About PythonWin' for further copyright information.
->>> 
-
+>>> +

Procedure 1.2. Option 2: Installing Python from Python.org

  1. @@ -355,8 +352,8 @@ see 'Help/About PythonWin' for further copyright information.

    After the installation is complete, close the installer and select Start->Programs->Python 2.3->IDLE (Python GUI). You'll see something like the following:

-
-Python 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
+
+Python 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
 Type "copyright", "credits" or "license()" for more information.
 
     ****************************************************************
@@ -367,14 +364,14 @@ Type "copyright", "credits" or "license()" for more information.
     ****************************************************************
     
 IDLE 1.0
->>> 
+>>> 
 

1.3. Python on Mac OS X

On Mac OS X, you have two choices for installing Python: install it, or don't install it. You probably want to install it.

Mac OS X 10.2 and later comes with a command-line version of Python preinstalled. If you are comfortable with the command line, you can use this version for the first third of the book. However, the preinstalled version does not come with an XML parser, so when you get to the XML chapter, you'll need to install the full version.

Rather than using the preinstalled version, you'll probably want to install the latest version, which also comes with a graphical interactive shell. -

+

Procedure 1.3. Running the Preinstalled Version of Python on Mac OS X

To use the preinstalled version of Python, follow these steps:

    @@ -392,15 +389,15 @@ interactive shell.

Try it out: -

+
 Welcome to Darwin!
-[localhost:~] you% python
-Python 2.2 (#1, 07/14/02, 23:25:09)
+[localhost:~] you% python
+Python 2.2 (#1, 07/14/02, 23:25:09)
 [GCC Apple cpp-precomp 6.14] on darwin
 Type "help", "copyright", "credits", or "license" for more information.
->>> [press Ctrl+D to get back to the command prompt]
-[localhost:~] you% 
-
+>>> [press Ctrl+D to get back to the command prompt] +[localhost:~] you% +

Procedure 1.4. Installing the Latest Version of Python on Mac OS X

Follow these steps to download and install the latest version of Python:

    @@ -430,30 +427,30 @@ Type "help", "copyright", "credits", or "license" for more information.

The MacPython IDE should display a splash screen, then take you to the interactive shell. If the interactive shell does not appear, select -Window->Python Interactive (Cmd-0). The opening window will look something like this: -

-Python 2.3 (#2, Jul 30 2003, 11:45:28)
+Window->Python Interactive (Cmd-0). The opening window will look something like this:
+
+Python 2.3 (#2, Jul 30 2003, 11:45:28)
 [GCC 3.1 20020420 (prerelease)]
 Type "copyright", "credits" or "license" for more information.
 MacPython IDE 1.0.1
->>> 
+>>> 
 

Note that once you install the latest version, the pre-installed version is still present. If you are running scripts from the command line, you need to be aware which version of Python you are using. -

Example 1.1. Two versions of Python

-[localhost:~] you% python
-Python 2.2 (#1, 07/14/02, 23:25:09)
+

Example 1.1. Two versions of Python

+[localhost:~] you% python
+Python 2.2 (#1, 07/14/02, 23:25:09)
 [GCC Apple cpp-precomp 6.14] on darwin
 Type "help", "copyright", "credits", or "license" for more information.
->>> [press Ctrl+D to get back to the command prompt]
-[localhost:~] you% /usr/local/bin/python
-Python 2.3 (#2, Jul 30 2003, 11:45:28)
+>>> [press Ctrl+D to get back to the command prompt]
+[localhost:~] you% /usr/local/bin/python
+Python 2.3 (#2, Jul 30 2003, 11:45:28)
 [GCC 3.1 20020420 (prerelease)] on darwin
 Type "help", "copyright", "credits", or "license" for more information.
->>> [press Ctrl+D to get back to the command prompt]
-[localhost:~] you% 
+>>> [press Ctrl+D to get back to the command prompt]
+[localhost:~] you% 
 

1.4. Python on Mac OS 9

Mac OS 9 does not come with any version of Python, but installation is very simple, and there is only one choice. -

+

Follow these steps to install Python on Mac OS 9:

  1. @@ -479,69 +476,53 @@ Type "help", "copyright", "credits", or "license" for more information.

The MacPython IDE should display a splash screen, and then take you to the interactive shell. If the interactive shell does not appear, select -Window->Python Interactive (Cmd-0). You'll see a screen like this: -

-Python 2.3 (#2, Jul 30 2003, 11:45:28)
+Window->Python Interactive (Cmd-0). You'll see a screen like this:
+
+Python 2.3 (#2, Jul 30 2003, 11:45:28)
 [GCC 3.1 20020420 (prerelease)]
 Type "copyright", "credits" or "license" for more information.
 MacPython IDE 1.0.1
->>> 
+>>> 
 

1.5. Python on RedHat Linux

Installing under UNIX-compatible operating systems such as Linux is easy if you're willing to install a binary package. Pre-built binary packages are available for most popular Linux distributions. Or you can always compile from source.

Download the latest Python RPM by going to http://www.python.org/ftp/python/ and selecting the highest version number listed, then selecting the rpms/ directory within that. Then download the RPM with the highest version number. You can install it with the rpm command, as shown here: -

Example 1.2. Installing on RedHat Linux 9

-localhost:~$ su -
-Password: [enter your root password]
-[root@localhost root]# wget http://python.org/ftp/python/2.3/rpms/redhat-9/python2.3-2.3-5pydotorg.i386.rpm
-Resolving python.org... done.
+

Example 1.2. Installing on RedHat Linux 9

+localhost:~$ su -
+Password: [enter your root password]
+[root@localhost root]# wget http://python.org/ftp/python/2.3/rpms/redhat-9/python2.3-2.3-5pydotorg.i386.rpm
+Resolving python.org... done.
 Connecting to python.org[194.109.137.226]:80... connected.
 HTTP request sent, awaiting response... 200 OK
 Length: 7,495,111 [application/octet-stream]
 ...
-[root@localhost root]# rpm -Uvh python2.3-2.3-5pydotorg.i386.rpm
-Preparing...                ########################################### [100%]
+[root@localhost root]# rpm -Uvh python2.3-2.3-5pydotorg.i386.rpm
+Preparing...                ########################################### [100%]
    1:python2.3              ########################################### [100%]
-[root@localhost root]# python          1
-Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
+[root@localhost root]# python          
+Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
 [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
 Type "help", "copyright", "credits", or "license" for more information.
->>> [press Ctrl+D to exit]
-[root@localhost root]# python2.3       2
-Python 2.3 (#1, Sep 12 2003, 10:53:56)
+>>> [press Ctrl+D to exit]
+[root@localhost root]# python2.3       
+Python 2.3 (#1, Sep 12 2003, 10:53:56)
 [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
 Type "help", "copyright", "credits", or "license" for more information.
->>> [press Ctrl+D to exit]
-[root@localhost root]# which python2.3 3
+>>> [press Ctrl+D to exit]
+[root@localhost root]# which python2.3 
 /usr/bin/python2.3
-
- - - - - - - - - - - - - -
1 -Whoops! Just typing python gives you the older version of Python -- the one that was installed by default. That's not the one you want. -
2 -At the time of this writing, the newest version is called python2.3. You'll probably want to change the path on the first line of the sample scripts to point to the newer version. -
3 -This is the complete path of the newer version of Python that you just installed. Use this on the #! line (the first line of each script) to ensure that scripts are running under the latest version of Python, and be sure to type python2.3 to get into the interactive shell. -
+
+
    +
  1. Whoops! Just typing python gives you the older version of Python -- the one that was installed by default. That's not the one you want. +
  2. At the time of this writing, the newest version is called python2.3. You'll probably want to change the path on the first line of the sample scripts to point to the newer version. +
  3. This is the complete path of the newer version of Python that you just installed. Use this on the #! line (the first line of each script) to ensure that scripts are running under the latest version of Python, and be sure to type python2.3 to get into the interactive shell.

    1.6. Python on Debian GNU/Linux

    If you are lucky enough to be running Debian GNU/Linux, you install Python through the apt command. -

    Example 1.3. Installing on Debian GNU/Linux

    -localhost:~$ su -
    -Password: [enter your root password]
    -localhost:~# apt-get install python
    -Reading Package Lists... Done
    +

    Example 1.3. Installing on Debian GNU/Linux

    +localhost:~$ su -
    +Password: [enter your root password]
    +localhost:~# apt-get install python
    +Reading Package Lists... Done
     Building Dependency Tree... Done
     The following extra packages will be installed:
       python2.3
    @@ -552,8 +533,8 @@ The following NEW packages will be installed:
     0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
     Need to get 0B/2880kB of archives.
     After unpacking 9351kB of additional disk space will be used.
    -Do you want to continue? [Y/n] Y
    -Selecting previously deselected package python2.3.
    +Do you want to continue? [Y/n] Y
    +Selecting previously deselected package python2.3.
     (Reading database ... 22848 files and directories currently installed.)
     Unpacking python2.3 (from .../python2.3_2.3.1-1_i386.deb) ...
     Selecting previously deselected package python.
    @@ -562,94 +543,78 @@ Setting up python (2.3.1-1) ...
     Setting up python2.3 (2.3.1-1) ...
     Compiling python modules in /usr/lib/python2.3 ...
     Compiling optimized python modules in /usr/lib/python2.3 ...
    -localhost:~# exit
    +localhost:~# exit
     logout
    -localhost:~$ python
    -Python 2.3.1 (#2, Sep 24 2003, 11:39:14)
    +localhost:~$ python
    +Python 2.3.1 (#2, Sep 24 2003, 11:39:14)
     [GCC 3.3.2 20030908 (Debian prerelease)] on linux2
     Type "help", "copyright", "credits" or "license" for more information.
    ->>> [press Ctrl+D to exit]
    +>>> [press Ctrl+D to exit]
     

    1.7. Python Installation from Source

    If you prefer to build from source, you can download the Python source code from http://www.python.org/ftp/python/. Select the highest version number listed, download the .tgz file), and then do the usual configure, make, make install dance. -

    Example 1.4. Installing from source

    -localhost:~$ su -
    -Password: [enter your root password]
    -localhost:~# wget http://www.python.org/ftp/python/2.3/Python-2.3.tgz
    -Resolving www.python.org... done.
    +

    Example 1.4. Installing from source

    +localhost:~$ su -
    +Password: [enter your root password]
    +localhost:~# wget http://www.python.org/ftp/python/2.3/Python-2.3.tgz
    +Resolving www.python.org... done.
     Connecting to www.python.org[194.109.137.226]:80... connected.
     HTTP request sent, awaiting response... 200 OK
     Length: 8,436,880 [application/x-tar]
     ...
    -localhost:~# tar xfz Python-2.3.tgz
    -localhost:~# cd Python-2.3
    -localhost:~/Python-2.3# ./configure
    -checking MACHDEP... linux2
    +localhost:~# tar xfz Python-2.3.tgz
    +localhost:~# cd Python-2.3
    +localhost:~/Python-2.3# ./configure
    +checking MACHDEP... linux2
     checking EXTRAPLATDIR...
     checking for --without-gcc... no
     ...
    -localhost:~/Python-2.3# make
    -gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
    +localhost:~/Python-2.3# make
    +gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
     -I. -I./Include  -DPy_BUILD_CORE -o Modules/python.o Modules/python.c
     gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
     -I. -I./Include  -DPy_BUILD_CORE -o Parser/acceler.o Parser/acceler.c
     gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
     -I. -I./Include  -DPy_BUILD_CORE -o Parser/grammar1.o Parser/grammar1.c
     ...
    -localhost:~/Python-2.3# make install
    -/usr/bin/install -c python /usr/local/bin/python2.3
    +localhost:~/Python-2.3# make install
    +/usr/bin/install -c python /usr/local/bin/python2.3
     ...
    -localhost:~/Python-2.3# exit
    +localhost:~/Python-2.3# exit
     logout
    -localhost:~$ which python
    +localhost:~$ which python
     /usr/local/bin/python
    -localhost:~$ python
    -Python 2.3.1 (#2, Sep 24 2003, 11:39:14)
    +localhost:~$ python
    +Python 2.3.1 (#2, Sep 24 2003, 11:39:14)
     [GCC 3.3.2 20030908 (Debian prerelease)] on linux2
     Type "help", "copyright", "credits" or "license" for more information.
    ->>> [press Ctrl+D to get back to the command prompt]
    -localhost:~$ 
    +>>> [press Ctrl+D to get back to the command prompt]
    +localhost:~$ 
     

    1.8. The Interactive Shell

    Now that you have Python installed, what's this interactive shell thing you're running?

    It's like this: Python leads a double life. It's an interpreter for scripts that you can run from the command line or run like applications, by double-clicking the scripts. But it's also an interactive shell that can evaluate arbitrary statements and expressions. This is extremely useful for debugging, quick hacking, and testing. I even know some people who use the Python interactive shell in lieu of a calculator!

    Launch the Python interactive shell in whatever way works on your platform, and let's dive in with the steps shown here: -

    Example 1.5. First Steps in the Interactive Shell

    ->>> 1 + 1               1
    +

    Example 1.5. First Steps in the Interactive Shell

    +>>> 1 + 1               
     2
    ->>> print 'hello world' 2
    +>>> print 'hello world' 
     hello world
    ->>> x = 1               3
    ->>> y = 2
    ->>> x + y
    +>>> x = 1               
    +>>> y = 2
    +>>> x + y
     3
    -
    - - - - - - - - - - - - - -
    1 -The Python interactive shell can evaluate arbitrary Python expressions, including any basic arithmetic expression. -
    2 -The interactive shell can execute arbitrary Python statements, including the print statement. -
    3 -You can also assign values to variables, and the values will be remembered as long as the shell is open (but not any longer +
    +
      +
    1. The Python interactive shell can evaluate arbitrary Python expressions, including any basic arithmetic expression. +
    2. The interactive shell can execute arbitrary Python statements, including the print statement. +
    3. You can also assign values to variables, and the values will be remembered as long as the shell is open (but not any longer than that). -

    1.9. Summary

    You should now have a version of Python installed that works for you.

    Depending on your platform, you may have more than one version of Python intsalled. If so, you need to be aware of your paths. If simply typing python on the command line doesn't run the version of Python that you want to use, you may need to enter the full pathname of your preferred version.

    Congratulations, and welcome to Python. -

    +

    Chapter 2. Your First Python Program

    You know how other books go on and on about programming fundamentals and finally work up to building a complete, working program? Let's skip all that. @@ -657,8 +622,9 @@ Let's skip all that.

    Here is a complete, working Python program.

    It probably makes absolutely no sense to you. Don't worry about that, because you're going to dissect it line by line. But read through it first and see what, if anything, you can make of it. -

    Example 2.1. odbchelper.py

    -

    If you have not already done so, you can download this and other examples used in this book.

    +

    Example 2.1. odbchelper.py

    +

    If you have not already done so, you can download this and other examples used in this book. +

    
     def buildConnectionString(params):
         """Build a connection string from a dictionary of parameters.
     
    @@ -671,60 +637,35 @@ if __name__ == "__main__":
                     "uid":"sa", \
                     "pwd":"secret" \
                     }
    -    print buildConnectionString(myParams)

    Now run this program and see what happens. - - - - - - -
    Tip
    In the ActivePython IDE on Windows, you can run the Python program you're editing by choosing -File->Run... (Ctrl-R). Output is displayed in the interactive window. -
    - - - - - - -
    Tip
    In the Python IDE on Mac OS, you can run a Python program with -Python->Run window... (Cmd-R), but there is an important option you must set first. Open the .py file in the IDE, pop up the options menu by clicking the black triangle in the upper-right corner of the window, and make sure the Run as __main__ option is checked. This is a per-file setting, but you'll only need to do it once per file. -
    - - - - - - -
    Tip
    On UNIX-compatible systems (including Mac OS X), you can run a Python program from the command line: python odbchelper.py
    -

    The id="odbchelper.output" output of odbchelper.py will look like this:

    server=mpilgrim;uid=sa;database=master;pwd=secret

    2.2. Declaring Functions

    + print buildConnectionString(myParams)

    Now run this program and see what happens. + + +
    TipIn the ActivePython IDE on Windows, you can run the Python program you're editing by choosing +File->Run... (Ctrl-R). Output is displayed in the interactive window. + + +
    TipIn the Python IDE on Mac OS, you can run a Python program with +Python->Run window... (Cmd-R), but there is an important option you must set first. Open the .py file in the IDE, pop up the options menu by clicking the black triangle in the upper-right corner of the window, and make sure the Run as __main__ option is checked. This is a per-file setting, but you'll only need to do it once per file. + + +
    TipOn UNIX-compatible systems (including Mac OS X), you can run a Python program from the command line: python odbchelper.py

    The id="odbchelper.output" output of odbchelper.py will look like this:

    server=mpilgrim;uid=sa;database=master;pwd=secret

    2.2. Declaring Functions

    Python has functions like most other languages, but it does not have separate header files like C++ or interface/implementation sections like Pascal. When you need a function, just declare it, like this: -

    +
    
     def buildConnectionString(params):

    Note that the keyword def starts the function declaration, followed by the function name, followed by the arguments in parentheses. Multiple arguments (not shown here) are separated with commas.

    Also note that the function doesn't define a return datatype. Python functions do not specify the datatype of their return value; they don't even specify whether or not they return a value. -In fact, every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, the Python null value. - - - - - - -
    Note
    In Visual Basic, functions (that return a value) start with function, and subroutines (that do not return a value) start with sub. There are no subroutines in Python. Everything is a function, all functions return a value (even if it's None), and all functions start with def. -
    -

    The argument, params, doesn't specify a datatype. In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally. - - - - -
    Note
    In Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument. +In fact, every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, the Python null value. + + +
    NoteIn Visual Basic, functions (that return a value) start with function, and subroutines (that do not return a value) start with sub. There are no subroutines in Python. Everything is a function, all functions return a value (even if it's None), and all functions start with def. +

    The argument, params, doesn't specify a datatype. In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally. + + + - -
    NoteIn Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument. In Python, you never explicitly specify the datatype of anything. Based on what value you assign, Python keeps track of the datatype internally. -

    2.2.1. How Python's Datatypes Compare to Other Programming Languages

    An erudite reader sent me this explanation of how Python compares to other programming languages: -

    +
    statically typed language
    A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare @@ -743,32 +684,21 @@ In fact, every Python function returns a value; if the function ever executes a

    So Python is both dynamically typed (because it doesn't use explicit datatype declarations) and strongly typed (because once a variable has a datatype, it actually matters).

    2.3. Documenting Functions

    You can document a Python function by giving it a docstring. -

    Example 2.2. Defining the buildConnectionString Function's docstring

    +

    Example 2.2. Defining the buildConnectionString Function's docstring

    
     def buildConnectionString(params):
         """Build a connection string from a dictionary of parameters.
     
         Returns string."""

    Triple quotes signify a multi-line string. Everything between the start and end quotes is part of a single string, including carriage returns and other quote characters. You can use them anywhere, but you'll see them most often used when defining a docstring. -

    - - - - - - -
    Note
    Triple quotes are also an easy way to define a string with both single and double quotes, like qq/.../ in Perl. -
    + + +
    NoteTriple quotes are also an easy way to define a string with both single and double quotes, like qq/.../ in Perl.

    Everything between the triple quotes is the function's docstring, which documents what the function does. A docstring, if it exists, must be the first thing defined in a function (that is, the first thing after the colon). You don't technically -need to give your function a docstring, but you always should. I know you've heard this in every programming class you've ever taken, but Python gives you an added incentive: the docstring is available at runtime as an attribute of the function. - - - - - - -
    Note
    Many Python IDEs use the docstring to provide context-sensitive documentation, so that when you type a function name, its docstring appears as a tooltip. This can be incredibly helpful, but it's only as good as the docstrings you write. -
    +need to give your function a docstring, but you always should. I know you've heard this in every programming class you've ever taken, but Python gives you an added incentive: the docstring is available at runtime as an attribute of the function. + + +
    NoteMany Python IDEs use the docstring to provide context-sensitive documentation, so that when you type a function name, its docstring appears as a tooltip. This can be incredibly helpful, but it's only as good as the docstrings you write. @@ -778,411 +708,252 @@ need to give your function a docstring, but you always should. I kn

    2.6. Testing Modules

    Python modules are objects and have several useful attributes. You can use this to easily test your modules as you write them. Here's an example that uses the if __name__ trick. -

    -if __name__ == "__main__":

    Some quick observations before you get to the good stuff. First, parentheses are not required around the if expression. Second, the if statement ends with a colon, and is followed by indented code. - - - - - - -
    Note
    Like C, Python uses == for comparison and = for assignment. Unlike C, Python does not support in-line assignment, so there's no chance of accidentally assigning the value you thought you were comparing. -
    +

    +if __name__ == "__main__":

    Some quick observations before you get to the good stuff. First, parentheses are not required around the if expression. Second, the if statement ends with a colon, and is followed by indented code. + + +
    NoteLike C, Python uses == for comparison and = for assignment. Unlike C, Python does not support in-line assignment, so there's no chance of accidentally assigning the value you thought you were comparing.

    So why is this particular if statement a trick? Modules are objects, and all modules have a built-in attribute __name__. A module's __name__ depends on how you're using the module. If you import the module, then __name__ is the module's filename, without a directory path or file extension. But you can also run the module directly as a standalone program, in which case __name__ will be a special default value, __main__. -

    >>> import odbchelper
    ->>> odbchelper.__name__
    +
    >>> import odbchelper
    +>>> odbchelper.__name__
     'odbchelper'

    Knowing this, you can design a test suite for your module within the module itself by putting it in this if statement. When you run the module directly, __name__ is __main__, so the test suite executes. When you import the module, __name__ is something else, so the test suite is ignored. This makes it easier to develop and debug new modules before integrating -them into a larger program. - - - - -
    Tip
    On MacPython, there is an additional step to make the if __name__ trick work. Pop up the module's options menu by clicking the black triangle in the upper-right corner of the window, and +them into a larger program. + + + - -
    TipOn MacPython, there is an additional step to make the if __name__ trick work. Pop up the module's options menu by clicking the black triangle in the upper-right corner of the window, and make sure Run as __main__ is checked. -
    -
    +

    Further Reading on Importing Modules

    -
    +

    Chapter 3. Native Datatypes

    3.2. Introducing Lists

    3.4. Declaring variables

    Now that you know something about dictionaries, tuples, and lists (oh my!), let's get back to the sample program from Chapter 2, odbchelper.py.

    Python has local and global variables like most other languages, but it has no explicit variable declarations. Variables spring into existence by being assigned a value, and they are automatically destroyed when they go out of scope. -

    Example 3.17. Defining the myParams Variable

    +

    Example 3.17. Defining the myParams Variable

    
     if __name__ == "__main__":
         myParams = {"server":"mpilgrim", \
                     "database":"master", \
                     "uid":"sa", \
                     "pwd":"secret" \
                     }

    Notice the indentation. An if statement is a code block and needs to be indented just like a function. -

    Also notice that the variable assignment is one command split over several lines, with a backslash (“\”) serving as a line-continuation marker. - - - - - - -
    Note
    When a command is split among several lines with the line-continuation marker (“\”), the continued lines can be indented in any manner; Python's normally stringent indentation rules do not apply. If your Python IDE auto-indents the continued line, you should probably accept its default unless you have a burning reason not to. -
    +

    Also notice that the variable assignment is one command split over several lines, with a backslash (“\”) serving as a line-continuation marker. + + +
    NoteWhen a command is split among several lines with the line-continuation marker (“\”), the continued lines can be indented in any manner; Python's normally stringent indentation rules do not apply. If your Python IDE auto-indents the continued line, you should probably accept its default unless you have a burning reason not to.

    Strictly speaking, expressions in parentheses, straight brackets, or curly braces (like defining a dictionary) can be split into multiple lines with or without the line continuation character (“\”). I like to include the backslash even when it's not required because I think it makes the code easier to read, but that's a matter of style.

    Third, you never declared the variable myParams, you just assigned a value to it. This is like VBScript without the option explicit option. Luckily, unlike VBScript, Python will not allow you to reference a variable that has never been assigned a value; trying to do so will raise an exception.

    3.4.1. Referencing Variables

    -

    Example 3.18. Referencing an Unbound Variable

    >>> x
    -Traceback (innermost last):
    +

    Example 3.18. Referencing an Unbound Variable

    >>> x
    +Traceback (innermost last):
       File "<interactive input>", line 1, in ?
     NameError: There is no variable named 'x'
    ->>> x = 1
    ->>> x
    +>>> x = 1
    +>>> x
     1

    You will thank Python for this one day.

    3.4.2. Assigning Multiple Values at Once

    One of the cooler programming shortcuts in Python is using sequences to assign multiple values at once. -

    Example 3.19. Assigning multiple values at once

    >>> v = ('a', 'b', 'e')
    ->>> (x, y, z) = v     1
    ->>> x
    +

    Example 3.19. Assigning multiple values at once

    >>> v = ('a', 'b', 'e')
    +>>> (x, y, z) = v     
    +>>> x
     'a'
    ->>> y
    +>>> y
     'b'
    ->>> z
    -'e'
    - - - - - -
    1 -v is a tuple of three elements, and (x, y, z) is a tuple of three variables. Assigning one to the other assigns each of the values of v to each of the variables, in order. -
    +>>> z +'e'
    +
      +
    1. v is a tuple of three elements, and (x, y, z) is a tuple of three variables. Assigning one to the other assigns each of the values of v to each of the variables, in order.

      This has all sorts of uses. I often want to assign names to a range of values. In C, you would use enum and manually list each constant and its associated value, which seems especially tedious when the values are consecutive. In Python, you can use the built-in range function with multi-variable assignment to quickly assign consecutive values. -

      Example 3.20. Assigning Consecutive Values

      >>> range(7)              1
      +

      Example 3.20. Assigning Consecutive Values

      >>> range(7)              
       [0, 1, 2, 3, 4, 5, 6]
      ->>> (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7) 2
      ->>> MONDAY                3
      +>>> (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7) 
      +>>> MONDAY                
       0
      ->>> TUESDAY
      +>>> TUESDAY
       1
      ->>> SUNDAY
      -6
      - - - - - - - - - - - - - -
      1 -The built-in range function returns a list of integers. In its simplest form, it takes an upper limit and returns a zero-based list counting +>>> SUNDAY +6
      +
        +
      1. The built-in range function returns a list of integers. In its simplest form, it takes an upper limit and returns a zero-based list counting up to but not including the upper limit. (If you like, you can pass other parameters to specify a base other than 0 and a step other than 1. You can print range.__doc__ for details.) -
      2 -MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are the variables you're defining. (This example came from the calendar module, a fun little module that prints calendars, like the UNIX program cal. The calendar module defines integer constants for days of the week.) -
      3 -Now each variable has its value: MONDAY is 0, TUESDAY is 1, and so forth. -
      +
    2. MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are the variables you're defining. (This example came from the calendar module, a fun little module that prints calendars, like the UNIX program cal. The calendar module defines integer constants for days of the week.) +
    3. Now each variable has its value: MONDAY is 0, TUESDAY is 1, and so forth.

      You can also use multi-variable assignment to build functions that return multiple values, simply by returning a tuple of all the values. The caller can treat it as a tuple, or assign the values to individual variables. Many standard Python libraries do this, including the os module, which you'll discuss in Chapter 6. -

      +

      Further Reading on Variables

      3.5. Formatting Strings

      Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the %s placeholder. - - - - - - - -
      Note
      String formatting in Python uses the same syntax as the sprintf function in C. -
      -

      Example 3.21. Introducing String Formatting

      >>> k = "uid"
      ->>> v = "sa"
      ->>> "%s=%s" % (k, v) 1
      -'uid=sa'
      - - - - - -
      1 -The whole expression evaluates to a string. The first %s is replaced by the value of k; the second %s is replaced by the value of v. All other characters in the string (in this case, the equal sign) stay as they are. -
      + + +
      NoteString formatting in Python uses the same syntax as the sprintf function in C. +

      Example 3.21. Introducing String Formatting

      >>> k = "uid"
      +>>> v = "sa"
      +>>> "%s=%s" % (k, v) 
      +'uid=sa'
      +
        +
      1. The whole expression evaluates to a string. The first %s is replaced by the value of k; the second %s is replaced by the value of v. All other characters in the string (in this case, the equal sign) stay as they are.

        Note that (k, v) is a tuple. I told you they were good for something.

        You might be thinking that this is a lot of work just to do simple string concatentation, and you would be right, except that string formatting isn't just concatenation. It's not even just formatting. It's also type coercion. -

        Example 3.22. String Formatting vs. Concatenating

        >>> uid = "sa"
        ->>> pwd = "secret"
        ->>> print pwd + " is not a good password for " + uid      1
        +

        Example 3.22. String Formatting vs. Concatenating

        >>> uid = "sa"
        +>>> pwd = "secret"
        +>>> print pwd + " is not a good password for " + uid      
         secret is not a good password for sa
        ->>> print "%s is not a good password for %s" % (pwd, uid) 2
        +>>> print "%s is not a good password for %s" % (pwd, uid) 
         secret is not a good password for sa
        ->>> userCount = 6
        ->>> print "Users connected: %d" % (userCount, )           3 4
        +>>> userCount = 6
        +>>> print "Users connected: %d" % (userCount, )            
         Users connected: 6
        ->>> print "Users connected: " + userCount                 5
        -Traceback (innermost last):
        +>>> print "Users connected: " + userCount                 
        +Traceback (innermost last):
           File "<interactive input>", line 1, in ?
        -TypeError: cannot concatenate 'str' and 'int' objects
        - - - - - - - - - - - - - - - - - - - - - -
        1 -+ is the string concatenation operator. -
        2 -In this trivial case, string formatting accomplishes the same result as concatentation.
        3 -(userCount, ) is a tuple with one element. Yes, the syntax is a little strange, but there's a good reason for it: it's unambiguously a +TypeError: cannot concatenate 'str' and 'int' objects
        +
          +
        1. + is the string concatenation operator. +
        2. In this trivial case, string formatting accomplishes the same result as concatentation. +
        3. (userCount, ) is a tuple with one element. Yes, the syntax is a little strange, but there's a good reason for it: it's unambiguously a tuple. In fact, you can always include a comma after the last element when defining a list, tuple, or dictionary, but the comma is required when defining a tuple with one element. If the comma weren't required, Python wouldn't know whether (userCount) was a tuple with one element or just the value of userCount. -
        4 -String formatting works with integers by specifying %d instead of %s. -
        5 -Trying to concatenate a string with a non-string raises an exception. Unlike string formatting, string concatenation works +
      2. String formatting works with integers by specifying %d instead of %s. +
      3. Trying to concatenate a string with a non-string raises an exception. Unlike string formatting, string concatenation works only when everything is already a string. -
      4. As with printf in C, string formatting in Python is like a Swiss Army knife. There are options galore, and modifier strings to specially format many different types of values. -

        Example 3.23. Formatting Numbers

        ->>> print "Today's stock price: %f" % 50.4625   1
        +

        Example 3.23. Formatting Numbers

        +>>> print "Today's stock price: %f" % 50.4625   
         50.462500
        ->>> print "Today's stock price: %.2f" % 50.4625 2
        +>>> print "Today's stock price: %.2f" % 50.4625 
         50.46
        ->>> print "Change since yesterday: %+.2f" % 1.5 3
        +>>> print "Change since yesterday: %+.2f" % 1.5 
         +1.50
        -
        - - - - - - - - - - - - - -
        1 -The %f string formatting option treats the value as a decimal, and prints it to six decimal places. -
        2 -The ".2" modifier of the %f option truncates the value to two decimal places. -
        3 -You can even combine modifiers. Adding the + modifier displays a plus or minus sign before the value. Note that the ".2" modifier is still in place, and is padding +
        +
          +
        1. The %f string formatting option treats the value as a decimal, and prints it to six decimal places. +
        2. The ".2" modifier of the %f option truncates the value to two decimal places. +
        3. You can even combine modifiers. Adding the + modifier displays a plus or minus sign before the value. Note that the ".2" modifier is still in place, and is padding the value to exactly two decimal places. -
        -
        +

        Further Reading on String Formatting

        3.6. Mapping Lists

        One of the most powerful features of Python is the list comprehension, which provides a compact way of mapping a list into another list by applying a function to each of the elements of the list. -

        Example 3.24. Introducing List Comprehensions

        >>> li = [1, 9, 8, 4]
        ->>> [elem*2 for elem in li]      1
        +

        Example 3.24. Introducing List Comprehensions

        >>> li = [1, 9, 8, 4]
        +>>> [elem*2 for elem in li]      
         [2, 18, 16, 8]
        ->>> li         2
        +>>> li         
         [1, 9, 8, 4]
        ->>> li = [elem*2 for elem in li] 3
        ->>> li
        -[2, 18, 16, 8]
        - - - - - - - - - - - - - -
        1 -To make sense of this, look at it from right to left. li is the list you're mapping. Python loops through li one element at a time, temporarily assigning the value of each element to the variable elem. Python then applies the function elem*2 and appends that result to the returned list. -
        2 -Note that list comprehensions do not change the original list.
        3 -It is safe to assign the result of a list comprehension to the variable that you're mapping. Python constructs the new list in memory, and when the list comprehension is complete, it assigns the result to the variable. -
        -
        -

        Here are the list comprehensions in the buildConnectionString function that you declared in Chapter 2:

        +>>> li = [elem*2 for elem in li] 
        +>>> li
        +[2, 18, 16, 8]
        +
          +
        1. To make sense of this, look at it from right to left. li is the list you're mapping. Python loops through li one element at a time, temporarily assigning the value of each element to the variable elem. Python then applies the function elem*2 and appends that result to the returned list. +
        2. Note that list comprehensions do not change the original list. +
        3. It is safe to assign the result of a list comprehension to the variable that you're mapping. Python constructs the new list in memory, and when the list comprehension is complete, it assigns the result to the variable. + +

          Here are the list comprehensions in the buildConnectionString function that you declared in Chapter 2:

          
           ["%s=%s" % (k, v) for k, v in params.items()]

          First, notice that you're calling the items function of the params dictionary. This function returns a list of tuples of all the data in the dictionary. -

          Example 3.25. The keys, values, and items Functions

          >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
          ->>> params.keys()   1
          +

          Example 3.25. The keys, values, and items Functions

          >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
          +>>> params.keys()   
           ['server', 'uid', 'database', 'pwd']
          ->>> params.values() 2
          +>>> params.values() 
           ['mpilgrim', 'sa', 'master', 'secret']
          ->>> params.items()  3
          -[('server', 'mpilgrim'), ('uid', 'sa'), ('database', 'master'), ('pwd', 'secret')]
          - - - - - - - - - - - - - -
          1 -The keys method of a dictionary returns a list of all the keys. The list is not in the order in which the dictionary was defined +>>> params.items() +[('server', 'mpilgrim'), ('uid', 'sa'), ('database', 'master'), ('pwd', 'secret')]
          +
            +
          1. The keys method of a dictionary returns a list of all the keys. The list is not in the order in which the dictionary was defined (remember that elements in a dictionary are unordered), but it is a list. -
          2 -The values method returns a list of all the values. The list is in the same order as the list returned by keys, so params.values()[n] == params[params.keys()[n]] for all values of n. -
          3 -The items method returns a list of tuples of the form (key, value). The list contains all the data in the dictionary. -
          +
        4. The values method returns a list of all the values. The list is in the same order as the list returned by keys, so params.values()[n] == params[params.keys()[n]] for all values of n. +
        5. The items method returns a list of tuples of the form (key, value). The list contains all the data in the dictionary.

          Now let's see what buildConnectionString does. It takes a list, params.items(), and maps it to a new list by applying string formatting to each element. The new list will have the same number of elements as params.items(), but each element in the new list will be a string that contains both a key and its associated value from the params dictionary. -

          Example 3.26. List Comprehensions in buildConnectionString, Step by Step

          >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
          ->>> params.items()
          +

          Example 3.26. List Comprehensions in buildConnectionString, Step by Step

          >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
          +>>> params.items()
           [('server', 'mpilgrim'), ('uid', 'sa'), ('database', 'master'), ('pwd', 'secret')]
          ->>> [k for k, v in params.items()]                1
          +>>> [k for k, v in params.items()]                
           ['server', 'uid', 'database', 'pwd']
          ->>> [v for k, v in params.items()]                2
          +>>> [v for k, v in params.items()]                
           ['mpilgrim', 'sa', 'master', 'secret']
          ->>> ["%s=%s" % (k, v) for k, v in params.items()] 3
          -['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
          - - - - - - - - - - - - - -
          1 -Note that you're using two variables to iterate through the params.items() list. This is another use of multi-variable assignment. The first element of params.items() is ('server', 'mpilgrim'), so in the first iteration of the list comprehension, k will get 'server' and v will get 'mpilgrim'. In this case, you're ignoring the value of v and only including the value of k in the returned list, so this list comprehension ends up being equivalent to params.keys(). -
          2 -Here you're doing the same thing, but ignoring the value of k, so this list comprehension ends up being equivalent to params.values(). -
          3 -Combining the previous two examples with some simple string formatting, you get a list of strings that include both the key and value of each element of the dictionary. This looks suspiciously +>>> ["%s=%s" % (k, v) for k, v in params.items()] +['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
          +
            +
          1. Note that you're using two variables to iterate through the params.items() list. This is another use of multi-variable assignment. The first element of params.items() is ('server', 'mpilgrim'), so in the first iteration of the list comprehension, k will get 'server' and v will get 'mpilgrim'. In this case, you're ignoring the value of v and only including the value of k in the returned list, so this list comprehension ends up being equivalent to params.keys(). +
          2. Here you're doing the same thing, but ignoring the value of k, so this list comprehension ends up being equivalent to params.values(). +
          3. Combining the previous two examples with some simple string formatting, you get a list of strings that include both the key and value of each element of the dictionary. This looks suspiciously like the output of the program. All that remains is to join the elements in this list into a single string. -
          -
          +

          Further Reading on List Comprehensions

          3.7. Joining Lists and Splitting Strings

          You have a list of key-value pairs in the form key=value, and you want to join them into a single string. To join any list of strings into a single string, use the join method of a string object. -

          -

          Here is an example of joining a list from the buildConnectionString function:

          +
          +

          Here is an example of joining a list from the buildConnectionString function:

          
               return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

          One interesting note before you continue. I keep repeating that functions are objects, strings are objects... everything is an object. You might have thought I meant that string variables are objects. But no, look closely at this example and you'll see that the string ";" itself is an object, and you are calling its join method.

          The join method joins the elements of the list into a single string, with each element separated by a semi-colon. The delimiter doesn't -need to be a semi-colon; it doesn't even need to be a single character. It can be any string. - - - - -
          Caution
          join works only on lists of strings; it does not do any type coercion. Joining a list that has one or more non-string elements +need to be a semi-colon; it doesn't even need to be a single character. It can be any string. + + + - -
          Cautionjoin works only on lists of strings; it does not do any type coercion. Joining a list that has one or more non-string elements will raise an exception. -
          -

          Example 3.27. Output of odbchelper.py

          >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
          ->>> ["%s=%s" % (k, v) for k, v in params.items()]
          +

          Example 3.27. Output of odbchelper.py

          >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
          +>>> ["%s=%s" % (k, v) for k, v in params.items()]
           ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
          ->>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])
          +>>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])
           'server=mpilgrim;uid=sa;database=master;pwd=secret'

          This string is then returned from the odbchelper function and printed by the calling block, which gives you the output that you marveled at when you started reading this chapter.

          You're probably wondering if there's an analogous method to split a string into a list. And of course there is, and it's called split. -

          Example 3.28. Splitting a String

          >>> li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
          ->>> s = ";".join(li)
          ->>> s
          +

          Example 3.28. Splitting a String

          >>> li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
          +>>> s = ";".join(li)
          +>>> s
           'server=mpilgrim;uid=sa;database=master;pwd=secret'
          ->>> s.split(";")    1
          +>>> s.split(";")    
           ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
          ->>> s.split(";", 1) 2
          -['server=mpilgrim', 'uid=sa;database=master;pwd=secret']
          - - - - - - - - - -
          1 -split reverses join by splitting a string into a multi-element list. Note that the delimiter (“;”) is stripped out completely; it does not appear in any of the elements of the returned list. -
          2 -split takes an optional second argument, which is the number of times to split. (“Oooooh, optional arguments...” You'll learn how to do this in your own functions in the next chapter.) -
          -
          - - - - -
          Tip
          anystring.split(delimiter, 1) is a useful technique when you want to search a string for a substring and then work with everything before the substring +>>> s.split(";", 1) +['server=mpilgrim', 'uid=sa;database=master;pwd=secret']
          +
            +
          1. split reverses join by splitting a string into a multi-element list. Note that the delimiter (“;”) is stripped out completely; it does not appear in any of the elements of the returned list. +
          2. split takes an optional second argument, which is the number of times to split. (“Oooooh, optional arguments...” You'll learn how to do this in your own functions in the next chapter.) + + + - -
            Tipanystring.split(delimiter, 1) is a useful technique when you want to search a string for a substring and then work with everything before the substring (which ends up in the first element of the returned list) and everything after it (which ends up in the second element). -
            -
            +

            Further Reading on String Methods

            3.7.1. Historical Note on String Methods

            @@ -1192,7 +963,7 @@ called split. the old string module (which still has a lot of useful stuff in it). I use the new join method exclusively, but you will see code written either way, and if it really bothers you, you can use the old string.join function instead.

            3.8. Summary

            The odbchelper.py program and its output should now make perfect sense. -

            +
            
             def buildConnectionString(params):
                 """Build a connection string from a dictionary of parameters.
             
            @@ -1205,10 +976,10 @@ if __name__ == "__main__":
                             "uid":"sa", \
                             "pwd":"secret" \
                             }
            -    print buildConnectionString(myParams)
            -

            Here is the output of odbchelper.py:

            server=mpilgrim;uid=sa;database=master;pwd=secret
            + print buildConnectionString(myParams)
            +

            Here is the output of odbchelper.py:

            server=mpilgrim;uid=sa;database=master;pwd=secret

            Before diving into the next chapter, make sure you're comfortable doing all of these things: -

            +
            • Using the Python IDE to test expressions interactively @@ -1226,7 +997,7 @@ if __name__ == "__main__":
            • Splitting strings into lists and joining lists into strings
            -
            +

            Chapter 4. The Power Of Introspection

            This chapter covers one of Python's strengths: introspection. As you know, everything in Python is an object, and introspection is code looking at other modules and functions in memory as objects, getting information about them, and manipulating them. Along the way, you'll define functions with no name, call functions with arguments out of order, and reference @@ -1234,9 +1005,10 @@ functions whose names you don't even know ahead of time.

            4.1. Diving In

            Here is a complete, working Python program. You should understand a good deal about it just by looking at it. The numbered lines illustrate concepts covered in Chapter 2, Your First Python Program. Don't worry if the rest of the code looks intimidating; you'll learn all about it throughout this chapter. -

            Example 4.1. apihelper.py

            -

            If you have not already done so, you can download this and other examples used in this book.

            -def info(object, spacing=10, collapse=1): 1 2 3
            +

            Example 4.1. apihelper.py

            +

            If you have not already done so, you can download this and other examples used in this book. +

            
            +def info(object, spacing=10, collapse=1):   
                 """Print methods and docstrings.
                 
                 Takes module, class, list, dictionary, or string."""
            @@ -1247,48 +1019,22 @@ def info(object, spacing=10, collapse=1): 4 5
            -    print info.__doc__
            - - - - - - - - - - - - - - - - - - - - - -
            1 -This module has one function, info. According to its function declaration, it takes three parameters: object, spacing, and collapse. The last two are actually optional parameters, as you'll see shortly. -
            2 -The info function has a multi-line docstring that succinctly describes the function's purpose. Note that no return value is mentioned; this function will be used solely +if __name__ == "__main__": + print info.__doc__
            +
              +
            1. This module has one function, info. According to its function declaration, it takes three parameters: object, spacing, and collapse. The last two are actually optional parameters, as you'll see shortly. +
            2. The info function has a multi-line docstring that succinctly describes the function's purpose. Note that no return value is mentioned; this function will be used solely for its effects, rather than its value. -
            3 -Code within the function is indented. -
            4 -The if __name__ trick allows this program do something useful when run by itself, without interfering with its use as a module for other programs. +
          3. Code within the function is indented. +
          4. The if __name__ trick allows this program do something useful when run by itself, without interfering with its use as a module for other programs. In this case, the program simply prints out the docstring of the info function. -
          5. 5 -if statements use == for comparison, and parentheses are not required. -
            +
          6. if statements use == for comparison, and parentheses are not required.

            The info function is designed to be used by you, the programmer, while working in the Python IDE. It takes any object that has functions or methods (like a module, which has functions, or a list, which has methods) and prints out the functions and their docstrings. -

            Example 4.2. Sample Usage of apihelper.py

            >>> from apihelper import info
            ->>> li = []
            ->>> info(li)
            -append     L.append(object) -- append object to end
            +

            Example 4.2. Sample Usage of apihelper.py

            >>> from apihelper import info
            +>>> li = []
            +>>> info(li)
            +append     L.append(object) -- append object to end
             count      L.count(value) -> integer -- return number of occurrences of value
             extend     L.extend(list) -- extend list by appending list elements
             index      L.index(value) -> integer -- return index of first occurrence of value
            @@ -1296,70 +1042,44 @@ insert     L.insert(index, object) -- insert object before index
             pop        L.pop([index]) -> item -- remove and return item at index (default last)
             remove     L.remove(value) -- remove first occurrence of value
             reverse    L.reverse() -- reverse *IN PLACE*
            -sort       L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1

            By default the output is formatted to be easy to read. Multi-line docstrings are collapsed into a single long line, but this option can be changed by specifying 0 for the collapse argument. If the function names are longer than 10 characters, you can specify a larger value for the spacing argument to make the output easier to read. -

            Example 4.3. Advanced Usage of apihelper.py

            >>> import odbchelper
            ->>> info(odbchelper)
            +sort       L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1

            By default the output is formatted to be easy to read. Multi-line docstrings are collapsed into a single long line, but this option can be changed by specifying 0 for the collapse argument. If the function names are longer than 10 characters, you can specify a larger value for the spacing argument to make the output easier to read. +

            Example 4.3. Advanced Usage of apihelper.py

            >>> import odbchelper
            +>>> info(odbchelper)
             buildConnectionString Build a connection string from a dictionary Returns string.
            ->>> info(odbchelper, 30)
            +>>> info(odbchelper, 30)
             buildConnectionString          Build a connection string from a dictionary Returns string.
            ->>> info(odbchelper, 30, 0)
            -buildConnectionString          Build a connection string from a dictionary
            +>>> info(odbchelper, 30, 0)
            +buildConnectionString          Build a connection string from a dictionary
                 
                 Returns string.
             

            4.2. Using Optional and Named Arguments

            Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. Futhermore, arguments can be specified in any order by using named arguments. Stored procedures in SQL Server Transact/SQL can do this, so if you're a SQL Server scripting guru, you can skim this part. -

            -

            Here is an example of info, a function with two optional arguments:

            +
            +

            Here is an example of info, a function with two optional arguments:

            
             def info(object, spacing=10, collapse=1):

            spacing and collapse are optional, because they have default values defined. object is required, because it has no default value. If info is called with only one argument, spacing defaults to 10 and collapse defaults to 1. If info is called with two arguments, collapse still defaults to 1.

            Say you want to specify a value for collapse but want to accept the default value for spacing. In most languages, you would be out of luck, because you would need to call the function with three arguments. But in Python, arguments can be specified by name, in any order. -

            Example 4.4. Valid Calls of info

            -info(odbchelper)  1
            -info(odbchelper, 12)                2
            -info(odbchelper, collapse=0)        3
            -info(spacing=15, object=odbchelper) 4
            - - - - - - - - - - - - - - - - - -
            1 -With only one argument, spacing gets its default value of 10 and collapse gets its default value of 1. -
            2 -With two arguments, collapse gets its default value of 1. -
            3 -Here you are naming the collapse argument explicitly and specifying its value. spacing still gets its default value of 10. -
            4 -Even required arguments (like object, which has no default value) can be named, and named arguments can appear in any order. -
            +

            Example 4.4. Valid Calls of info

            
            +info(odbchelper)  
            +info(odbchelper, 12)                
            +info(odbchelper, collapse=0)        
            +info(spacing=15, object=odbchelper) 
            +
              +
            1. With only one argument, spacing gets its default value of 10 and collapse gets its default value of 1. +
            2. With two arguments, collapse gets its default value of 1. +
            3. Here you are naming the collapse argument explicitly and specifying its value. spacing still gets its default value of 10. +
            4. Even required arguments (like object, which has no default value) can be named, and named arguments can appear in any order.

              This looks totally whacked until you realize that arguments are simply a dictionary. The “normal” method of calling functions without argument names is actually just a shorthand where Python matches up the values with the argument names in the order they're specified in the function declaration. And most of the -time, you'll call functions the “normal” way, but you always have the additional flexibility if you need it. - - - - -
              Note
              The only thing you need to do to call a function is specify a value (somehow) for each required argument; the manner and order +time, you'll call functions the “normal” way, but you always have the additional flexibility if you need it. + + + - -
              NoteThe only thing you need to do to call a function is specify a value (somehow) for each required argument; the manner and order in which you do that is up to you. -
              -
              +

              Further Reading on Optional Arguments

              4.3. Using type, str, dir, and Other Built-In Functions

              @@ -1368,180 +1088,96 @@ time, you'll call functions the “normal” way, but you always have th cough, Visual Basic).

              4.3.1. The type Function

              The type function returns the datatype of any arbitrary object. The possible types are listed in the types module. This is useful for helper functions that can handle several types of data. -

              Example 4.5. Introducing type

              >>> type(1)           1
              +

              Example 4.5. Introducing type

              >>> type(1)           
               <type 'int'>
              ->>> li = []
              ->>> type(li)          2
              +>>> li = []
              +>>> type(li)          
               <type 'list'>
              ->>> import odbchelper
              ->>> type(odbchelper)  3
              +>>> import odbchelper
              +>>> type(odbchelper)  
               <type 'module'>
              ->>> import types      4
              ->>> type(odbchelper) == types.ModuleType
              -True
              - - - - - - - - - - - - - - - - - -
              1 -type takes anything -- and I mean anything -- and returns its datatype. Integers, strings, lists, dictionaries, tuples, functions, +>>> import types +>>> type(odbchelper) == types.ModuleType +True
              +
                +
              1. type takes anything -- and I mean anything -- and returns its datatype. Integers, strings, lists, dictionaries, tuples, functions, classes, modules, even types are acceptable. -
              2 -type can take a variable and return its datatype. -
              3 -type also works on modules. -
              4 -You can use the constants in the types module to compare types of objects. This is what the info function does, as you'll see shortly. -
              +
            5. type can take a variable and return its datatype. +
            6. type also works on modules. +
            7. You can use the constants in the types module to compare types of objects. This is what the info function does, as you'll see shortly.

              4.3.2. The str Function

              The str coerces data into a string. Every datatype can be coerced into a string. -

              Example 4.6. Introducing str

              ->>> str(1)          1
              +

              Example 4.6. Introducing str

              +>>> str(1)          
               '1'
              ->>> horsemen = ['war', 'pestilence', 'famine']
              ->>> horsemen
              +>>> horsemen = ['war', 'pestilence', 'famine']
              +>>> horsemen
               ['war', 'pestilence', 'famine']
              ->>> horsemen.append('Powerbuilder')
              ->>> str(horsemen)   2
              +>>> horsemen.append('Powerbuilder')
              +>>> str(horsemen)   
               "['war', 'pestilence', 'famine', 'Powerbuilder']"
              ->>> str(odbchelper) 3
              +>>> str(odbchelper) 
               "<module 'odbchelper' from 'c:\\docbook\\dip\\py\\odbchelper.py'>"
              ->>> str(None)       4
              -'None'
              - - - - - - - - - - - - - - - - - -
              1 -For simple datatypes like integers, you would expect str to work, because almost every language has a function to convert an integer to a string. -
              2 -However, str works on any object of any type. Here it works on a list which you've constructed in bits and pieces. -
              3 -str also works on modules. Note that the string representation of the module includes the pathname of the module on disk, so +>>> str(None) +'None'
              +
                +
              1. For simple datatypes like integers, you would expect str to work, because almost every language has a function to convert an integer to a string. +
              2. However, str works on any object of any type. Here it works on a list which you've constructed in bits and pieces. +
              3. str also works on modules. Note that the string representation of the module includes the pathname of the module on disk, so yours will be different. -
              4 -A subtle but important behavior of str is that it works on None, the Python null value. It returns the string 'None'. You'll use this to your advantage in the info function, as you'll see shortly. -
              +
            8. A subtle but important behavior of str is that it works on None, the Python null value. It returns the string 'None'. You'll use this to your advantage in the info function, as you'll see shortly.

              At the heart of the info function is the powerful dir function. dir returns a list of the attributes and methods of any object: modules, functions, strings, lists, dictionaries... pretty much anything. -

              Example 4.7. Introducing dir

              >>> li = []
              ->>> dir(li)           1
              -['append', 'count', 'extend', 'index', 'insert',
              +

              Example 4.7. Introducing dir

              >>> li = []
              +>>> dir(li)           
              +['append', 'count', 'extend', 'index', 'insert',
               'pop', 'remove', 'reverse', 'sort']
              ->>> d = {}
              ->>> dir(d)            2
              +>>> d = {}
              +>>> dir(d)            
               ['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'setdefault', 'update', 'values']
              ->>> import odbchelper
              ->>> dir(odbchelper)   3
              -['__builtins__', '__doc__', '__file__', '__name__', 'buildConnectionString']
              - - - - - - - - - - - - - -
              1 -li is a list, so dir(li) returns a list of all the methods of a list. Note that the returned list contains the names of the methods as strings, not +>>> import odbchelper +>>> dir(odbchelper) +['__builtins__', '__doc__', '__file__', '__name__', 'buildConnectionString']
              +
                +
              1. li is a list, so dir(li) returns a list of all the methods of a list. Note that the returned list contains the names of the methods as strings, not the methods themselves. -
              2 -d is a dictionary, so dir(d) returns a list of the names of dictionary methods. At least one of these, keys, should look familiar. -
              3 -This is where it really gets interesting. odbchelper is a module, so dir(odbchelper) returns a list of all kinds of stuff defined in the module, including built-in attributes, like __name__, __doc__, and whatever other attributes and methods you define. In this case, odbchelper has only one user-defined method, the buildConnectionString function described in Chapter 2. -
              +
            9. d is a dictionary, so dir(d) returns a list of the names of dictionary methods. At least one of these, keys, should look familiar. +
            10. This is where it really gets interesting. odbchelper is a module, so dir(odbchelper) returns a list of all kinds of stuff defined in the module, including built-in attributes, like __name__, __doc__, and whatever other attributes and methods you define. In this case, odbchelper has only one user-defined method, the buildConnectionString function described in Chapter 2.

              Finally, the callable function takes any object and returns True if the object can be called, or False otherwise. Callable objects include functions, class methods, even classes themselves. (More on classes in the next chapter.) -

              Example 4.8. Introducing callable

              ->>> import string
              ->>> string.punctuation           1
              +

              Example 4.8. Introducing callable

              +>>> import string
              +>>> string.punctuation           
               '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
              ->>> string.join2
              +>>> string.join
               <function join at 00C55A7C>
              ->>> callable(string.punctuation) 3
              +>>> callable(string.punctuation) 
               False
              ->>> callable(string.join)        4
              +>>> callable(string.join)        
               True
              ->>> print string.join.__doc__    5
              -join(list [,sep]) -> string
              +>>> print string.join.__doc__    
              +join(list [,sep]) -> string
               
                   Return a string composed of the words in list, with
                   intervening occurrences of sep. The default separator is a
                   single space.
               
              -    (joinfields and join are synonymous)
              - - - - - - - - - - - - - - - - - - - - - -
              1 -The functions in the string module are deprecated (although many people still use the join function), but the module contains a lot of useful constants like this string.punctuation, which contains all the standard punctuation characters. -
              2 -string.join is a function that joins a list of strings. -
              3 -string.punctuation is not callable; it is a string. (A string does have callable methods, but the string itself is not callable.) -
              4 -string.join is callable; it's a function that takes two arguments. -
              5 -Any callable object may have a docstring. By using the callable function on each of an object's attributes, you can determine which attributes you care about (methods, functions, classes) + (joinfields and join are synonymous)
              +
                +
              1. The functions in the string module are deprecated (although many people still use the join function), but the module contains a lot of useful constants like this string.punctuation, which contains all the standard punctuation characters. +
              2. string.join is a function that joins a list of strings. +
              3. string.punctuation is not callable; it is a string. (A string does have callable methods, but the string itself is not callable.) +
              4. string.join is callable; it's a function that takes two arguments. +
              5. Any callable object may have a docstring. By using the callable function on each of an object's attributes, you can determine which attributes you care about (methods, functions, classes) and which you want to ignore (constants and so on) without knowing anything about the object ahead of time. -

              4.3.3. Built-In Functions

              type, str, dir, and all the rest of Python's built-in functions are grouped into a special module called __builtin__. (That's two underscores before and after.) If it helps, you can think of Python automatically executing from __builtin__ import * on startup, which imports all the “built-in” functions into the namespace so you can use them directly.

              The advantage of thinking like this is that you can access all the built-in functions and attributes as a group by getting information about the __builtin__ module. And guess what, Python has a function called info. Try it yourself and skim through the list now. We'll dive into some of the more important functions later. (Some of the built-in error classes, like AttributeError, should already look familiar.) -

              Example 4.9. Built-in Attributes and Functions

              >>> from apihelper import info
              ->>> import __builtin__
              ->>> info(__builtin__, 20)
              -ArithmeticError      Base class for arithmetic errors.
              +

              Example 4.9. Built-in Attributes and Functions

              >>> from apihelper import info
              +>>> import __builtin__
              +>>> info(__builtin__, 20)
              +ArithmeticError      Base class for arithmetic errors.
               AssertionError       Assertion failed.
               AttributeError       Attribute not found.
               EOFError             Read beyond end of file.
              @@ -1550,215 +1186,119 @@ Exception            Common base class for all exceptions.
               FloatingPointError   Floating point operation failed.
               IOError              I/O operation failed.
               
              -[...snip...]
              - - - - -
              Note
              Python comes with excellent reference manuals, which you should peruse thoroughly to learn all the modules Python has to offer. But unlike most languages, where you would find yourself referring back to the manuals or man pages to remind +[...snip...] + + - -
              NotePython comes with excellent reference manuals, which you should peruse thoroughly to learn all the modules Python has to offer. But unlike most languages, where you would find yourself referring back to the manuals or man pages to remind yourself how to use these modules, Python is largely self-documenting. -
              -
              +

              Further Reading on Built-In Functions

              4.4. Getting Object References With getattr

              You already know that Python functions are objects. What you don't know is that you can get a reference to a function without knowing its name until run-time, by using the getattr function. -

              Example 4.10. Introducing getattr

              >>> li = ["Larry", "Curly"]
              ->>> li.pop     1
              +

              Example 4.10. Introducing getattr

              >>> li = ["Larry", "Curly"]
              +>>> li.pop     
               <built-in method pop of list object at 010DF884>
              ->>> getattr(li, "pop")           2
              +>>> getattr(li, "pop")           
               <built-in method pop of list object at 010DF884>
              ->>> getattr(li, "append")("Moe") 3
              ->>> li
              +>>> getattr(li, "append")("Moe") 
              +>>> li
               ["Larry", "Curly", "Moe"]
              ->>> getattr({}, "clear")         4
              +>>> getattr({}, "clear")         
               <built-in method clear of dictionary object at 00F113D4>
              ->>> getattr((), "pop")           5
              -Traceback (innermost last):
              +>>> getattr((), "pop")           
              +Traceback (innermost last):
                 File "<interactive input>", line 1, in ?
              -AttributeError: 'tuple' object has no attribute 'pop'
              - - - - - - - - - - - - - - - - - - - - - -
              1 -This gets a reference to the pop method of the list. Note that this is not calling the pop method; that would be li.pop(). This is the method itself. -
              2 -This also returns a reference to the pop method, but this time, the method name is specified as a string argument to the getattr function. getattr is an incredibly useful built-in function that returns any attribute of any object. In this case, the object is a list, +AttributeError: 'tuple' object has no attribute 'pop'
              +
                +
              1. This gets a reference to the pop method of the list. Note that this is not calling the pop method; that would be li.pop(). This is the method itself. +
              2. This also returns a reference to the pop method, but this time, the method name is specified as a string argument to the getattr function. getattr is an incredibly useful built-in function that returns any attribute of any object. In this case, the object is a list, and the attribute is the pop method. -
              3 -In case it hasn't sunk in just how incredibly useful this is, try this: the return value of getattr is the method, which you can then call just as if you had said li.append("Moe") directly. But you didn't call the function directly; you specified the function name as a string instead. -
              4 -getattr also works on dictionaries. -
              5 -In theory, getattr would work on tuples, except that tuples have no methods, so getattr will raise an exception no matter what attribute name you give. -
              +
            11. In case it hasn't sunk in just how incredibly useful this is, try this: the return value of getattr is the method, which you can then call just as if you had said li.append("Moe") directly. But you didn't call the function directly; you specified the function name as a string instead. +
            12. getattr also works on dictionaries. +
            13. In theory, getattr would work on tuples, except that tuples have no methods, so getattr will raise an exception no matter what attribute name you give.

              4.4.1. getattr with Modules

              getattr isn't just for built-in datatypes. It also works on modules. -

              Example 4.11. The getattr Function in apihelper.py

              >>> import odbchelper
              ->>> odbchelper.buildConnectionString             1
              +

              Example 4.11. The getattr Function in apihelper.py

              >>> import odbchelper
              +>>> odbchelper.buildConnectionString             
               <function buildConnectionString at 00D18DD4>
              ->>> getattr(odbchelper, "buildConnectionString") 2
              +>>> getattr(odbchelper, "buildConnectionString") 
               <function buildConnectionString at 00D18DD4>
              ->>> object = odbchelper
              ->>> method = "buildConnectionString"
              ->>> getattr(object, method)    3
              +>>> object = odbchelper
              +>>> method = "buildConnectionString"
              +>>> getattr(object, method)    
               <function buildConnectionString at 00D18DD4>
              ->>> type(getattr(object, method))                4
              +>>> type(getattr(object, method))                
               <type 'function'>
              ->>> import types
              ->>> type(getattr(object, method)) == types.FunctionType
              +>>> import types
              +>>> type(getattr(object, method)) == types.FunctionType
               True
              ->>> callable(getattr(object, method))            5
              -True
              - - - - - - - - - - - - - - - - - - - - - -
              1 -This returns a reference to the buildConnectionString function in the odbchelper module, which you studied in Chapter 2, Your First Python Program. (The hex address you see is specific to my machine; your output will be different.) -
              2 -Using getattr, you can get the same reference to the same function. In general, getattr(object, "attribute") is equivalent to object.attribute. If object is a module, then attribute can be anything defined in the module: a function, class, or global variable. -
              3 -And this is what you actually use in the info function. object is passed into the function as an argument; method is a string which is the name of a method or function. -
              4 -In this case, method is the name of a function, which you can prove by getting its type. -
              5 -Since method is a function, it is callable. -
              +>>> callable(getattr(object, method)) +True
              +
                +
              1. This returns a reference to the buildConnectionString function in the odbchelper module, which you studied in Chapter 2, Your First Python Program. (The hex address you see is specific to my machine; your output will be different.) +
              2. Using getattr, you can get the same reference to the same function. In general, getattr(object, "attribute") is equivalent to object.attribute. If object is a module, then attribute can be anything defined in the module: a function, class, or global variable. +
              3. And this is what you actually use in the info function. object is passed into the function as an argument; method is a string which is the name of a method or function. +
              4. In this case, method is the name of a function, which you can prove by getting its type. +
              5. Since method is a function, it is callable.

                4.4.2. getattr As a Dispatcher

                A common usage pattern of getattr is as a dispatcher. For example, if you had a program that could output data in a variety of different formats, you could define separate functions for each output format and use a single dispatch function to call the right one.

                For example, let's imagine a program that prints site statistics in HTML, XML, and plain text formats. The choice of output format could be specified on the command line, or stored in a configuration file. A statsout module defines three functions, output_html, output_xml, and output_text. Then the main program defines a single output function, like this: -

                Example 4.12. Creating a Dispatcher with getattr

                +

                Example 4.12. Creating a Dispatcher with getattr

                
                 import statsout
                 
                -def output(data, format="text"):            1
                -    output_function = getattr(statsout, "output_%s" % format) 2
                -    return output_function(data)            3
                -
                - - - - - - - - - - - - - -
                1 -The output function takes one required argument, data, and one optional argument, format. If format is not specified, it defaults to text, and you will end up calling the plain text output function. -
                2 -You concatenate the format argument with "output_" to produce a function name, and then go get that function from the statsout module. This allows you to easily extend the program later to support other output formats, without changing this dispatch +def output(data, format="text"): + output_function = getattr(statsout, "output_%s" % format) + return output_function(data) +
                +
                  +
                1. The output function takes one required argument, data, and one optional argument, format. If format is not specified, it defaults to text, and you will end up calling the plain text output function. +
                2. You concatenate the format argument with "output_" to produce a function name, and then go get that function from the statsout module. This allows you to easily extend the program later to support other output formats, without changing this dispatch function. Just add another function to statsout named, for instance, output_pdf, and pass "pdf" as the format into the output function. -
                3 -Now you can simply call the output function in the same way as any other function. The output_function variable is a reference to the appropriate function from the statsout module. -
                +
              6. Now you can simply call the output function in the same way as any other function. The output_function variable is a reference to the appropriate function from the statsout module.

                Did you see the bug in the previous example? This is a very loose coupling of strings and functions, and there is no error checking. What happens if the user passes in a format that doesn't have a corresponding function defined in statsout? Well, getattr will return None, which will be assigned to output_function instead of a valid function, and the next line that attempts to call that function will crash and raise an exception. That's bad.

                Luckily, getattr takes an optional third argument, a default value. -

                Example 4.13. getattr Default Values

                +

                Example 4.13. getattr Default Values

                
                 import statsout
                 
                 def output(data, format="text"):
                     output_function = getattr(statsout, "output_%s" % format, statsout.output_text)
                -    return output_function(data) 1
                -
                - - - - - -
                1 -This function call is guaranteed to work, because you added a third argument to the call to getattr. The third argument is a default value that is returned if the attribute or method specified by the second argument wasn't + return output_function(data) +
                +
                  +
                1. This function call is guaranteed to work, because you added a third argument to the call to getattr. The third argument is a default value that is returned if the attribute or method specified by the second argument wasn't found. -

                As you can see, getattr is quite powerful. It is the heart of introspection, and you'll see even more powerful examples of it in later chapters.

                4.5. Filtering Lists

                As you know, Python has powerful capabilities for mapping lists into other lists, via list comprehensions (Section 3.6, “Mapping Lists”). This can be combined with a filtering mechanism, where some elements in the list are mapped while others are skipped entirely. -

                -

                Here is the list filtering syntax:

                +
                +

                Here is the list filtering syntax:

                
                 [mapping-expression for element in source-list if filter-expression]

                This is an extension of the list comprehensions that you know and love. The first two thirds are the same; the last part, starting with the if, is the filter expression. A filter expression can be any expression that evaluates true or false (which in Python can be almost anything). Any element for which the filter expression evaluates true will be included in the mapping. All other elements are ignored, so they are never put through the mapping expression and are not included in the output list. -

                Example 4.14. Introducing List Filtering

                >>> li = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"]
                ->>> [elem for elem in li if len(elem) > 1]       1
                +

                Example 4.14. Introducing List Filtering

                >>> li = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"]
                +>>> [elem for elem in li if len(elem) > 1]       
                 ['mpilgrim', 'foo']
                ->>> [elem for elem in li if elem != "b"]         2
                +>>> [elem for elem in li if elem != "b"]         
                 ['a', 'mpilgrim', 'foo', 'c', 'd', 'd']
                ->>> [elem for elem in li if li.count(elem) == 1] 3
                -['a', 'mpilgrim', 'foo', 'c']
                - - - - - - - - - - - - - -
                1 -The mapping expression here is simple (it just returns the value of each element), so concentrate on the filter expression. +>>> [elem for elem in li if li.count(elem) == 1] +['a', 'mpilgrim', 'foo', 'c']
                +
                  +
                1. The mapping expression here is simple (it just returns the value of each element), so concentrate on the filter expression. As Python loops through the list, it runs each element through the filter expression. If the filter expression is true, the element is mapped and the result of the mapping expression is included in the returned list. Here, you are filtering out all the one-character strings, so you're left with a list of all the longer strings. -
                2 -Here, you are filtering out a specific value, b. Note that this filters all occurrences of b, since each time it comes up, the filter expression will be false. -
                3 -count is a list method that returns the number of times a value occurs in a list. You might think that this filter would eliminate +
              7. Here, you are filtering out a specific value, b. Note that this filters all occurrences of b, since each time it comes up, the filter expression will be false. +
              8. count is a list method that returns the number of times a value occurs in a list. You might think that this filter would eliminate duplicates from a list, returning a list containing only one copy of each value in the original list. But it doesn't, because values that appear twice in the original list (in this case, b and d) are excluded completely. There are ways of eliminating duplicates from a list, but filtering is not the solution. -
              9. -

                Let's id="apihelper.filter.care" get back to this line from apihelper.py:

                +

                Let's id="apihelper.filter.care" get back to this line from apihelper.py:

                
                     methodList = [method for method in dir(object) if callable(getattr(object, method))]

                This looks complicated, and it is complicated, but the basic structure is the same. The whole filter expression returns a list, which is assigned to the methodList variable. The first half of the expression is the list mapping part. The mapping expression is an identity expression, which it returns the value of each element. dir(object) returns a list of object's attributes and methods -- that's the list you're mapping. So the only new part is the filter expression after the if. @@ -1767,136 +1307,76 @@ which it returns the value of each element. dir(objectgetattr function. Then you check to see if that object is callable, which will be any methods and functions, both built-in (like the pop method of a list) and user-defined (like the buildConnectionString function of the odbchelper module). You don't care about other attributes, like the __name__ attribute that's built in to every module. -

                +

                Further Reading on Filtering Lists

                4.6. The Peculiar Nature of and and or

                In Python, and and or perform boolean logic as you would expect, but they do not return boolean values; instead, they return one of the actual values they are comparing. -

                Example 4.15. Introducing and

                >>> 'a' and 'b'         1
                +

                Example 4.15. Introducing and

                >>> 'a' and 'b'         
                 'b'
                ->>> '' and 'b'          2
                +>>> '' and 'b'          
                 ''
                ->>> 'a' and 'b' and 'c' 3
                -'c'
                - - - - - - - - - - - - - -
                1 -When using and, values are evaluated in a boolean context from left to right. 0, '', [], (), {}, and None are false in a boolean context; everything else is true. Well, almost everything. By default, instances of classes are +>>> 'a' and 'b' and 'c' +'c'
                +
                  +
                1. When using and, values are evaluated in a boolean context from left to right. 0, '', [], (), {}, and None are false in a boolean context; everything else is true. Well, almost everything. By default, instances of classes are true in a boolean context, but you can define special methods in your class to make an instance evaluate to false. You'll learn all about classes and special methods in Chapter 5. If all values are true in a boolean context, and returns the last value. In this case, and evaluates 'a', which is true, then 'b', which is true, and returns 'b'. -
                2 -If any value is false in a boolean context, and returns the first false value. In this case, '' is the first false value. -
                3 -All values are true, so and returns the last value, 'c'. -
                -

                Example 4.16. Introducing or

                >>> 'a' or 'b'          1
                +
              10. If any value is false in a boolean context, and returns the first false value. In this case, '' is the first false value. +
              11. All values are true, so and returns the last value, 'c'. +

                Example 4.16. Introducing or

                >>> 'a' or 'b'          
                 'a'
                ->>> '' or 'b'           2
                +>>> '' or 'b'           
                 'b'
                ->>> '' or [] or {}      3
                +>>> '' or [] or {}      
                 {}
                ->>> def sidefx():
                -...    print "in sidefx()"
                -...    return 1
                ->>> 'a' or sidefx()     4
                -'a'
                - - - - - - - - - - - - - - - - - -
                1 -When using or, values are evaluated in a boolean context from left to right, just like and. If any value is true, or returns that value immediately. In this case, 'a' is the first true value. -
                2 -or evaluates '', which is false, then 'b', which is true, and returns 'b'. -
                3 -If all values are false, or returns the last value. or evaluates '', which is false, then [], which is false, then {}, which is false, and returns {}. -
                4 -Note that or evaluates values only until it finds one that is true in a boolean context, and then it ignores the rest. This distinction +>>> def sidefx(): +... print "in sidefx()" +... return 1 +>>> 'a' or sidefx() +'a'
                +
                  +
                1. When using or, values are evaluated in a boolean context from left to right, just like and. If any value is true, or returns that value immediately. In this case, 'a' is the first true value. +
                2. or evaluates '', which is false, then 'b', which is true, and returns 'b'. +
                3. If all values are false, or returns the last value. or evaluates '', which is false, then [], which is false, then {}, which is false, and returns {}. +
                4. Note that or evaluates values only until it finds one that is true in a boolean context, and then it ignores the rest. This distinction is important if some values can have side effects. Here, the function sidefx is never called, because or evaluates 'a', which is true, and returns 'a' immediately. -

                If you're a C hacker, you are certainly familiar with the bool ? a : b expression, which evaluates to a if bool is true, and b otherwise. Because of the way and and or work in Python, you can accomplish the same thing.

                4.6.1. Using the and-or Trick

                -

                Example 4.17. Introducing the and-or Trick

                >>> a = "first"
                ->>> b = "second"
                ->>> 1 and a or b 1
                +

                Example 4.17. Introducing the and-or Trick

                >>> a = "first"
                +>>> b = "second"
                +>>> 1 and a or b 
                 'first'
                ->>> 0 and a or b 2
                +>>> 0 and a or b 
                 'second'
                -
                - - - - - - - - - -
                1 -This syntax looks similar to the bool ? a : b expression in C. The entire expression is evaluated from left to right, so the and is evaluated first. 1 and 'first' evalutes to 'first', then 'first' or 'second' evalutes to 'first'. -
                2 -0 and 'first' evalutes to False, and then 0 or 'second' evaluates to 'second'. -
                +
                +
                  +
                1. This syntax looks similar to the bool ? a : b expression in C. The entire expression is evaluated from left to right, so the and is evaluated first. 1 and 'first' evalutes to 'first', then 'first' or 'second' evalutes to 'first'. +
                2. 0 and 'first' evalutes to False, and then 0 or 'second' evaluates to 'second'.

                  However, since this Python expression is simply boolean logic, and not a special construct of the language, there is one extremely important difference between this and-or trick in Python and the bool ? a : b syntax in C. If the value of a is false, the expression will not work as you would expect it to. (Can you tell I was bitten by this? More than once?) -

                  Example 4.18. When the and-or Trick Fails

                  >>> a = ""
                  ->>> b = "second"
                  ->>> 1 and a or b         1
                  -'second'
                  - - - - - -
                  1 -Since a is an empty string, which Python considers false in a boolean context, 1 and '' evalutes to '', and then '' or 'second' evalutes to 'second'. Oops! That's not what you wanted. -
                  +

                  Example 4.18. When the and-or Trick Fails

                  >>> a = ""
                  +>>> b = "second"
                  +>>> 1 and a or b         
                  +'second'
                  +
                    +
                  1. Since a is an empty string, which Python considers false in a boolean context, 1 and '' evalutes to '', and then '' or 'second' evalutes to 'second'. Oops! That's not what you wanted.

                    The and-or trick, bool and a or b, will not work like the C expression bool ? a : b when a is false in a boolean context.

                    The real trick behind the and-or trick, then, is to make sure that the value of a is never false. One common way of doing this is to turn a into [a] and b into [b], then taking the first element of the returned list, which will be either a or b. -

                    Example 4.19. Using the and-or Trick Safely

                    >>> a = ""
                    ->>> b = "second"
                    ->>> (1 and [a] or [b])[0] 1
                    -''
                    - - - - - -
                    1 -Since [a] is a non-empty list, it is never false. Even if a is 0 or '' or some other false value, the list [a] is true because it has one element. -
                    +

                    Example 4.19. Using the and-or Trick Safely

                    >>> a = ""
                    +>>> b = "second"
                    +>>> (1 and [a] or [b])[0] 
                    +''
                    +
                      +
                    1. Since [a] is a non-empty list, it is never false. Even if a is 0 or '' or some other false value, the list [a] is true because it has one element.

                      By now, this trick may seem like more trouble than it's worth. You could, after all, accomplish the same thing with an if statement, so why go through all this fuss? Well, in many cases, you are choosing between two constant values, so you can use the simpler syntax and not worry, because you know that the a value will always be true. And even if you need to use the more complicated safe form, there are good reasons to do so. For example, there are some cases in Python where if statements are not allowed, such as in lambda functions. -

                      +

                      Further Reading on the and-or Trick

                      4.7. Using lambda Functions

                      Python supports an interesting syntax that lets you define one-line mini-functions on the fly. Borrowed from Lisp, these so-called lambda functions can be used anywhere a function is required. -

                      Example 4.20. Introducing lambda Functions

                      >>> def f(x):
                      -...    return x*2
                      -...    
                      ->>> f(3)
                      +

                      Example 4.20. Introducing lambda Functions

                      >>> def f(x):
                      +...    return x*2
                      +...    
                      +>>> f(3)
                       6
                      ->>> g = lambda x: x*2  1
                      ->>> g(3)
                      +>>> g = lambda x: x*2  
                      +>>> g(3)
                       6
                      ->>> (lambda x: x*2)(3) 2
                      -6
                      - - - - - - - - - -
                      1 -This is a lambda function that accomplishes the same thing as the normal function above it. Note the abbreviated syntax here: there are no +>>> (lambda x: x*2)(3) +6
                      +
                        +
                      1. This is a lambda function that accomplishes the same thing as the normal function above it. Note the abbreviated syntax here: there are no parentheses around the argument list, and the return keyword is missing (it is implied, since the entire function can only be one expression). Also, the function has no name, but it can be called through the variable it is assigned to. -
                      2 -You can use a lambda function without even assigning it to a variable. This may not be the most useful thing in the world, but it just goes to +
                    2. You can use a lambda function without even assigning it to a variable. This may not be the most useful thing in the world, but it just goes to show that a lambda is just an in-line function. -
                    3. To generalize, a lambda function is a function that takes any number of arguments (including optional arguments) and returns the value of a single expression. lambda functions can not contain commands, and they can not contain more than one expression. Don't try to squeeze too much into -a lambda function; if you need something more complex, define a normal function instead and make it as long as you want. - - - - -
                      Note
                      lambda functions are a matter of style. Using them is never required; anywhere you could use them, you could define a separate +a lambda function; if you need something more complex, define a normal function instead and make it as long as you want. + + + - -
                      Notelambda functions are a matter of style. Using them is never required; anywhere you could use them, you could define a separate normal function and use that instead. I use them in places where I want to encapsulate specific, non-reusable code without littering my code with a lot of little one-line functions. -

                      4.7.1. Real-World lambda Functions

                      -
                      -

                      Here are the lambda functions in apihelper.py:

                      +
                      +

                      Here are the lambda functions in apihelper.py:

                      
                           processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)

                      Notice that this uses the simple form of the and-or trick, which is okay, because a lambda function is always true in a boolean context. (That doesn't mean that a lambda function can't return a false value. The function is always true; its return value could be anything.)

                      Also notice that you're using the split function with no arguments. You've already seen it used with one or two arguments, but without any arguments it splits on whitespace. -

                      Example 4.21. split With No Arguments

                      >>> s = "this   is\na\ttest"  1
                      ->>> print s
                      -this   is
                      +

                      Example 4.21. split With No Arguments

                      >>> s = "this   is\na\ttest"  
                      +>>> print s
                      +this   is
                       a	test
                      ->>> print s.split()           2
                      +>>> print s.split()           
                       ['this', 'is', 'a', 'test']
                      ->>> print " ".join(s.split()) 3
                      -'this is a test'
                      - - - - - - - - - - - - - -
                      1 -This is a multiline string, defined by escape characters instead of triple quotes. \n is a carriage return, and \t is a tab character. -
                      2 -split without any arguments splits on whitespace. So three spaces, a carriage return, and a tab character are all the same. -
                      3 -You can normalize whitespace by splitting a string with split and then rejoining it with join, using a single space as a delimiter. This is what the info function does to collapse multi-line docstrings into a single line. -
                      +>>> print " ".join(s.split()) +'this is a test'
                      +
                        +
                      1. This is a multiline string, defined by escape characters instead of triple quotes. \n is a carriage return, and \t is a tab character. +
                      2. split without any arguments splits on whitespace. So three spaces, a carriage return, and a tab character are all the same. +
                      3. You can normalize whitespace by splitting a string with split and then rejoining it with join, using a single space as a delimiter. This is what the info function does to collapse multi-line docstrings into a single line.

                        So what is the info function actually doing with these lambda functions, splits, and and-or tricks? -

                        +
                             processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)

                        processFunc is now a function, but which function it is depends on the value of the collapse variable. If collapse is true, processFunc(string) will collapse whitespace; otherwise, processFunc(string) will return its argument unchanged. -

                        To do this in a less robust language, like Visual Basic, you would probably create a function that took a string and a collapse argument and used an if statement to decide whether to collapse the whitespace or not, then returned the appropriate value. This would be inefficient, +

                        To do this in a less robust language, like Visual Basic, you would probably create a function that took a string and a collapse argument and used an if statement to decide whether to collapse the whitespace or not, then returned the appropriate value. This would be inefficient, because the function would need to handle every possible case. Every time you called it, it would need to decide whether to collapse whitespace before it could give you what you wanted. In Python, you can take that decision logic out of the function and define a lambda function that is custom-tailored to give you exactly (and only) what you want. This is more efficient, more elegant, and less prone to those nasty oh-I-thought-those-arguments-were-reversed kinds of errors. -

                        +

                        Further Reading on lambda Functions

                        4.8. Putting It All Together

                        The last line of code, the only one you haven't deconstructed yet, is the one that does all the work. But by now the work is easy, because everything you need is already set up just the way you need it. All the dominoes are in place; it's time to knock them down. -

                        -

                        This is the meat of apihelper.py:

                        +
                        +

                        This is the meat of apihelper.py:

                        
                             print "\n".join(["%s %s" %
                             (method.ljust(spacing),
                              processFunc(str(getattr(object, method).__doc__)))
                            for method in methodList])

                        Note that this is one command, split over multiple lines, but it doesn't use the line continuation character (\). Remember when I said that some expressions can be split into multiple lines without using a backslash? A list comprehension is one of those expressions, since the entire expression is contained in square brackets. -

                        Now, let's take it from the end and work backwards. The

                        +

                        Now, let's take it from the end and work backwards. The

                        
                         for method in methodList

                        shows that this is a list comprehension. As you know, methodList is a list of all the methods you care about in object. So you're looping through that list with method. -

                        Example 4.22. Getting a docstring Dynamically

                        >>> import odbchelper
                        ->>> object = odbchelper 1
                        ->>> method = 'buildConnectionString'      2
                        ->>> getattr(object, method)               3
                        +

                        Example 4.22. Getting a docstring Dynamically

                        >>> import odbchelper
                        +>>> object = odbchelper 
                        +>>> method = 'buildConnectionString'      
                        +>>> getattr(object, method)               
                         <function buildConnectionString at 010D6D74>
                        ->>> print getattr(object, method).__doc__ 4
                        -Build a connection string from a dictionary of parameters.
                        +>>> print getattr(object, method).__doc__ 
                        +Build a connection string from a dictionary of parameters.
                         
                        -    Returns string.
                        - - - - - - - - - - - - - - - - - -
                        1 -In the info function, object is the object you're getting help on, passed in as an argument. -
                        2 -As you're looping through methodList, method is the name of the current method. -
                        3 -Using the getattr function, you're getting a reference to the method function in the object module. -
                        4 -Now, printing the actual docstring of the method is easy. -
                        + Returns string.
                        +
                          +
                        1. In the info function, object is the object you're getting help on, passed in as an argument. +
                        2. As you're looping through methodList, method is the name of the current method. +
                        3. Using the getattr function, you're getting a reference to the method function in the object module. +
                        4. Now, printing the actual docstring of the method is easy.

                          The next piece of the puzzle is the use of str around the docstring. As you may recall, str is a built-in function that coerces data into a string. But a docstring is always a string, so why bother with the str function? The answer is that not every function has a docstring, and if it doesn't, its __doc__ attribute is None. -

                          Example 4.23. Why Use str on a docstring?

                          >>> >>> def foo(): print 2
                          ->>> >>> foo()
                          +

                          Example 4.23. Why Use str on a docstring?

                          >>> >>> def foo(): print 2
                          +>>> >>> foo()
                           2
                          ->>> >>> foo.__doc__     1
                          ->>> foo.__doc__ == None 2
                          +>>> >>> foo.__doc__     
                          +>>> foo.__doc__ == None 
                           True
                          ->>> str(foo.__doc__)    3
                          +>>> str(foo.__doc__)    
                           'None'
                          -
                          - - - - - - - - - - - - - -
                          1 -You can easily define a function that has no docstring, so its __doc__ attribute is None. Confusingly, if you evaluate the __doc__ attribute directly, the Python IDE prints nothing at all, which makes sense if you think about it, but is still unhelpful. -
                          2 -You can verify that the value of the __doc__ attribute is actually None by comparing it directly. -
                          3 -The str function takes the null value and returns a string representation of it, 'None'. -
                          -
                          - - - - - - -
                          Note
                          In SQL, you must use IS NULL instead of = NULL to compare a null value. In Python, you can use either == None or is None, but is None is faster. -
                          +
                          +
                            +
                          1. You can easily define a function that has no docstring, so its __doc__ attribute is None. Confusingly, if you evaluate the __doc__ attribute directly, the Python IDE prints nothing at all, which makes sense if you think about it, but is still unhelpful. +
                          2. You can verify that the value of the __doc__ attribute is actually None by comparing it directly. +
                          3. The str function takes the null value and returns a string representation of it, 'None'. + + +
                            NoteIn SQL, you must use IS NULL instead of = NULL to compare a null value. In Python, you can use either == None or is None, but is None is faster.

                            Now that you are guaranteed to have a string, you can pass the string to processFunc, which you have already defined as a function that either does or doesn't collapse whitespace. Now you see why it was important to use str to convert a None value into a string representation. processFunc is assuming a string argument and calling its split method, which would crash if you passed it None because None doesn't have a split method.

                            Stepping back even further, you see that you're using string formatting again to concatenate the return value of processFunc with the return value of method's ljust method. This is a new string method that you haven't seen before. -

                            Example 4.24. Introducing ljust

                            >>> s = 'buildConnectionString'
                            ->>> s.ljust(30) 1
                            +

                            Example 4.24. Introducing ljust

                            >>> s = 'buildConnectionString'
                            +>>> s.ljust(30) 
                             'buildConnectionString         '
                            ->>> s.ljust(20) 2
                            -'buildConnectionString'
                            - - - - - - - - - -
                            1 -ljust pads the string with spaces to the given length. This is what the info function uses to make two columns of output and line up all the docstrings in the second column. -
                            2 -If the given length is smaller than the length of the string, ljust will simply return the string unchanged. It never truncates the string. -
                            +>>> s.ljust(20) +'buildConnectionString'
                            +
                              +
                            1. ljust pads the string with spaces to the given length. This is what the info function uses to make two columns of output and line up all the docstrings in the second column. +
                            2. If the given length is smaller than the length of the string, ljust will simply return the string unchanged. It never truncates the string.

                              You're almost finished. Given the padded method name from the ljust method and the (possibly collapsed) docstring from the call to processFunc, you concatenate the two and get a single string. Since you're mapping methodList, you end up with a list of strings. Using the join method of the string "\n", you join this list into a single string, with each element of the list on a separate line, and print the result. -

                              Example 4.25. Printing a List

                              >>> li = ['a', 'b', 'c']
                              ->>> print "\n".join(li) 1
                              -a
                              +

                              Example 4.25. Printing a List

                              >>> li = ['a', 'b', 'c']
                              +>>> print "\n".join(li) 
                              +a
                               b
                              -c
                              - - - - - -
                              1 -This is also a useful debugging trick when you're working with lists. And in Python, you're always working with lists. -
                              +c
                              +
                                +
                              1. This is also a useful debugging trick when you're working with lists. And in Python, you're always working with lists.

                                That's the last piece of the puzzle. You should now understand this code. -

                                +
                                
                                     print "\n".join(["%s %s" %
                                     (method.ljust(spacing),
                                      processFunc(str(getattr(object, method).__doc__)))
                                    for method in methodList])

                                4.9. Summary

                                The apihelper.py program and its output should now make perfect sense. -

                                +
                                
                                 def info(object, spacing=10, collapse=1):
                                     """Print methods and docstrings.
                                     
                                @@ -2135,11 +1523,11 @@ def info(object, spacing=10, collapse=1):
                                    for method in methodList])
                                 
                                 if __name__ == "__main__":
                                -    print info.__doc__
                                -

                                Here is the output of apihelper.py:

                                >>> from apihelper import info
                                ->>> li = []
                                ->>> info(li)
                                -append     L.append(object) -- append object to end
                                +    print info.__doc__
                                +

                                Here is the output of apihelper.py:

                                >>> from apihelper import info
                                +>>> li = []
                                +>>> info(li)
                                +append     L.append(object) -- append object to end
                                 count      L.count(value) -> integer -- return number of occurrences of value
                                 extend     L.extend(list) -- extend list by appending list elements
                                 index      L.index(value) -> integer -- return index of first occurrence of value
                                @@ -2147,9 +1535,9 @@ insert     L.insert(index, object) -- insert object before index
                                 pop        L.pop([index]) -> item -- remove and return item at index (default last)
                                 remove     L.remove(value) -- remove first occurrence of value
                                 reverse    L.reverse() -- reverse *IN PLACE*
                                -sort       L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1
                                +sort L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1

                                Before diving into the next chapter, make sure you're comfortable doing all of these things: -

                                +
                                • Defining and calling functions with optional and named arguments
                                • Using str to coerce any arbitrary value into a string representation @@ -2164,14 +1552,15 @@ sort L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, to advancing your understanding of Python. You'll see more complex applications of this concept throughout this book.
                                -
                                +

                                Chapter 5. Objects and Object-Orientation

                                This chapter, and pretty much every chapter after this, deals with object-oriented Python programming.

                                5.1. Diving In

                                Here is a complete, working Python program. Read the docstrings of the module, the classes, and the functions to get an overview of what this program does and how it works. As usual, don't worry about the stuff you don't understand; that's what the rest of the chapter is for. -

                                Example 5.1. fileinfo.py

                                -

                                If you have not already done so, you can download this and other examples used in this book.

                                +

                                Example 5.1. fileinfo.py

                                +

                                If you have not already done so, you can download this and other examples used in this book. +

                                
                                 """Framework for getting filetype-specific metadata.
                                 
                                 Instantiate appropriate class with filename. Returned object acts like a
                                @@ -2246,21 +1635,16 @@ def listDirectory(directory, fileExtList):
                                     return [getFileInfoClass(f)(f) for f in fileList]           
                                 
                                 if __name__ == "__main__":
                                -    for info in listDirectory("/music/_singles/", [".mp3"]): 1
                                +    for info in listDirectory("/music/_singles/", [".mp3"]): 
                                         print "\n".join(["%s=%s" % (k, v) for k, v in info.items()])
                                -        print
                                - - - - - -
                                1 -This program's output depends on the files on your hard drive. To get meaningful output, you'll need to change the directory + print
                                +
                                  +
                                1. This program's output depends on the files on your hard drive. To get meaningful output, you'll need to change the directory path to point to a directory of MP3 files on your own machine. -
                                -
                                +

                                This is the output I got on my machine. Your output will be different, unless, by some startling coincidence, you share my - exact taste in music.

                                album=
                                +   exact taste in music.
                                +
                                album=
                                 artist=Ghost in the Machine
                                 title=A Time Long Forgotten (Concept
                                 genre=31
                                @@ -2308,65 +1692,34 @@ name=/music/_singles/spinning.mp3
                                 year=2000
                                 comment=http://mp3.com/artists/95/vxp

                                5.2. Importing Modules Using from module import

                                Python has two ways of importing modules. Both are useful, and you should know when to use each. One way, import module, you've already seen in Section 2.4, “Everything Is an Object”. The other way accomplishes the same thing, but it has subtle and important differences. -

                                -

                                Here is the basic from module import syntax:

                                +
                                +

                                Here is the basic from module import syntax:

                                
                                 from UserDict import UserDict
                                 

                                This is similar to the import module syntax that you know and love, but with an important difference: the attributes and methods of the imported module types are imported directly into the local namespace, so they are available directly, without qualification by module name. You -can import individual items or use from module import * to import everything. - - - - - - -
                                Note
                                from module import * in Python is like use module in Perl; import module in Python is like require module in Perl. -
                                - - - - - - -
                                Note
                                from module import * in Python is like import module.* in Java; import module in Python is like import module in Java. -
                                -

                                Example 5.2. import module vs. from module import

                                >>> import types
                                ->>> types.FunctionType             1
                                +can import individual items or use from module import * to import everything.
                                +
                                +
                                +
                                Notefrom module import * in Python is like use module in Perl; import module in Python is like require module in Perl. + + +
                                Notefrom module import * in Python is like import module.* in Java; import module in Python is like import module in Java. +

                                Example 5.2. import module vs. from module import

                                >>> import types
                                +>>> types.FunctionType             
                                 <type 'function'>
                                ->>> FunctionType 2
                                -Traceback (innermost last):
                                +>>> FunctionType 
                                +Traceback (innermost last):
                                   File "<interactive input>", line 1, in ?
                                 NameError: There is no variable named 'FunctionType'
                                ->>> from types import FunctionType 3
                                ->>> FunctionType 4
                                -<type 'function'>
                                - - - - - - - - - - - - - - - - - -
                                1 -The types module contains no methods; it just has attributes for each Python object type. Note that the attribute, FunctionType, must be qualified by the module name, types. -
                                2 -FunctionType by itself has not been defined in this namespace; it exists only in the context of types. -
                                3 -This syntax imports the attribute FunctionType from the types module directly into the local namespace. -
                                4 -Now FunctionType can be accessed directly, without reference to types. -
                                +>>> from types import FunctionType +>>> FunctionType +<type 'function'>
                                +
                                  +
                                1. The types module contains no methods; it just has attributes for each Python object type. Note that the attribute, FunctionType, must be qualified by the module name, types. +
                                2. FunctionType by itself has not been defined in this namespace; it exists only in the context of types. +
                                3. This syntax imports the attribute FunctionType from the types module directly into the local namespace. +
                                4. Now FunctionType can be accessed directly, without reference to types.

                                  When should you use from module import? -

                                  +
                                  • If you will be accessing attributes and methods often and don't want to type the module name over and over, use from module import. @@ -2375,22 +1728,17 @@ NameError: There is no variable named 'FunctionType'
                                  • If the module contains attributes or functions with the same name as ones in your module, you must use import module to avoid name conflicts.
                                  -

                                  Other than that, it's just a matter of style, and you will see Python code written both ways. - - - - -
                                  Caution
                                  Use from module import * sparingly, because it makes it difficult to determine where a particular function or attribute came from, and that makes +

                                  Other than that, it's just a matter of style, and you will see Python code written both ways. + + + - -
                                  CautionUse from module import * sparingly, because it makes it difficult to determine where a particular function or attribute came from, and that makes debugging and refactoring more difficult. -
                                  -

                                  +

                                  Further Reading on Module Importing Techniques

                                  5.3. Defining Classes

                                  @@ -2398,164 +1746,81 @@ NameError: There is no variable named 'FunctionType' classes you've defined.

                                  Defining a class in Python is simple. As with functions, there is no separate interface definition. Just define the class and start coding. A Python class starts with the reserved word class, followed by the class name. Technically, that's all that's required, since a class doesn't need to inherit from any other class. -

                                  Example 5.3. The Simplest Python Class

                                  -class Loaf: 1
                                  -    pass    2 3
                                  - - - - - - - - - - - - - -
                                  1 -The name of this class is Loaf, and it doesn't inherit from any other class. Class names are usually capitalized, EachWordLikeThis, but this is only a convention, not a requirement. -
                                  2 -This class doesn't define any methods or attributes, but syntactically, there needs to be something in the definition, so +

                                  Example 5.3. The Simplest Python Class

                                  
                                  +class Loaf: 
                                  +    pass     
                                  +
                                    +
                                  1. The name of this class is Loaf, and it doesn't inherit from any other class. Class names are usually capitalized, EachWordLikeThis, but this is only a convention, not a requirement. +
                                  2. This class doesn't define any methods or attributes, but syntactically, there needs to be something in the definition, so you use pass. This is a Python reserved word that just means “move along, nothing to see here”. It's a statement that does nothing, and it's a good placeholder when you're stubbing out functions or classes. -
                                  3 -You probably guessed this, but everything in a class is indented, just like the code within a function, if statement, for loop, and so forth. The first thing not indented is not in the class. -
                                  -
                                  - - - - - - -
                                  Note
                                  The pass statement in Python is like an empty set of braces ({}) in Java or C. -
                                  +
                                5. You probably guessed this, but everything in a class is indented, just like the code within a function, if statement, for loop, and so forth. The first thing not indented is not in the class. + + +
                                  NoteThe pass statement in Python is like an empty set of braces ({}) in Java or C.

                                  Of course, realistically, most classes will be inherited from other classes, and they will define their own class methods and attributes. But as you've just seen, there is nothing that a class absolutely must have, other than a name. In particular, C++ programmers may find it odd that Python classes don't have explicit constructors and destructors. Python classes do have something similar to a constructor: the __init__ method. -

                                  Example 5.4. Defining the FileInfo Class

                                  +

                                  Example 5.4. Defining the FileInfo Class

                                  
                                   from UserDict import UserDict
                                   
                                  -class FileInfo(UserDict): 1
                                  - - - - - -
                                  1 -In Python, the ancestor of a class is simply listed in parentheses immediately after the class name. So the FileInfo class is inherited from the UserDict class (which was imported from the UserDict module). UserDict is a class that acts like a dictionary, allowing you to essentially subclass the dictionary datatype and add your own behavior. +class FileInfo(UserDict):
                                  +
                                    +
                                  1. In Python, the ancestor of a class is simply listed in parentheses immediately after the class name. So the FileInfo class is inherited from the UserDict class (which was imported from the UserDict module). UserDict is a class that acts like a dictionary, allowing you to essentially subclass the dictionary datatype and add your own behavior. (There are similar classes UserList and UserString which allow you to subclass lists and strings.) There is a bit of black magic behind this, which you will demystify later in this chapter when you explore the UserDict class in more depth. -
                                  -
                                  - - - - -
                                  Note
                                  In Python, the ancestor of a class is simply listed in parentheses immediately after the class name. There is no special keyword like + + + - -
                                  NoteIn Python, the ancestor of a class is simply listed in parentheses immediately after the class name. There is no special keyword like extends in Java. -

                                  Python supports multiple inheritance. In the parentheses following the class name, you can list as many ancestor classes as you like, separated by commas.

                                  5.3.1. Initializing and Coding Classes

                                  This example shows the initialization of the FileInfo class using the __init__ method. -

                                  Example 5.5. Initializing the FileInfo Class

                                  +

                                  Example 5.5. Initializing the FileInfo Class

                                  
                                   class FileInfo(UserDict):
                                  -    "store file metadata"              1
                                  -    def __init__(self, filename=None): 2 3 4
                                  - - - - - - - - - - - - - - - - - -
                                  1 -Classes can (and should) have docstrings too, just like modules and functions. -
                                  2 -__init__ is called immediately after an instance of the class is created. It would be tempting but incorrect to call this the constructor + "store file metadata" + def __init__(self, filename=None):
                                  +
                                    +
                                  1. Classes can (and should) have docstrings too, just like modules and functions. +
                                  2. __init__ is called immediately after an instance of the class is created. It would be tempting but incorrect to call this the constructor of the class. It's tempting, because it looks like a constructor (by convention, __init__ is the first method defined for the class), acts like one (it's the first piece of code executed in a newly created instance of the class), and even sounds like one (“init” certainly suggests a constructor-ish nature). Incorrect, because the object has already been constructed by the time __init__ is called, and you already have a valid reference to the new instance of the class. But __init__ is the closest thing you're going to get to a constructor in Python, and it fills much the same role. -
                                  3 -The first argument of every class method, including __init__, is always a reference to the current instance of the class. By convention, this argument is always named self. In the __init__ method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called. Although +
                                6. The first argument of every class method, including __init__, is always a reference to the current instance of the class. By convention, this argument is always named self. In the __init__ method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called. Although you need to specify self explicitly when defining the method, you do not specify it when calling the method; Python will add it for you automatically. -
                                7. 4 -__init__ methods can take any number of arguments, and just like functions, the arguments can be defined with default values, making +
                                8. __init__ methods can take any number of arguments, and just like functions, the arguments can be defined with default values, making them optional to the caller. In this case, filename has a default value of None, which is the Python null value. -
                                9. -
                                  - - - - - - -
                                  Note
                                  By convention, the first argument of any Python class method (the reference to the current instance) is called self. This argument fills the role of the reserved word this in C++ or Java, but self is not a reserved word in Python, merely a naming convention. Nonetheless, please don't call it anything but self; this is a very strong convention. -
                                  -

                                  Example 5.6. Coding the FileInfo Class

                                  +
                                  +
                                  +
                                  NoteBy convention, the first argument of any Python class method (the reference to the current instance) is called self. This argument fills the role of the reserved word this in C++ or Java, but self is not a reserved word in Python, merely a naming convention. Nonetheless, please don't call it anything but self; this is a very strong convention. +

                                  Example 5.6. Coding the FileInfo Class

                                  
                                   class FileInfo(UserDict):
                                       "store file metadata"
                                       def __init__(self, filename=None):
                                  -        UserDict.__init__(self)        1
                                  -        self["name"] = filename        2
                                  -3
                                  - - - - - - - - - - - - - -
                                  1 -Some pseudo-object-oriented languages like Powerbuilder have a concept of “extending” constructors and other events, where the ancestor's method is called automatically before the descendant's method is executed. + UserDict.__init__(self) + self["name"] = filename +
                                  +
                                    +
                                  1. Some pseudo-object-oriented languages like Powerbuilder have a concept of “extending” constructors and other events, where the ancestor's method is called automatically before the descendant's method is executed. Python does not do this; you must always explicitly call the appropriate method in the ancestor class. -
                                  2 -I told you that this class acts like a dictionary, and here is the first sign of it. You're assigning the argument filename as the value of this object's name key. -
                                  3 -Note that the __init__ method never returns a value. -
                                  +
                                10. I told you that this class acts like a dictionary, and here is the first sign of it. You're assigning the argument filename as the value of this object's name key. +
                                11. Note that the __init__ method never returns a value.

                                  5.3.2. Knowing When to Use self and __init__

                                  When defining your class methods, you must explicitly list self as the first argument for each method, including __init__. When you call a method of an ancestor class from within your class, you must include the self argument. But when you call your class method from outside, you do not specify anything for the self argument; you skip it entirely, and Python automatically adds the instance reference for you. I am aware that this is confusing at first; it's not really inconsistent, but it may appear inconsistent because it relies on a distinction (between bound and unbound methods) that you don't know about yet.

                                  Whew. I realize that's a lot to absorb, but you'll get the hang of it. All Python classes work the same way, so once you learn one, you've learned them all. If you forget everything else, remember this - one thing, because I promise it will trip you up: - - - - -
                                  Note
                                  __init__ methods are optional, but when you define one, you must remember to explicitly call the ancestor's __init__ method (if it defines one). This is more generally true: whenever a descendant wants to extend the behavior of the ancestor, + one thing, because I promise it will trip you up: + + - -
                                  Note__init__ methods are optional, but when you define one, you must remember to explicitly call the ancestor's __init__ method (if it defines one). This is more generally true: whenever a descendant wants to extend the behavior of the ancestor, the descendant method must explicitly call the ancestor method at the proper time, with the proper arguments. -
                                  -
                                  +

                                  Further Reading on Python Classes

                                    -
                                  • Learning to Program has a gentler introduction to classes. +
                                  • Learning to Program has a gentler introduction to classes. -
                                  • How to Think Like a Computer Scientist shows how to use classes to model compound datatypes. +
                                  • How to Think Like a Computer Scientist shows how to use classes to model compound datatypes. -
                                  • Python Tutorial has an in-depth look at classes, namespaces, and inheritance. +
                                  • Python Tutorial has an in-depth look at classes, namespaces, and inheritance.
                                  • Python Knowledge Base answers common questions about classes. @@ -2563,254 +1828,126 @@ class FileInfo(UserDict):

                                    5.4. Instantiating Classes

                                    Instantiating classes in Python is straightforward. To instantiate a class, simply call the class as if it were a function, passing the arguments that the __init__ method defines. The return value will be the newly created object. -

                                    Example 5.7. Creating a FileInfo Instance

                                    >>> import fileinfo
                                    ->>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3") 1
                                    ->>> f.__class__    2
                                    +

                                    Example 5.7. Creating a FileInfo Instance

                                    >>> import fileinfo
                                    +>>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3") 
                                    +>>> f.__class__    
                                     <class fileinfo.FileInfo at 010EC204>
                                    ->>> f.__doc__      3
                                    +>>> f.__doc__      
                                     'store file metadata'
                                    ->>> f              4
                                    -{'name': '/music/_singles/kairo.mp3'}
                                    - - - - - - - - - - - - - - - - - -
                                    1 -You are creating an instance of the FileInfo class (defined in the fileinfo module) and assigning the newly created instance to the variable f. You are passing one parameter, /music/_singles/kairo.mp3, which will end up as the filename argument in FileInfo's __init__ method. -
                                    2 -Every class instance has a built-in attribute, __class__, which is the object's class. (Note that the representation of this includes the physical address of the instance on my +>>> f +{'name': '/music/_singles/kairo.mp3'}
                                    +
                                      +
                                    1. You are creating an instance of the FileInfo class (defined in the fileinfo module) and assigning the newly created instance to the variable f. You are passing one parameter, /music/_singles/kairo.mp3, which will end up as the filename argument in FileInfo's __init__ method. +
                                    2. Every class instance has a built-in attribute, __class__, which is the object's class. (Note that the representation of this includes the physical address of the instance on my machine; your representation will be different.) Java programmers may be familiar with the Class class, which contains methods like getName and getSuperclass to get metadata information about an object. In Python, this kind of metadata is available directly on the object itself through attributes like __class__, __name__, and __bases__. -
                                    3 -You can access the instance's docstring just as with a function or a module. All instances of a class share the same docstring. -
                                    4 -Remember when the __init__ method assigned its filename argument to self["name"]? Well, here's the result. The arguments you pass when you create the class instance get sent right along to the __init__ method (along with the object reference, self, which Python adds for free). -
                                    -
                                    - - - - - - -
                                    Note
                                    In Python, simply call a class as if it were a function to create a new instance of the class. There is no explicit new operator like C++ or Java. -
                                    +
                                  • You can access the instance's docstring just as with a function or a module. All instances of a class share the same docstring. +
                                  • Remember when the __init__ method assigned its filename argument to self["name"]? Well, here's the result. The arguments you pass when you create the class instance get sent right along to the __init__ method (along with the object reference, self, which Python adds for free). + + +
                                    NoteIn Python, simply call a class as if it were a function to create a new instance of the class. There is no explicit new operator like C++ or Java.

                                    5.4.1. Garbage Collection

                                    If creating new instances is easy, destroying them is even easier. In general, there is no need to explicitly free instances, because they are freed automatically when the variables assigned to them go out of scope. Memory leaks are rare in Python. -

                                    Example 5.8. Trying to Implement a Memory Leak

                                    >>> def leakmem():
                                    -...    f = fileinfo.FileInfo('/music/_singles/kairo.mp3') 1
                                    -...    
                                    ->>> for i in range(100):
                                    -...    leakmem()      2
                                    - - - - - - - - - -
                                    1 -Every time the leakmem function is called, you are creating an instance of FileInfo and assigning it to the variable f, which is a local variable within the function. Then the function ends without ever freeing f, so you would expect a memory leak, but you would be wrong. When the function ends, the local variable f goes out of scope. At this point, there are no longer any references to the newly created instance of FileInfo (since you never assigned it to anything other than f), so Python destroys the instance for us. -
                                    2 -No matter how many times you call the leakmem function, it will never leak memory, because every time, Python will destroy the newly created FileInfo class before returning from leakmem. -
                                    +

                                    Example 5.8. Trying to Implement a Memory Leak

                                    >>> def leakmem():
                                    +...    f = fileinfo.FileInfo('/music/_singles/kairo.mp3') 
                                    +...    
                                    +>>> for i in range(100):
                                    +...    leakmem()      
                                    +
                                      +
                                    1. Every time the leakmem function is called, you are creating an instance of FileInfo and assigning it to the variable f, which is a local variable within the function. Then the function ends without ever freeing f, so you would expect a memory leak, but you would be wrong. When the function ends, the local variable f goes out of scope. At this point, there are no longer any references to the newly created instance of FileInfo (since you never assigned it to anything other than f), so Python destroys the instance for us. +
                                    2. No matter how many times you call the leakmem function, it will never leak memory, because every time, Python will destroy the newly created FileInfo class before returning from leakmem.

                                      The technical term for this form of garbage collection is “reference counting”. Python keeps a list of references to every instance created. In the above example, there was only one reference to the FileInfo instance: the local variable f. When the function ends, the variable f goes out of scope, so the reference count drops to 0, and Python destroys the instance automatically.

                                      In previous versions of Python, there were situations where reference counting failed, and Python couldn't clean up after you. If you created two instances that referenced each other (for instance, a doubly-linked list, where each node has a pointer to the previous and next node in the list), neither instance would ever be destroyed automatically because Python (correctly) believed that there is always a reference to each instance. Python 2.0 has an additional form of garbage collection called “mark-and-sweep” which is smart enough to notice this virtual gridlock and clean up circular references correctly.

                                      As a former philosophy major, it disturbs me to think that things disappear when no one is looking at them, but that's exactly what happens in Python. In general, you can simply forget about memory management and let Python clean up after you. -

                                      +

                                      Further Reading on Garbage Collection

                                      5.5. Exploring UserDict: A Wrapper Class

                                      -

                                      As you've seen, FileInfo is a class that acts like a dictionary. To explore this further, let's look at the UserDict class in the UserDict module, which is the ancestor of the FileInfo class. This is nothing special; the class is written in Python and stored in a .py file, just like any other Python code. In particular, it's stored in the lib directory in your Python installation. - - - - - - -
                                      Tip
                                      In the ActivePython IDE on Windows, you can quickly open any module in your library path by selecting -File->Locate... (Ctrl-L). -
                                      -

                                      Example 5.9. Defining the UserDict Class

                                      -class UserDict:              1
                                      -    def __init__(self, dict=None):             2
                                      -        self.data = {}       3
                                      -        if dict is not None: self.update(dict) 4 5
                                      -
                                      - - - - - - - -
                                      1 -Note that UserDict is a base class, not inherited from any other class. -
                                      2 -This is the __init__ method that you overrode in the FileInfo class. Note that the argument list in this ancestor class is different than the descendant. That's okay; each subclass can have +

                                      As you've seen, FileInfo is a class that acts like a dictionary. To explore this further, let's look at the UserDict class in the UserDict module, which is the ancestor of the FileInfo class. This is nothing special; the class is written in Python and stored in a .py file, just like any other Python code. In particular, it's stored in the lib directory in your Python installation. + + + - - - - - - - - - - - - - -
                                      TipIn the ActivePython IDE on Windows, you can quickly open any module in your library path by selecting +File->Locate... (Ctrl-L). +

                                      Example 5.9. Defining the UserDict Class

                                      
                                      +class UserDict:              
                                      +    def __init__(self, dict=None):             
                                      +        self.data = {}       
                                      +        if dict is not None: self.update(dict)  
                                      +
                                      +
                                        +
                                      1. Note that UserDict is a base class, not inherited from any other class. +
                                      2. This is the __init__ method that you overrode in the FileInfo class. Note that the argument list in this ancestor class is different than the descendant. That's okay; each subclass can have its own set of arguments, as long as it calls the ancestor with the correct arguments. Here the ancestor class has a way to define initial values (by passing a dictionary in the dict argument) which the FileInfo does not use. -
                                      3 -Python supports data attributes (called “instance variables” in Java and Powerbuilder, and “member variables” in C++). Data attributes are pieces of data held by a specific instance of a class. In this case, each instance of UserDict will have a data attribute data. To reference this attribute from code outside the class, you qualify it with the instance name, instance.data, in the same way that you qualify a function with its module name. To reference a data attribute from within the class, +
                                    3. Python supports data attributes (called “instance variables” in Java and Powerbuilder, and “member variables” in C++). Data attributes are pieces of data held by a specific instance of a class. In this case, each instance of UserDict will have a data attribute data. To reference this attribute from code outside the class, you qualify it with the instance name, instance.data, in the same way that you qualify a function with its module name. To reference a data attribute from within the class, you use self as the qualifier. By convention, all data attributes are initialized to reasonable values in the __init__ method. However, this is not required, since data attributes, like local variables, spring into existence when they are first assigned a value. -
                                    4. 4 -The update method is a dictionary duplicator: it copies all the keys and values from one dictionary to another. This does not clear the target dictionary first; if the target dictionary already has some keys, the ones from the source dictionary will +
                                    5. The update method is a dictionary duplicator: it copies all the keys and values from one dictionary to another. This does not clear the target dictionary first; if the target dictionary already has some keys, the ones from the source dictionary will be overwritten, but others will be left untouched. Think of update as a merge function, not a copy function. -
                                    6. 5 -This is a syntax you may not have seen before (I haven't used it in the examples in this book). It's an if statement, but instead of having an indented block starting on the next line, there is just a single statement on the same +
                                    7. This is a syntax you may not have seen before (I haven't used it in the examples in this book). It's an if statement, but instead of having an indented block starting on the next line, there is just a single statement on the same line, after the colon. This is perfectly legal syntax, which is just a shortcut you can use when you have only one statement in a block. (It's like specifying a single statement without braces in C++.) You can use this syntax, or you can have indented code on subsequent lines, but you can't do both for the same block. -
                                    8. - - - - - -
                                      Note
                                      Java and Powerbuilder support function overloading by argument list, i.e. one class can have multiple methods with the same name but a different number of arguments, or arguments of different types. - Other languages (most notably PL/SQL) even support function overloading by argument name; i.e. one class can have multiple methods with the same name and the same number of arguments of the same type but different argument + + + - -
                                      NoteJava and Powerbuilder support function overloading by argument list, i.e. one class can have multiple methods with the same name but a different number of arguments, or arguments of different types. + Other languages (most notably PL/SQL) even support function overloading by argument name; i.e. one class can have multiple methods with the same name and the same number of arguments of the same type but different argument names. Python supports neither of these; it has no form of function overloading whatsoever. Methods are defined solely by their name, and there can be only one method per class with a given name. So if a descendant class has an __init__ method, it always overrides the ancestor __init__ method, even if the descendant defines it with a different argument list. And the same rule applies to any other method. -
                                      - - - - -
                                      Note
                                      Guido, the original author of Python, explains method overriding this way: "Derived classes may override methods of their base classes. Because methods have no + + + - -
                                      NoteGuido, the original author of Python, explains method overriding this way: "Derived classes may override methods of their base classes. Because methods have no special privileges when calling other methods of the same object, a method of a base class that calls another method defined in the same base class, may in fact end up calling a method of a derived class that overrides it. (For C++ programmers: all methods in Python are effectively virtual.)" If that doesn't make sense to you (it confuses the hell out of me), feel free to ignore it. I just thought I'd pass it along. -
                                      - - - - - - -
                                      Caution
                                      Always assign an initial value to all of an instance's data attributes in the __init__ method. It will save you hours of debugging later, tracking down AttributeError exceptions because you're referencing uninitialized (and therefore non-existent) attributes. -
                                      -

                                      Example 5.10. UserDict Normal Methods

                                      -    def clear(self): self.data.clear()          1
                                      -    def copy(self):           2
                                      -        if self.__class__ is UserDict:          3
                                      +
                                      +
                                      +
                                      CautionAlways assign an initial value to all of an instance's data attributes in the __init__ method. It will save you hours of debugging later, tracking down AttributeError exceptions because you're referencing uninitialized (and therefore non-existent) attributes. +

                                      Example 5.10. UserDict Normal Methods

                                      
                                      +    def clear(self): self.data.clear()          
                                      +    def copy(self):           
                                      +        if self.__class__ is UserDict:          
                                                   return UserDict(self.data)         
                                      -        import copy           4
                                      +        import copy           
                                               return copy.copy(self)                 
                                      -    def keys(self): return self.data.keys()     5
                                      +    def keys(self): return self.data.keys()     
                                           def items(self): return self.data.items()  
                                           def values(self): return self.data.values()
                                      -
                                      - - - - - - - - - - - - - - - - - - - - - -
                                      1 -clear is a normal class method; it is publicly available to be called by anyone at any time. Notice that clear, like all class methods, has self as its first argument. (Remember that you don't include self when you call the method; it's something that Python adds for you.) Also note the basic technique of this wrapper class: store a real dictionary (data) as a data attribute, define all the methods that a real dictionary has, and have each class method redirect to the corresponding +
                                      +
                                        +
                                      1. clear is a normal class method; it is publicly available to be called by anyone at any time. Notice that clear, like all class methods, has self as its first argument. (Remember that you don't include self when you call the method; it's something that Python adds for you.) Also note the basic technique of this wrapper class: store a real dictionary (data) as a data attribute, define all the methods that a real dictionary has, and have each class method redirect to the corresponding method on the real dictionary. (In case you'd forgotten, a dictionary's clear method deletes all of its keys and their associated values.) -
                                      2 -The copy method of a real dictionary returns a new dictionary that is an exact duplicate of the original (all the same key-value pairs). +
                                    9. The copy method of a real dictionary returns a new dictionary that is an exact duplicate of the original (all the same key-value pairs). But UserDict can't simply redirect to self.data.copy, because that method returns a real dictionary, and what you want is to return a new instance that is the same class as self. -
                                    10. 3 -You use the __class__ attribute to see if self is a UserDict; if so, you're golden, because you know how to copy a UserDict: just create a new UserDict and give it the real dictionary that you've squirreled away in self.data. Then you immediately return the new UserDict you don't even get to the import copy on the next line. -
                                      4 -If self.__class__ is not UserDict, then self must be some subclass of UserDict (like maybe FileInfo), in which case life gets trickier. UserDict doesn't know how to make an exact copy of one of its descendants; there could, for instance, be other data attributes defined +
                                    11. You use the __class__ attribute to see if self is a UserDict; if so, you're golden, because you know how to copy a UserDict: just create a new UserDict and give it the real dictionary that you've squirreled away in self.data. Then you immediately return the new UserDict you don't even get to the import copy on the next line. +
                                    12. If self.__class__ is not UserDict, then self must be some subclass of UserDict (like maybe FileInfo), in which case life gets trickier. UserDict doesn't know how to make an exact copy of one of its descendants; there could, for instance, be other data attributes defined in the subclass, so you would need to iterate through them and make sure to copy all of them. Luckily, Python comes with a module to do exactly this, and it's called copy. I won't go into the details here (though it's a wicked cool module, if you're ever inclined to dive into it on your own). Suffice it to say that copy can copy arbitrary Python objects, and that's how you're using it here. -
                                    13. 5 -The rest of the methods are straightforward, redirecting the calls to the built-in methods on self.data. -
                                      -
                                      - - - - -
                                      Note
                                      In versions of Python prior to 2.2, you could not directly subclass built-in datatypes like strings, lists, and dictionaries. To compensate for +
                                    14. The rest of the methods are straightforward, redirecting the calls to the built-in methods on self.data. + + + - -
                                      NoteIn versions of Python prior to 2.2, you could not directly subclass built-in datatypes like strings, lists, and dictionaries. To compensate for this, Python comes with wrapper classes that mimic the behavior of these built-in datatypes: UserString, UserList, and UserDict. Using a combination of normal and special methods, the UserDict class does an excellent imitation of a dictionary. In Python 2.2 and later, you can inherit classes directly from built-in datatypes like dict. An example of this is given in the examples that come with this book, in fileinfo_fromdict.py. -

                                      In Python, you can inherit directly from the dict built-in datatype, as shown in this example. There are three differences here compared to the UserDict version. -

                                      Example 5.11. Inheriting Directly from Built-In Datatype dict

                                      -class FileInfo(dict):1
                                      +

                                      Example 5.11. Inheriting Directly from Built-In Datatype dict

                                      
                                      +class FileInfo(dict):
                                           "store file metadata"
                                      -    def __init__(self, filename=None): 2
                                      +    def __init__(self, filename=None): 
                                               self["name"] = filename
                                      -
                                      - - - - - - - - - -
                                      1 -The first difference is that you don't need to import the UserDict module, since dict is a built-in datatype and is always available. The second is that you are inheriting from dict directly, instead of from UserDict.UserDict. -
                                      2 -The third difference is subtle but important. Because of the way UserDict works internally, it requires you to manually call its __init__ method to properly initialize its internal data structures. dict does not work like this; it is not a wrapper, and it requires no explicit initialization. -
                                      -
                                      +
                                      +
                                        +
                                      1. The first difference is that you don't need to import the UserDict module, since dict is a built-in datatype and is always available. The second is that you are inheriting from dict directly, instead of from UserDict.UserDict. +
                                      2. The third difference is subtle but important. Because of the way UserDict works internally, it requires you to manually call its __init__ method to properly initialize its internal data structures. dict does not work like this; it is not a wrapper, and it requires no explicit initialization. +

                                        Further Reading on UserDict

                                        5.6. Special Class Methods

                                        @@ -2820,213 +1957,116 @@ class FileInfo(dict):get and set items with a syntax that doesn't include explicitly invoking methods. This is where special class methods come in: they provide a way to map non-method-calling syntax into method calls.

                                        5.6.1. Getting and Setting Items

                                        -

                                        Example 5.12. The __getitem__ Special Method

                                        -    def __getitem__(self, key): return self.data[key]
                                        >>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3")
                                        ->>> f
                                        +

                                        Example 5.12. The __getitem__ Special Method

                                        
                                        +    def __getitem__(self, key): return self.data[key]
                                        >>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3")
                                        +>>> f
                                         {'name':'/music/_singles/kairo.mp3'}
                                        ->>> f.__getitem__("name") 1
                                        +>>> f.__getitem__("name") 
                                         '/music/_singles/kairo.mp3'
                                        ->>> f["name"]             2
                                        -'/music/_singles/kairo.mp3'
                                        - - - - - - - - - -
                                        1 -The __getitem__ special method looks simple enough. Like the normal methods clear, keys, and values, it just redirects to the dictionary to return its value. But how does it get called? Well, you can call __getitem__ directly, but in practice you wouldn't actually do that; I'm just doing it here to show you how it works. The right way +>>> f["name"] +'/music/_singles/kairo.mp3'
                                        +
                                          +
                                        1. The __getitem__ special method looks simple enough. Like the normal methods clear, keys, and values, it just redirects to the dictionary to return its value. But how does it get called? Well, you can call __getitem__ directly, but in practice you wouldn't actually do that; I'm just doing it here to show you how it works. The right way to use __getitem__ is to get Python to call it for you. -
                                        2 -This looks just like the syntax you would use to get a dictionary value, and in fact it returns the value you would expect. But here's the missing link: under the covers, Python has converted this syntax to the method call f.__getitem__("name"). That's why __getitem__ is a special class method; not only can you call it yourself, you can get Python to call it for you by using the right syntax. -
                                        +
                                      3. This looks just like the syntax you would use to get a dictionary value, and in fact it returns the value you would expect. But here's the missing link: under the covers, Python has converted this syntax to the method call f.__getitem__("name"). That's why __getitem__ is a special class method; not only can you call it yourself, you can get Python to call it for you by using the right syntax.

                                        Of course, Python has a __setitem__ special method to go along with __getitem__, as shown in the next example. -

                                        Example 5.13. The __setitem__ Special Method

                                        -    def __setitem__(self, key, item): self.data[key] = item
                                        >>> f
                                        +

                                        Example 5.13. The __setitem__ Special Method

                                        
                                        +    def __setitem__(self, key, item): self.data[key] = item
                                        >>> f
                                         {'name':'/music/_singles/kairo.mp3'}
                                        ->>> f.__setitem__("genre", 31) 1
                                        ->>> f
                                        +>>> f.__setitem__("genre", 31) 
                                        +>>> f
                                         {'name':'/music/_singles/kairo.mp3', 'genre':31}
                                        ->>> f["genre"] = 32            2
                                        ->>> f
                                        -{'name':'/music/_singles/kairo.mp3', 'genre':32}
                                        - - - - - - - - - -
                                        1 -Like the __getitem__ method, __setitem__ simply redirects to the real dictionary self.data to do its work. And like __getitem__, you wouldn't ordinarily call it directly like this; Python calls __setitem__ for you when you use the right syntax. -
                                        2 -This looks like regular dictionary syntax, except of course that f is really a class that's trying very hard to masquerade as a dictionary, and __setitem__ is an essential part of that masquerade. This line of code actually calls f.__setitem__("genre", 32) under the covers. -
                                        +>>> f["genre"] = 32 +>>> f +{'name':'/music/_singles/kairo.mp3', 'genre':32}
                                        +
                                          +
                                        1. Like the __getitem__ method, __setitem__ simply redirects to the real dictionary self.data to do its work. And like __getitem__, you wouldn't ordinarily call it directly like this; Python calls __setitem__ for you when you use the right syntax. +
                                        2. This looks like regular dictionary syntax, except of course that f is really a class that's trying very hard to masquerade as a dictionary, and __setitem__ is an essential part of that masquerade. This line of code actually calls f.__setitem__("genre", 32) under the covers.

                                          __setitem__ is a special class method because it gets called for you, but it's still a class method. Just as easily as the __setitem__ method was defined in UserDict, you can redefine it in the descendant class to override the ancestor method. This allows you to define classes that act like dictionaries in some ways but define their own behavior above and beyond the built-in dictionary.

                                          This concept is the basis of the entire framework you're studying in this chapter. Each file type can have a handler class that knows how to get metadata from a particular type of file. Once some attributes (like the file's name and location) are known, the handler class knows how to derive other attributes automatically. This is done by overriding the __setitem__ method, checking for particular keys, and adding additional processing when they are found.

                                          For example, MP3FileInfo is a descendant of FileInfo. When an MP3FileInfo's name is set, it doesn't just set the name key (like the ancestor FileInfo does); it also looks in the file itself for MP3 tags and populates a whole set of keys. The next example shows how this works. -

                                          Example 5.14. Overriding __setitem__ in MP3FileInfo

                                          -    def __setitem__(self, key, item):         1
                                          -        if key == "name" and item:            2
                                          -            self.__parse(item)                3
                                          -        FileInfo.__setitem__(self, key, item) 4
                                          - - - - - - - - - - - - - - - - - -
                                          1 -Notice that this __setitem__ method is defined exactly the same way as the ancestor method. This is important, since Python will be calling the method for you, and it expects it to be defined with a certain number of arguments. (Technically speaking, +

                                          Example 5.14. Overriding __setitem__ in MP3FileInfo

                                          
                                          +    def __setitem__(self, key, item):         
                                          +        if key == "name" and item:            
                                          +            self.__parse(item)                
                                          +        FileInfo.__setitem__(self, key, item) 
                                          +
                                            +
                                          1. Notice that this __setitem__ method is defined exactly the same way as the ancestor method. This is important, since Python will be calling the method for you, and it expects it to be defined with a certain number of arguments. (Technically speaking, the names of the arguments don't matter; only the number of arguments is important.) -
                                          2 -Here's the crux of the entire MP3FileInfo class: if you're assigning a value to the name key, you want to do something extra. -
                                          3 -The extra processing you do for names is encapsulated in the __parse method. This is another class method defined in MP3FileInfo, and when you call it, you qualify it with self. Just calling __parse would look for a normal function defined outside the class, which is not what you want. Calling self.__parse will look for a class method defined within the class. This isn't anything new; you reference data attributes the same way. -
                                          4 -After doing this extra processing, you want to call the ancestor method. Remember that this is never done for you in Python; you must do it manually. Note that you're calling the immediate ancestor, FileInfo, even though it doesn't have a __setitem__ method. That's okay, because Python will walk up the ancestor tree until it finds a class with the method you're calling, so this line of code will eventually +
                                        3. Here's the crux of the entire MP3FileInfo class: if you're assigning a value to the name key, you want to do something extra. +
                                        4. The extra processing you do for names is encapsulated in the __parse method. This is another class method defined in MP3FileInfo, and when you call it, you qualify it with self. Just calling __parse would look for a normal function defined outside the class, which is not what you want. Calling self.__parse will look for a class method defined within the class. This isn't anything new; you reference data attributes the same way. +
                                        5. After doing this extra processing, you want to call the ancestor method. Remember that this is never done for you in Python; you must do it manually. Note that you're calling the immediate ancestor, FileInfo, even though it doesn't have a __setitem__ method. That's okay, because Python will walk up the ancestor tree until it finds a class with the method you're calling, so this line of code will eventually find and call the __setitem__ defined in UserDict. -
                                        6. -
                                          - - - - - - -
                                          Note
                                          When accessing data attributes within a class, you need to qualify the attribute name: self.attribute. When calling other methods within a class, you need to qualify the method name: self.method. -
                                          -

                                          Example 5.15. Setting an MP3FileInfo's name

                                          >>> import fileinfo
                                          ->>> mp3file = fileinfo.MP3FileInfo() 1
                                          ->>> mp3file
                                          +
                                          +
                                          +
                                          NoteWhen accessing data attributes within a class, you need to qualify the attribute name: self.attribute. When calling other methods within a class, you need to qualify the method name: self.method. +

                                          Example 5.15. Setting an MP3FileInfo's name

                                          >>> import fileinfo
                                          +>>> mp3file = fileinfo.MP3FileInfo() 
                                          +>>> mp3file
                                           {'name':None}
                                          ->>> mp3file["name"] = "/music/_singles/kairo.mp3"      2
                                          ->>> mp3file
                                          -{'album': 'Rave Mix', 'artist': '***DJ MARY-JANE***', 'genre': 31,
                                          +>>> mp3file["name"] = "/music/_singles/kairo.mp3"      
                                          +>>> mp3file
                                          +{'album': 'Rave Mix', 'artist': '***DJ MARY-JANE***', 'genre': 31,
                                           'title': 'KAIRO****THE BEST GOA', 'name': '/music/_singles/kairo.mp3',
                                           'year': '2000', 'comment': 'http://mp3.com/DJMARYJANE'}
                                          ->>> mp3file["name"] = "/music/_singles/sidewinder.mp3" 3
                                          ->>> mp3file
                                          -{'album': '', 'artist': 'The Cynic Project', 'genre': 18, 'title': 'Sidewinder', 
                                          +>>> mp3file["name"] = "/music/_singles/sidewinder.mp3" 
                                          +>>> mp3file
                                          +{'album': '', 'artist': 'The Cynic Project', 'genre': 18, 'title': 'Sidewinder', 
                                           'name': '/music/_singles/sidewinder.mp3', 'year': '2000', 
                                          -'comment': 'http://mp3.com/cynicproject'}
                                          - - - - - - - - - - - - - -
                                          1 -First, you create an instance of MP3FileInfo, without passing it a filename. (You can get away with this because the filename argument of the __init__ method is optional.) Since MP3FileInfo has no __init__ method of its own, Python walks up the ancestor tree and finds the __init__ method of FileInfo. This __init__ method manually calls the __init__ method of UserDict and then sets the name key to filename, which is None, since you didn't pass a filename. Thus, mp3file initially looks like a dictionary with one key, name, whose value is None. +'comment': 'http://mp3.com/cynicproject'}
                                          +
                                            +
                                          1. First, you create an instance of MP3FileInfo, without passing it a filename. (You can get away with this because the filename argument of the __init__ method is optional.) Since MP3FileInfo has no __init__ method of its own, Python walks up the ancestor tree and finds the __init__ method of FileInfo. This __init__ method manually calls the __init__ method of UserDict and then sets the name key to filename, which is None, since you didn't pass a filename. Thus, mp3file initially looks like a dictionary with one key, name, whose value is None. -
                                          2 -Now the real fun begins. Setting the name key of mp3file triggers the __setitem__ method on MP3FileInfo (not UserDict), which notices that you're setting the name key with a real value and calls self.__parse. Although you haven't traced through the __parse method yet, you can see from the output that it sets several other keys: album, artist, genre, title, year, and comment. +
                                        7. Now the real fun begins. Setting the name key of mp3file triggers the __setitem__ method on MP3FileInfo (not UserDict), which notices that you're setting the name key with a real value and calls self.__parse. Although you haven't traced through the __parse method yet, you can see from the output that it sets several other keys: album, artist, genre, title, year, and comment. -
                                        8. 3 -Modifying the name key will go through the same process again: Python calls __setitem__, which calls self.__parse, which sets all the other keys. +
                                        9. Modifying the name key will go through the same process again: Python calls __setitem__, which calls self.__parse, which sets all the other keys. -
                                        10. 5.7. Advanced Special Class Methods

                                          Python has more special methods than just __getitem__ and __setitem__. Some of them let you emulate functionality that you may not even know about.

                                          This example shows some of the other special methods in UserDict. -

                                          Example 5.16. More Special Methods in UserDict

                                          -    def __repr__(self): return repr(self.data)     1
                                          -    def __cmp__(self, dict):     2
                                          +

                                          Example 5.16. More Special Methods in UserDict

                                          
                                          +    def __repr__(self): return repr(self.data)     
                                          +    def __cmp__(self, dict):     
                                                   if isinstance(dict, UserDict):            
                                                       return cmp(self.data, dict.data)      
                                                   else: 
                                                       return cmp(self.data, dict)           
                                          -    def __len__(self): return len(self.data)       3
                                          -    def __delitem__(self, key): del self.data[key] 4
                                          - - - - - - - - - - - - - - - - - -
                                          1 -__repr__ is a special method that is called when you call repr(instance). The repr function is a built-in function that returns a string representation of an object. It works on any object, not just class + def __len__(self): return len(self.data) + def __delitem__(self, key): del self.data[key]
                                          +
                                            +
                                          1. __repr__ is a special method that is called when you call repr(instance). The repr function is a built-in function that returns a string representation of an object. It works on any object, not just class instances. You're already intimately familiar with repr and you don't even know it. In the interactive window, when you type just a variable name and press the ENTER key, Python uses repr to display the variable's value. Go create a dictionary d with some data and then print repr(d) to see for yourself. -
                                          2 -__cmp__ is called when you compare class instances. In general, you can compare any two Python objects, not just class instances, by using ==. There are rules that define when built-in datatypes are considered equal; for instance, dictionaries are equal when they +
                                        11. __cmp__ is called when you compare class instances. In general, you can compare any two Python objects, not just class instances, by using ==. There are rules that define when built-in datatypes are considered equal; for instance, dictionaries are equal when they have all the same keys and values, and strings are equal when they are the same length and contain the same sequence of characters. For class instances, you can define the __cmp__ method and code the comparison logic yourself, and then you can use == to compare instances of your class and Python will call your __cmp__ special method for you. -
                                        12. 3 -__len__ is called when you call len(instance). The len function is a built-in function that returns the length of an object. It works on any object that could reasonably be thought +
                                        13. __len__ is called when you call len(instance). The len function is a built-in function that returns the length of an object. It works on any object that could reasonably be thought of as having a length. The len of a string is its number of characters; the len of a dictionary is its number of keys; the len of a list or tuple is its number of elements. For class instances, define the __len__ method and code the length calculation yourself, and then call len(instance) and Python will call your __len__ special method for you. -
                                        14. 4 -__delitem__ is called when you call del instance[key], which you may remember as the way to delete individual items from a dictionary. When you use del on a class instance, Python calls the __delitem__ special method for you. -
                                          -
                                          - - - - - - -
                                          Note
                                          In Java, you determine whether two string variables reference the same physical memory location by using str1 == str2. This is called object identity, and it is written in Python as str1 is str2. To compare string values in Java, you would use str1.equals(str2); in Python, you would use str1 == str2. Java programmers who have been taught to believe that the world is a better place because == in Java compares by identity instead of by value may have a difficult time adjusting to Python's lack of such “gotchas”. -
                                          +
                                        15. __delitem__ is called when you call del instance[key], which you may remember as the way to delete individual items from a dictionary. When you use del on a class instance, Python calls the __delitem__ special method for you. + + +
                                          NoteIn Java, you determine whether two string variables reference the same physical memory location by using str1 == str2. This is called object identity, and it is written in Python as str1 is str2. To compare string values in Java, you would use str1.equals(str2); in Python, you would use str1 == str2. Java programmers who have been taught to believe that the world is a better place because == in Java compares by identity instead of by value may have a difficult time adjusting to Python's lack of such “gotchas”.

                                          At this point, you may be thinking, “All this work just to do something in a class that I can do with a built-in datatype.” And it's true that life would be easier (and the entire UserDict class would be unnecessary) if you could inherit from built-in datatypes like a dictionary. But even if you could, special methods would still be useful, because they can be used in any class, not just wrapper classes like UserDict. -

                                          Special methods mean that any class can store key/value pairs like a dictionary, just by defining the __setitem__ method. Any class can act like a sequence, just by defining the __getitem__ method. Any class that defines the __cmp__ method can be compared with ==. And if your class represents something that has a length, don't define a GetLength method; define the __len__ method and use len(instance). - - - - - - -
                                          Note
                                          While other object-oriented languages only let you define the physical model of an object (“this object has a GetLength method”), Python's special class methods like __len__ allow you to define the logical model of an object (“this object has a length”). -
                                          +

                                          Special methods mean that any class can store key/value pairs like a dictionary, just by defining the __setitem__ method. Any class can act like a sequence, just by defining the __getitem__ method. Any class that defines the __cmp__ method can be compared with ==. And if your class represents something that has a length, don't define a GetLength method; define the __len__ method and use len(instance). + + +
                                          NoteWhile other object-oriented languages only let you define the physical model of an object (“this object has a GetLength method”), Python's special class methods like __len__ allow you to define the logical model of an object (“this object has a length”).

                                          Python has a lot of other special methods. There's a whole set of them that let classes act like numbers, allowing you to add, subtract, and do other arithmetic operations on class instances. (The canonical example of this is a class that represents complex numbers, numbers with both real and imaginary components.) The __call__ method lets a class act like a function, allowing you to call a class instance directly. And there are other special methods that allow classes to have read-only and write-only data attributes; you'll talk more about those in later chapters. -

                                          +

                                          Further Reading on Special Class Methods

                                          5.8. Introducing Class Attributes

                                          You already know about data attributes, which are variables owned by a specific instance of a class. Python also supports class attributes, which are variables owned by the class itself. -

                                          Example 5.17. Introducing Class Attributes

                                          +

                                          Example 5.17. Introducing Class Attributes

                                          
                                           class MP3FileInfo(FileInfo):
                                               "store ID3v1.0 MP3 tags"
                                               tagDataMap = {"title"   : (  3,  33, stripnulls),
                                          @@ -3034,118 +2074,66 @@ class MP3FileInfo(FileInfo):
                                           "album"   : ( 63,  93, stripnulls),
                                           "year"    : ( 93,  97, stripnulls),
                                           "comment" : ( 97, 126, stripnulls),
                                          -"genre"   : (127, 128, ord)}
                                          >>> import fileinfo
                                          ->>> fileinfo.MP3FileInfo            1
                                          +"genre"   : (127, 128, ord)}
                                          >>> import fileinfo
                                          +>>> fileinfo.MP3FileInfo            
                                           <class fileinfo.MP3FileInfo at 01257FDC>
                                          ->>> fileinfo.MP3FileInfo.tagDataMap 2
                                          -{'title': (3, 33, <function stripnulls at 0260C8D4>), 
                                          +>>> fileinfo.MP3FileInfo.tagDataMap 
                                          +{'title': (3, 33, <function stripnulls at 0260C8D4>), 
                                           'genre': (127, 128, <built-in function ord>), 
                                           'artist': (33, 63, <function stripnulls at 0260C8D4>), 
                                           'year': (93, 97, <function stripnulls at 0260C8D4>), 
                                           'comment': (97, 126, <function stripnulls at 0260C8D4>), 
                                           'album': (63, 93, <function stripnulls at 0260C8D4>)}
                                          ->>> m = fileinfo.MP3FileInfo()      3
                                          ->>> m.tagDataMap
                                          -{'title': (3, 33, <function stripnulls at 0260C8D4>), 
                                          +>>> m = fileinfo.MP3FileInfo()      
                                          +>>> m.tagDataMap
                                          +{'title': (3, 33, <function stripnulls at 0260C8D4>), 
                                           'genre': (127, 128, <built-in function ord>), 
                                           'artist': (33, 63, <function stripnulls at 0260C8D4>), 
                                           'year': (93, 97, <function stripnulls at 0260C8D4>), 
                                           'comment': (97, 126, <function stripnulls at 0260C8D4>), 
                                          -'album': (63, 93, <function stripnulls at 0260C8D4>)}
                                          - - - - - - - - - - - - - -
                                          1 -MP3FileInfo is the class itself, not any particular instance of the class. -
                                          2 -tagDataMap is a class attribute: literally, an attribute of the class. It is available before creating any instances of the class. -
                                          3 -Class attributes are available both through direct reference to the class and through any instance of the class.
                                          -
                                          - - - - - - -
                                          Note
                                          In Java, both static variables (called class attributes in Python) and instance variables (called data attributes in Python) are defined immediately after the class definition (one with the static keyword, one without). In Python, only class attributes can be defined here; data attributes are defined in the __init__ method. -
                                          -

                                          Class attributes can be used as class-level constants (which is how you use them in MP3FileInfo), but they are not really constants. You can also change them. - - - - - - -
                                          Note
                                          There are no constants in Python. Everything can be changed if you try hard enough. This fits with one of the core principles of Python: bad behavior should be discouraged but not banned. If you really want to change the value of None, you can do it, but don't come running to me when your code is impossible to debug. -
                                          -

                                          Example 5.18. Modifying Class Attributes

                                          >>> class counter:
                                          -...    count = 0   1
                                          -...    def __init__(self):
                                          -...        self.__class__.count += 1 2
                                          -...    
                                          ->>> counter
                                          +'album': (63, 93, <function stripnulls at 0260C8D4>)}
                                          +
                                            +
                                          1. MP3FileInfo is the class itself, not any particular instance of the class. +
                                          2. tagDataMap is a class attribute: literally, an attribute of the class. It is available before creating any instances of the class. +
                                          3. Class attributes are available both through direct reference to the class and through any instance of the class. + + +
                                            NoteIn Java, both static variables (called class attributes in Python) and instance variables (called data attributes in Python) are defined immediately after the class definition (one with the static keyword, one without). In Python, only class attributes can be defined here; data attributes are defined in the __init__ method. +

                                            Class attributes can be used as class-level constants (which is how you use them in MP3FileInfo), but they are not really constants. You can also change them. + + +
                                            NoteThere are no constants in Python. Everything can be changed if you try hard enough. This fits with one of the core principles of Python: bad behavior should be discouraged but not banned. If you really want to change the value of None, you can do it, but don't come running to me when your code is impossible to debug. +

                                            Example 5.18. Modifying Class Attributes

                                            >>> class counter:
                                            +...    count = 0   
                                            +...    def __init__(self):
                                            +...        self.__class__.count += 1 
                                            +...    
                                            +>>> counter
                                             <class __main__.counter at 010EAECC>
                                            ->>> counter.count   3
                                            +>>> counter.count   
                                             0
                                            ->>> c = counter()
                                            ->>> c.count         4
                                            +>>> c = counter()
                                            +>>> c.count         
                                             1
                                            ->>> counter.count
                                            +>>> counter.count
                                             1
                                            ->>> d = counter()   5
                                            ->>> d.count
                                            +>>> d = counter()   
                                            +>>> d.count
                                             2
                                            ->>> c.count
                                            +>>> c.count
                                             2
                                            ->>> counter.count
                                            -2
                                            - - - - - - - - - - - - - - - - - - - - - -
                                            1 -count is a class attribute of the counter class. -
                                            2 -__class__ is a built-in attribute of every class instance (of every class). It is a reference to the class that self is an instance of (in this case, the counter class). -
                                            3 -Because count is a class attribute, it is available through direct reference to the class, before you have created any instances of the +>>> counter.count +2
                                            +
                                              +
                                            1. count is a class attribute of the counter class. +
                                            2. __class__ is a built-in attribute of every class instance (of every class). It is a reference to the class that self is an instance of (in this case, the counter class). +
                                            3. Because count is a class attribute, it is available through direct reference to the class, before you have created any instances of the class. -
                                            4 -Creating an instance of the class calls the __init__ method, which increments the class attribute count by 1. This affects the class itself, not just the newly created instance. -
                                            5 -Creating a second instance will increment the class attribute count again. Notice how the class attribute is shared by the class and all instances of the class. -
                                            +
                                          4. Creating an instance of the class calls the __init__ method, which increments the class attribute count by 1. This affects the class itself, not just the newly created instance. +
                                          5. Creating a second instance will increment the class attribute count again. Notice how the class attribute is shared by the class and all instances of the class.

                                            5.9. Private Functions

                                            Like most languages, Python has the concept of private elements: -

                                            +
                                            • Private functions, which can't be called from outside their module
                                            • Private class methods, which can't be called from outside their class @@ -3156,44 +2144,33 @@ class MP3FileInfo(FileInfo): public. Python has no concept of protected class methods (accessible only in their own class and descendant classes). Class methods are either private (accessible only in their own class) or public (accessible from anywhere).

                                              In MP3FileInfo, there are two methods: __parse and __setitem__. As you have already discussed, __setitem__ is a special method; normally, you would call it indirectly by using the dictionary syntax on a class instance, but it is public, and you could -call it directly (even from outside the fileinfo module) if you had a really good reason. However, __parse is private, because it has two underscores at the beginning of its name. - - - - -
                                              Note
                                              In Python, all special methods (like __setitem__) and built-in attributes (like __doc__) follow a standard naming convention: they both start with and end with two underscores. Don't name your own methods and +call it directly (even from outside the fileinfo module) if you had a really good reason. However, __parse is private, because it has two underscores at the beginning of its name. + + + - -
                                              NoteIn Python, all special methods (like __setitem__) and built-in attributes (like __doc__) follow a standard naming convention: they both start with and end with two underscores. Don't name your own methods and attributes this way, because it will only confuse you (and others) later. -
                                              -

                                              Example 5.19. Trying to Call a Private Method

                                              >>> import fileinfo
                                              ->>> m = fileinfo.MP3FileInfo()
                                              ->>> m.__parse("/music/_singles/kairo.mp3") 1
                                              -Traceback (innermost last):
                                              +

                                              Example 5.19. Trying to Call a Private Method

                                              >>> import fileinfo
                                              +>>> m = fileinfo.MP3FileInfo()
                                              +>>> m.__parse("/music/_singles/kairo.mp3") 
                                              +Traceback (innermost last):
                                                 File "<interactive input>", line 1, in ?
                                              -AttributeError: 'MP3FileInfo' instance has no attribute '__parse'
                                              - - - - - -
                                              1 -If you try to call a private method, Python will raise a slightly misleading exception, saying that the method does not exist. Of course it does exist, but it's private, +AttributeError: 'MP3FileInfo' instance has no attribute '__parse'
                                              +
                                                +
                                              1. If you try to call a private method, Python will raise a slightly misleading exception, saying that the method does not exist. Of course it does exist, but it's private, so it's not accessible outside the class.Strictly speaking, private methods are accessible outside their class, just not easily accessible. Nothing in Python is truly private; internally, the names of private methods and attributes are mangled and unmangled on the fly to make them seem inaccessible by their given names. You can access the __parse method of the MP3FileInfo class by the name _MP3FileInfo__parse. Acknowledge that this is interesting, but promise to never, ever do it in real code. Private methods are private for a reason, but like many other things in Python, their privateness is ultimately a matter of convention, not force. -
                                              -
                                              +

                                              Further Reading on Private Functions

                                              5.10. Summary

                                              That's it for the hard-core object trickery. You'll see a real-world application of special class methods in Chapter 12, which uses getattr to create a proxy to a remote web service.

                                              The next chapter will continue using this code sample to explore other Python concepts, such as exceptions, file objects, and for loops.

                                              Before diving into the next chapter, make sure you're comfortable doing all of these things: -

                                              +
                                              Defining private attributes and methods -
                                              +

                                              Chapter 6. Exceptions and File Handling

                                              In this chapter, you will dive into exceptions, file objects, for loops, and the os and sys modules. If you've used exceptions in another programming language, you can skim the first section to get a sense of Python's syntax. Be sure to tune in again for file handling.

                                              6.1. Handling Exceptions

                                              -

                                              Like many other programming languages, Python has exception handling via try...except blocks. - - - - - - -
                                              Note
                                              Python uses try...except to handle exceptions and raise to generate them. Java and C++ use try...catch to handle exceptions, and throw to generate them. -
                                              +

                                              Like many other programming languages, Python has exception handling via try...except blocks. + + +
                                              NotePython uses try...except to handle exceptions and raise to generate them. Java and C++ use try...catch to handle exceptions, and throw to generate them.

                                              Exceptions are everywhere in Python. Virtually every module in the standard Python library uses them, and Python itself will raise them in a lot of different circumstances. You've already seen them repeatedly throughout this book. -

                                              +
                                              • Accessing a non-existent dictionary key will raise a KeyError exception. @@ -3239,44 +2211,23 @@ way back to the default behavior built in to Python, which is to spit out some d many times, an exception is something you can anticipate. If you're opening a file, it might not exist. If you're connecting to a database, it might be unavailable, or you might not have the correct security credentials to access it. If you know a line of code may raise an exception, you should handle the exception using a try...except block. -

                                                Example 6.1. Opening a Non-Existent File

                                                >>> fsock = open("/notthere", "r")      1
                                                -Traceback (innermost last):
                                                +

                                                Example 6.1. Opening a Non-Existent File

                                                >>> fsock = open("/notthere", "r")      
                                                +Traceback (innermost last):
                                                   File "<interactive input>", line 1, in ?
                                                 IOError: [Errno 2] No such file or directory: '/notthere'
                                                ->>> try:
                                                -...    fsock = open("/notthere")       2
                                                -... except IOError:   3
                                                -...    print "The file does not exist, exiting gracefully"
                                                -... print "This line will always print" 4
                                                -The file does not exist, exiting gracefully
                                                -This line will always print
                                                - - - - - - - - - - - - - - - - - -
                                                1 -Using the built-in open function, you can try to open a file for reading (more on open in the next section). But the file doesn't exist, so this raises the IOError exception. Since you haven't provided any explicit check for an IOError exception, Python just prints out some debugging information about what happened and then gives up. -
                                                2 -You're trying to open the same non-existent file, but this time you're doing it within a try...except block. -
                                                3 -When the open method raises an IOError exception, you're ready for it. The except IOError: line catches the exception and executes your own block of code, which in this case just prints a more pleasant error message. -
                                                4 -Once an exception has been handled, processing continues normally on the first line after the try...except block. Note that this line will always print, whether or not an exception occurs. If you really did have a file called +>>> try: +... fsock = open("/notthere") +... except IOError: +... print "The file does not exist, exiting gracefully" +... print "This line will always print" +The file does not exist, exiting gracefully +This line will always print
                                                +
                                                  +
                                                1. Using the built-in open function, you can try to open a file for reading (more on open in the next section). But the file doesn't exist, so this raises the IOError exception. Since you haven't provided any explicit check for an IOError exception, Python just prints out some debugging information about what happened and then gives up. +
                                                2. You're trying to open the same non-existent file, but this time you're doing it within a try...except block. +
                                                3. When the open method raises an IOError exception, you're ready for it. The except IOError: line catches the exception and executes your own block of code, which in this case just prints a more pleasant error message. +
                                                4. Once an exception has been handled, processing continues normally on the first line after the try...except block. Note that this line will always print, whether or not an exception occurs. If you really did have a file called notthere in your root directory, the call to open would succeed, the except clause would be ignored, and this line would still be executed. -

                                                Exceptions may seem unfriendly (after all, if you don't catch the exception, your entire program will crash), but consider the alternative. Would you rather get back an unusable file object to a non-existent file? You'd need to check its validity somehow anyway, and if you forgot, somewhere down the line, your program would give you strange errors somewhere down the @@ -3289,281 +2240,151 @@ exceptions, errors occur immediately, and you can handle them in a standard way

                                                You can also define your own exceptions by creating a class that inherits from the built-in Exception class, and then raise your exceptions with the raise command. See the further reading section if you're interested in doing this.

                                                The next example demonstrates how to use an exception to support platform-specific functionality. This code comes from the getpass module, a wrapper module for getting a password from the user. Getting a password is accomplished differently on UNIX, Windows, and Mac OS platforms, but this code encapsulates all of those differences. -

                                                Example 6.2. Supporting Platform-Specific Functionality

                                                +

                                                Example 6.2. Supporting Platform-Specific Functionality

                                                
                                                   # Bind the name getpass to the appropriate function
                                                   try:
                                                -      import termios, TERMIOS   1
                                                +      import termios, TERMIOS   
                                                   except ImportError:
                                                       try:
                                                -          import msvcrt         2
                                                +          import msvcrt         
                                                       except ImportError:
                                                           try:
                                                -              from EasyDialogs import AskPassword 3
                                                +              from EasyDialogs import AskPassword 
                                                           except ImportError:
                                                -              getpass = default_getpass           4
                                                -          else:                 5
                                                +              getpass = default_getpass           
                                                +          else:                 
                                                               getpass = AskPassword
                                                       else:
                                                           getpass = win_getpass
                                                   else:
                                                -      getpass = unix_getpass
                                                - - - - - - - - - - - - - - - - - - - - - -
                                                1 -termios is a UNIX-specific module that provides low-level control over the input terminal. If this module is not available (because it's not + getpass = unix_getpass
                                                +
                                                  +
                                                1. termios is a UNIX-specific module that provides low-level control over the input terminal. If this module is not available (because it's not on your system, or your system doesn't support it), the import fails and Python raises an ImportError, which you catch. -
                                                2 -OK, you didn't have termios, so let's try msvcrt, which is a Windows-specific module that provides an API to many useful functions in the Microsoft Visual C++ runtime services. If this import fails, Python will raise an ImportError, which you catch. -
                                                3 -If the first two didn't work, you try to import a function from EasyDialogs, which is a Mac OS-specific module that provides functions to pop up dialog boxes of various types. Once again, if this import fails, Python will raise an ImportError, which you catch. -
                                                4 -None of these platform-specific modules is available (which is possible, since Python has been ported to a lot of different platforms), so you need to fall back on a default password input function (which is +
                                              • OK, you didn't have termios, so let's try msvcrt, which is a Windows-specific module that provides an API to many useful functions in the Microsoft Visual C++ runtime services. If this import fails, Python will raise an ImportError, which you catch. +
                                              • If the first two didn't work, you try to import a function from EasyDialogs, which is a Mac OS-specific module that provides functions to pop up dialog boxes of various types. Once again, if this import fails, Python will raise an ImportError, which you catch. +
                                              • None of these platform-specific modules is available (which is possible, since Python has been ported to a lot of different platforms), so you need to fall back on a default password input function (which is defined elsewhere in the getpass module). Notice what you're doing here: assigning the function default_getpass to the variable getpass. If you read the official getpass documentation, it tells you that the getpass module defines a getpass function. It does this by binding getpass to the correct function for your platform. Then when you call the getpass function, you're really calling a platform-specific function that this code has set up for you. You don't need to know or care which platform your code is running on -- just call getpass, and it will always do the right thing. -
                                              • 5 -A try...except block can have an else clause, like an if statement. If no exception is raised during the try block, the else clause is executed afterwards. In this case, that means that the from EasyDialogs import AskPassword import worked, so you should bind getpass to the AskPassword function. Each of the other try...except blocks has similar else clauses to bind getpass to the appropriate function when you find an import that works. -
                                                -
                                                +
                                              • A try...except block can have an else clause, like an if statement. If no exception is raised during the try block, the else clause is executed afterwards. In this case, that means that the from EasyDialogs import AskPassword import worked, so you should bind getpass to the AskPassword function. Each of the other try...except blocks has similar else clauses to bind getpass to the appropriate function when you find an import that works. +

                                                Further Reading on Exception Handling

                                                6.2. Working with File Objects

                                                Python has a built-in function, open, for opening a file on disk. open returns a file object, which has methods and attributes for getting information about and manipulating the opened file. -

                                                Example 6.3. Opening a File

                                                >>> f = open("/music/_singles/kairo.mp3", "rb") 1
                                                ->>> f       2
                                                +

                                                Example 6.3. Opening a File

                                                >>> f = open("/music/_singles/kairo.mp3", "rb") 
                                                +>>> f       
                                                 <open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
                                                ->>> f.mode  3
                                                +>>> f.mode  
                                                 'rb'
                                                ->>> f.name  4
                                                -'/music/_singles/kairo.mp3'
                                                - - - - - - - - - - - - - - - - - -
                                                1 -The open method can take up to three parameters: a filename, a mode, and a buffering parameter. Only the first one, the filename, +>>> f.name +'/music/_singles/kairo.mp3'
                                                +
                                                  +
                                                1. The open method can take up to three parameters: a filename, a mode, and a buffering parameter. Only the first one, the filename, is required; the other two are optional. If not specified, the file is opened for reading in text mode. Here you are opening the file for reading in binary mode. (print open.__doc__ displays a great explanation of all the possible modes.) -
                                                2 -The open function returns an object (by now, this should not surprise you). A file object has several useful attributes. -
                                                3 -The mode attribute of a file object tells you in which mode the file was opened. -
                                                4 -The name attribute of a file object tells you the name of the file that the file object has open. -
                                                +
                                              • The open function returns an object (by now, this should not surprise you). A file object has several useful attributes. +
                                              • The mode attribute of a file object tells you in which mode the file was opened. +
                                              • The name attribute of a file object tells you the name of the file that the file object has open.

                                                6.2.1. Reading Files

                                                After you open a file, the first thing you'll want to do is read from it, as shown in the next example. -

                                                Example 6.4. Reading a File

                                                ->>> f
                                                +

                                                Example 6.4. Reading a File

                                                +>>> f
                                                 <open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
                                                ->>> f.tell()              1
                                                +>>> f.tell()              
                                                 0
                                                ->>> f.seek(-128, 2)       2
                                                ->>> f.tell()              3
                                                +>>> f.seek(-128, 2)       
                                                +>>> f.tell()              
                                                 7542909
                                                ->>> tagData = f.read(128) 4
                                                ->>> tagData
                                                -'TAGKAIRO****THE BEST GOA         ***DJ MARY-JANE***            
                                                +>>> tagData = f.read(128) 
                                                +>>> tagData
                                                +'TAGKAIRO****THE BEST GOA         ***DJ MARY-JANE***            
                                                 Rave Mix    2000http://mp3.com/DJMARYJANE     \037'
                                                ->>> f.tell()              5
                                                -7543037
                                                - - - - - - - - - - - - - - - - - - - - - -
                                                1 -A file object maintains state about the file it has open. The tell method of a file object tells you your current position in the open file. Since you haven't done anything with this file +>>> f.tell() +7543037
                                                +
                                                  +
                                                1. A file object maintains state about the file it has open. The tell method of a file object tells you your current position in the open file. Since you haven't done anything with this file yet, the current position is 0, which is the beginning of the file. -
                                                2 -The seek method of a file object moves to another position in the open file. The second parameter specifies what the first one means; +
                                              • The seek method of a file object moves to another position in the open file. The second parameter specifies what the first one means; 0 means move to an absolute position (counting from the start of the file), 1 means move to a relative position (counting from the current position), and 2 means move to a position relative to the end of the file. Since the MP3 tags you're looking for are stored at the end of the file, you use 2 and tell the file object to move to a position 128 bytes from the end of the file. -
                                              • 3 -The tell method confirms that the current file position has moved. -
                                                4 -The read method reads a specified number of bytes from the open file and returns a string with the data that was read. The optional +
                                              • The tell method confirms that the current file position has moved. +
                                              • The read method reads a specified number of bytes from the open file and returns a string with the data that was read. The optional parameter specifies the maximum number of bytes to read. If no parameter is specified, read will read until the end of the file. (You could have simply said read() here, since you know exactly where you are in the file and you are, in fact, reading the last 128 bytes.) The read data is assigned to the tagData variable, and the current position is updated based on how many bytes were read. -
                                              • 5 -The tell method confirms that the current position has moved. If you do the math, you'll see that after reading 128 bytes, the position +
                                              • The tell method confirms that the current position has moved. If you do the math, you'll see that after reading 128 bytes, the position has been incremented by 128. -
                                              • 6.2.2. Closing Files

                                                Open files consume system resources, and depending on the file mode, other programs may not be able to access them. It's important to close files as soon as you're finished with them. -

                                                Example 6.5. Closing a File

                                                ->>> f
                                                +

                                                Example 6.5. Closing a File

                                                +>>> f
                                                 <open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
                                                ->>> f.closed       1
                                                +>>> f.closed       
                                                 False
                                                ->>> f.close()      2
                                                ->>> f
                                                +>>> f.close()      
                                                +>>> f
                                                 <closed file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
                                                ->>> f.closed       3
                                                +>>> f.closed       
                                                 True
                                                ->>> f.seek(0)      4
                                                -Traceback (innermost last):
                                                +>>> f.seek(0)      
                                                +Traceback (innermost last):
                                                   File "<interactive input>", line 1, in ?
                                                 ValueError: I/O operation on closed file
                                                ->>> f.tell()
                                                -Traceback (innermost last):
                                                +>>> f.tell()
                                                +Traceback (innermost last):
                                                   File "<interactive input>", line 1, in ?
                                                 ValueError: I/O operation on closed file
                                                ->>> f.read()
                                                -Traceback (innermost last):
                                                +>>> f.read()
                                                +Traceback (innermost last):
                                                   File "<interactive input>", line 1, in ?
                                                 ValueError: I/O operation on closed file
                                                ->>> f.close()      5
                                                - - - - - - - - - - - - - - - - - - - - - -
                                                1 -The closed attribute of a file object indicates whether the object has a file open or not. In this case, the file is still open (closed is False). -
                                                2 -To close a file, call the close method of the file object. This frees the lock (if any) that you were holding on the file, flushes buffered writes (if any) +>>> f.close()
                                                +
                                                  +
                                                1. The closed attribute of a file object indicates whether the object has a file open or not. In this case, the file is still open (closed is False). +
                                                2. To close a file, call the close method of the file object. This frees the lock (if any) that you were holding on the file, flushes buffered writes (if any) that the system hadn't gotten around to actually writing yet, and releases the system resources. -
                                                3 -The closed attribute confirms that the file is closed. -
                                                4 -Just because a file is closed doesn't mean that the file object ceases to exist. The variable f will continue to exist until it goes out of scope or gets manually deleted. However, none of the methods that manipulate an open file will work once the file has been closed; +
                                              • The closed attribute confirms that the file is closed. +
                                              • Just because a file is closed doesn't mean that the file object ceases to exist. The variable f will continue to exist until it goes out of scope or gets manually deleted. However, none of the methods that manipulate an open file will work once the file has been closed; they all raise an exception. -
                                              • 5 -Calling close on a file object whose file is already closed does not raise an exception; it fails silently. -
                                                +
                                              • Calling close on a file object whose file is already closed does not raise an exception; it fails silently.

                                                6.2.3. Handling I/O Errors

                                                Now you've seen enough to understand the file handling code in the fileinfo.py sample code from teh previous chapter. This example shows how to safely open and read from a file and gracefully handle errors. -

                                                Example 6.6. File Objects in MP3FileInfo

                                                -        try:              1
                                                -            fsock = open(filename, "rb", 0) 2
                                                +

                                                Example 6.6. File Objects in MP3FileInfo

                                                
                                                +        try:              
                                                +            fsock = open(filename, "rb", 0) 
                                                             try:         
                                                -                fsock.seek(-128, 2)         3
                                                -                tagdata = fsock.read(128)   4
                                                -            finally:      5
                                                +                fsock.seek(-128, 2)         
                                                +                tagdata = fsock.read(128)   
                                                +            finally:      
                                                                 fsock.close()              
                                                             .
                                                             .
                                                             .
                                                -        except IOError:   6
                                                -            pass         
                                                - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                1 -Because opening and reading files is risky and may raise an exception, all of this code is wrapped in a try...except block. (Hey, isn't standardized indentation great? This is where you start to appreciate it.) -
                                                2 -The open function may raise an IOError. (Maybe the file doesn't exist.) -
                                                3 -The seek method may raise an IOError. (Maybe the file is smaller than 128 bytes.) -
                                                4 -The read method may raise an IOError. (Maybe the disk has a bad sector, or it's on a network drive and the network just went down.) -
                                                5 -This is new: a try...finally block. Once the file has been opened successfully by the open function, you want to make absolutely sure that you close it, even if an exception is raised by the seek or read methods. That's what a try...finally block is for: code in the finally block will always be executed, even if something in the try block raises an exception. Think of it as code that gets executed on the way out, regardless of what happened before. -
                                                6 -At last, you handle your IOError exception. This could be the IOError exception raised by the call to open, seek, or read. Here, you really don't care, because all you're going to do is ignore it silently and continue. (Remember, pass is a Python statement that does nothing.) That's perfectly legal; “handling” an exception can mean explicitly doing nothing. It still counts as handled, and processing will continue normally on the + except IOError: + pass
                                                +
                                                  +
                                                1. Because opening and reading files is risky and may raise an exception, all of this code is wrapped in a try...except block. (Hey, isn't standardized indentation great? This is where you start to appreciate it.) +
                                                2. The open function may raise an IOError. (Maybe the file doesn't exist.) +
                                                3. The seek method may raise an IOError. (Maybe the file is smaller than 128 bytes.) +
                                                4. The read method may raise an IOError. (Maybe the disk has a bad sector, or it's on a network drive and the network just went down.) +
                                                5. This is new: a try...finally block. Once the file has been opened successfully by the open function, you want to make absolutely sure that you close it, even if an exception is raised by the seek or read methods. That's what a try...finally block is for: code in the finally block will always be executed, even if something in the try block raises an exception. Think of it as code that gets executed on the way out, regardless of what happened before. +
                                                6. At last, you handle your IOError exception. This could be the IOError exception raised by the call to open, seek, or read. Here, you really don't care, because all you're going to do is ignore it silently and continue. (Remember, pass is a Python statement that does nothing.) That's perfectly legal; “handling” an exception can mean explicitly doing nothing. It still counts as handled, and processing will continue normally on the next line of code after the try...except block. -

                                                6.2.4. Writing to Files

                                                As you would expect, you can also write to files in much the same way that you read from them. There are two basic file modes: -

                                                +
                                                • "Append" mode will add data to the end of the file.
                                                • "write" mode will overwrite the file. @@ -3571,215 +2392,130 @@ ValueError: I/O operation on closed file

                                                  Either mode will create the file automatically if it doesn't already exist, so there's never a need for any sort of fiddly "if the log file doesn't exist yet, create a new empty file just so you can open it for the first time" logic. Just open it and start writing. -

                                                  Example 6.7. Writing to Files

                                                  ->>> logfile = open('test.log', 'w') 1
                                                  ->>> logfile.write('test succeeded') 2
                                                  ->>> logfile.close()
                                                  ->>> print file('test.log').read()   3
                                                  +

                                                  Example 6.7. Writing to Files

                                                  +>>> logfile = open('test.log', 'w') 
                                                  +>>> logfile.write('test succeeded') 
                                                  +>>> logfile.close()
                                                  +>>> print file('test.log').read()   
                                                   test succeeded
                                                  ->>> logfile = open('test.log', 'a') 4
                                                  ->>> logfile.write('line 2')
                                                  ->>> logfile.close()
                                                  ->>> print file('test.log').read()   5
                                                  +>>> logfile = open('test.log', 'a') 
                                                  +>>> logfile.write('line 2')
                                                  +>>> logfile.close()
                                                  +>>> print file('test.log').read()   
                                                   test succeededline 2
                                                  -
                                                  - - - - - - - - - - - - - - - - - - - - - -
                                                  1 -You start boldly by creating either the new file test.log or overwrites the existing file, and opening the file for writing. (The second parameter "w" means open the file for writing.) Yes, that's all as dangerous as it sounds. I hope you didn't care about the previous +
                                                  +
                                                    +
                                                  1. You start boldly by creating either the new file test.log or overwrites the existing file, and opening the file for writing. (The second parameter "w" means open the file for writing.) Yes, that's all as dangerous as it sounds. I hope you didn't care about the previous contents of that file, because it's gone now. -
                                                  2 -You can add data to the newly opened file with the write method of the file object returned by open. -
                                                  3 -file is a synonym for open. This one-liner opens the file, reads its contents, and prints them. -
                                                  4 -You happen to know that test.log exists (since you just finished writing to it), so you can open it and append to it. (The "a" parameter means open the file for appending.) Actually you could do this even if the file didn't exist, because opening +
                                                • You can add data to the newly opened file with the write method of the file object returned by open. +
                                                • file is a synonym for open. This one-liner opens the file, reads its contents, and prints them. +
                                                • You happen to know that test.log exists (since you just finished writing to it), so you can open it and append to it. (The "a" parameter means open the file for appending.) Actually you could do this even if the file didn't exist, because opening the file for appending will create the file if necessary. But appending will never harm the existing contents of the file. -
                                                • 5 -As you can see, both the original line you wrote and the second line you appended are now in test.log. Also note that carriage returns are not included. Since you didn't write them explicitly to the file either time, the +
                                                • As you can see, both the original line you wrote and the second line you appended are now in test.log. Also note that carriage returns are not included. Since you didn't write them explicitly to the file either time, the file doesn't include them. You can write a carriage return with the "\n" character. Since you didn't do this, everything you wrote to the file ended up smooshed together on the same line. -
                                                • -
                                                  +

                                                  Further Reading on File Handling

                                                  6.3. Iterating with for Loops

                                                  Like most other languages, Python has for loops. The only reason you haven't seen them until now is that Python is good at so many other things that you don't need them as often.

                                                  Most other languages don't have a powerful list datatype like Python, so you end up doing a lot of manual work, specifying a start, end, and step to define a range of integers or characters or other iteratable entities. But in Python, a for loop simply iterates over a list, the same way list comprehensions work. -

                                                  Example 6.8. Introducing the for Loop

                                                  >>> li = ['a', 'b', 'e']
                                                  ->>> for s in li:         1
                                                  -...    print s          2
                                                  -a
                                                  +

                                                  Example 6.8. Introducing the for Loop

                                                  >>> li = ['a', 'b', 'e']
                                                  +>>> for s in li:         
                                                  +...    print s          
                                                  +a
                                                   b
                                                   e
                                                  ->>> print "\n".join(li)  3
                                                  -a
                                                  +>>> print "\n".join(li)  
                                                  +a
                                                   b
                                                  -e
                                                  - - - - - - - - - - - - - -
                                                  1 -The syntax for a for loop is similar to list comprehensions. li is a list, and s will take the value of each element in turn, starting from the first element. -
                                                  2 -Like an if statement or any other indented block, a for loop can have any number of lines of code in it. -
                                                  3 -This is the reason you haven't seen the for loop yet: you haven't needed it yet. It's amazing how often you use for loops in other languages when all you really want is a join or a list comprehension. -
                                                  +e
                                                  +
                                                    +
                                                  1. The syntax for a for loop is similar to list comprehensions. li is a list, and s will take the value of each element in turn, starting from the first element. +
                                                  2. Like an if statement or any other indented block, a for loop can have any number of lines of code in it. +
                                                  3. This is the reason you haven't seen the for loop yet: you haven't needed it yet. It's amazing how often you use for loops in other languages when all you really want is a join or a list comprehension.

                                                    Doing a “normal” (by Visual Basic standards) counter for loop is also simple. -

                                                    Example 6.9. Simple Counters

                                                    ->>> for i in range(5):             1
                                                    -...    print i
                                                    -0
                                                    +

                                                    Example 6.9. Simple Counters

                                                    +>>> for i in range(5):             
                                                    +...    print i
                                                    +0
                                                     1
                                                     2
                                                     3
                                                     4
                                                    ->>> li = ['a', 'b', 'c', 'd', 'e']
                                                    ->>> for i in range(len(li)):       2
                                                    -...    print li[i]
                                                    -a
                                                    +>>> li = ['a', 'b', 'c', 'd', 'e']
                                                    +>>> for i in range(len(li)):       
                                                    +...    print li[i]
                                                    +a
                                                     b
                                                     c
                                                     d
                                                     e
                                                    -
                                                    - - - - - - - - - -
                                                    1 -As you saw in Example 3.20, “Assigning Consecutive Values”, range produces a list of integers, which you then loop through. I know it looks a bit odd, but it is occasionally (and I stress +
                                                    +
                                                      +
                                                    1. As you saw in Example 3.20, “Assigning Consecutive Values”, range produces a list of integers, which you then loop through. I know it looks a bit odd, but it is occasionally (and I stress occasionally) useful to have a counter loop. -
                                                    2 -Don't ever do this. This is Visual Basic-style thinking. Break out of it. Just iterate through the list, as shown in the previous example. -
                                                    +
                                                  4. Don't ever do this. This is Visual Basic-style thinking. Break out of it. Just iterate through the list, as shown in the previous example.

                                                    for loops are not just for simple counters. They can iterate through all kinds of things. Here is an example of using a for loop to iterate through a dictionary. -

                                                    Example 6.10. Iterating Through a Dictionary

                                                    ->>> import os
                                                    ->>> for k, v in os.environ.items():      1 2
                                                    -...    print "%s=%s" % (k, v)
                                                    -USERPROFILE=C:\Documents and Settings\mpilgrim
                                                    +

                                                    Example 6.10. Iterating Through a Dictionary

                                                    +>>> import os
                                                    +>>> for k, v in os.environ.items():       
                                                    +...    print "%s=%s" % (k, v)
                                                    +USERPROFILE=C:\Documents and Settings\mpilgrim
                                                     OS=Windows_NT
                                                     COMPUTERNAME=MPILGRIM
                                                     USERNAME=mpilgrim
                                                     
                                                     [...snip...]
                                                    ->>> print "\n".join(["%s=%s" % (k, v)
                                                    -...    for k, v in os.environ.items()]) 3
                                                    -USERPROFILE=C:\Documents and Settings\mpilgrim
                                                    +>>> print "\n".join(["%s=%s" % (k, v)
                                                    +...    for k, v in os.environ.items()]) 
                                                    +USERPROFILE=C:\Documents and Settings\mpilgrim
                                                     OS=Windows_NT
                                                     COMPUTERNAME=MPILGRIM
                                                     USERNAME=mpilgrim
                                                     
                                                    -[...snip...]
                                                    - - - - - - - - - - - - - -
                                                    1 -os.environ is a dictionary of the environment variables defined on your system. In Windows, these are your user and system variables +[...snip...]
                                                    +
                                                      +
                                                    1. os.environ is a dictionary of the environment variables defined on your system. In Windows, these are your user and system variables accessible from MS-DOS. In UNIX, they are the variables exported in your shell's startup scripts. In Mac OS, there is no concept of environment variables, so this dictionary is empty. -
                                                    2 -os.environ.items() returns a list of tuples: [(key1, value1), (key2, value2), ...]. The for loop iterates through this list. The first round, it assigns key1 to k and value1 to v, so k = USERPROFILE and v = C:\Documents and Settings\mpilgrim. In the second round, k gets the second key, OS, and v gets the corresponding value, Windows_NT. -
                                                    3 -With multi-variable assignment and list comprehensions, you can replace the entire for loop with a single statement. Whether you actually do this in real code is a matter of personal coding style. I like it +
                                                  5. os.environ.items() returns a list of tuples: [(key1, value1), (key2, value2), ...]. The for loop iterates through this list. The first round, it assigns key1 to k and value1 to v, so k = USERPROFILE and v = C:\Documents and Settings\mpilgrim. In the second round, k gets the second key, OS, and v gets the corresponding value, Windows_NT. +
                                                  6. With multi-variable assignment and list comprehensions, you can replace the entire for loop with a single statement. Whether you actually do this in real code is a matter of personal coding style. I like it because it makes it clear that what I'm doing is mapping a dictionary into a list, then joining the list into a single string. Other programmers prefer to write this out as a for loop. The output is the same in either case, although this version is slightly faster, because there is only one print statement instead of many. -
                                                  7. Now we can look at the for loop in MP3FileInfo, from the sample fileinfo.py program introduced in Chapter 5. -

                                                    Example 6.11. for Loop in MP3FileInfo

                                                    +

                                                    Example 6.11. for Loop in MP3FileInfo

                                                    
                                                         tagDataMap = {"title"   : (  3,  33, stripnulls),
                                                     "artist"  : ( 33,  63, stripnulls),
                                                     "album"   : ( 63,  93, stripnulls),
                                                     "year"    : ( 93,  97, stripnulls),
                                                     "comment" : ( 97, 126, stripnulls),
                                                    -"genre"   : (127, 128, ord)}             1
                                                    +"genre"   : (127, 128, ord)}             
                                                         .
                                                         .
                                                         .
                                                                 if tagdata[:3] == "TAG":
                                                    -                for tag, (start, end, parseFunc) in self.tagDataMap.items(): 2
                                                    -  self[tag] = parseFunc(tagdata[start:end])                3
                                                    - - - - - - - - - - - - - -
                                                    1 -tagDataMap is a class attribute that defines the tags you're looking for in an MP3 file. Tags are stored in fixed-length fields. Once you read the last 128 bytes of the file, bytes 3 through 32 of those + for tag, (start, end, parseFunc) in self.tagDataMap.items(): + self[tag] = parseFunc(tagdata[start:end])
                                                    +
                                                      +
                                                    1. tagDataMap is a class attribute that defines the tags you're looking for in an MP3 file. Tags are stored in fixed-length fields. Once you read the last 128 bytes of the file, bytes 3 through 32 of those are always the song title, 33 through 62 are always the artist name, 63 through 92 are the album name, and so forth. Note that tagDataMap is a dictionary of tuples, and each tuple contains two integers and a function reference. -
                                                    2 -This looks complicated, but it's not. The structure of the for variables matches the structure of the elements of the list returned by items. Remember that items returns a list of tuples of the form (key, value). The first element of that list is ("title", (3, 33, <function stripnulls>)), so the first time around the loop, tag gets "title", start gets 3, end gets 33, and parseFunc gets the function stripnulls. -
                                                    3 -Now that you've extracted all the parameters for a single MP3 tag, saving the tag data is easy. You slice tagdata from start to end to get the actual data for this tag, call parseFunc to post-process the data, and assign this as the value for the key tag in the pseudo-dictionary self. After iterating through all the elements in tagDataMap, self has the values for all the tags, and you know what that looks like. -
                                                    +
                                                  8. This looks complicated, but it's not. The structure of the for variables matches the structure of the elements of the list returned by items. Remember that items returns a list of tuples of the form (key, value). The first element of that list is ("title", (3, 33, <function stripnulls>)), so the first time around the loop, tag gets "title", start gets 3, end gets 33, and parseFunc gets the function stripnulls. +
                                                  9. Now that you've extracted all the parameters for a single MP3 tag, saving the tag data is easy. You slice tagdata from start to end to get the actual data for this tag, call parseFunc to post-process the data, and assign this as the value for the key tag in the pseudo-dictionary self. After iterating through all the elements in tagDataMap, self has the values for all the tags, and you know what that looks like.

                                                    6.4. Using sys.modules

                                                    Modules, like everything else in Python, are objects. Once imported, you can always get a reference to a module through the global dictionary sys.modules. -

                                                    Example 6.12. Introducing sys.modules

                                                    >>> import sys        1
                                                    ->>> print '\n'.join(sys.modules.keys()) 2
                                                    -win32api
                                                    +

                                                    Example 6.12. Introducing sys.modules

                                                    >>> import sys        
                                                    +>>> print '\n'.join(sys.modules.keys()) 
                                                    +win32api
                                                     os.path
                                                     os
                                                     exceptions
                                                    @@ -3791,25 +2527,14 @@ __builtin__
                                                     site
                                                     signal
                                                     UserDict
                                                    -stat
                                                    - - - - - - - - - -
                                                    1 -The sys module contains system-level information, such as the version of Python you're running (sys.version or sys.version_info), and system-level options such as the maximum allowed recursion depth (sys.getrecursionlimit() and sys.setrecursionlimit()). -
                                                    2 -sys.modules is a dictionary containing all the modules that have ever been imported since Python was started; the key is the module name, the value is the module object. Note that this is more than just the modules your program has imported. Python preloads some modules on startup, and if you're using a Python IDE, sys.modules contains all the modules imported by all the programs you've run within the IDE. -
                                                    +stat
                                                    +
                                                      +
                                                    1. The sys module contains system-level information, such as the version of Python you're running (sys.version or sys.version_info), and system-level options such as the maximum allowed recursion depth (sys.getrecursionlimit() and sys.setrecursionlimit()). +
                                                    2. sys.modules is a dictionary containing all the modules that have ever been imported since Python was started; the key is the module name, the value is the module object. Note that this is more than just the modules your program has imported. Python preloads some modules on startup, and if you're using a Python IDE, sys.modules contains all the modules imported by all the programs you've run within the IDE.

                                                      This example demonstrates how to use sys.modules. -

                                                      Example 6.13. Using sys.modules

                                                      >>> import fileinfo         1
                                                      ->>> print '\n'.join(sys.modules.keys())
                                                      -win32api
                                                      +

                                                      Example 6.13. Using sys.modules

                                                      >>> import fileinfo         
                                                      +>>> print '\n'.join(sys.modules.keys())
                                                      +win32api
                                                       os.path
                                                       os
                                                       fileinfo
                                                      @@ -3823,390 +2548,197 @@ site
                                                       signal
                                                       UserDict
                                                       stat
                                                      ->>> fileinfo
                                                      +>>> fileinfo
                                                       <module 'fileinfo' from 'fileinfo.pyc'>
                                                      ->>> sys.modules["fileinfo"] 2
                                                      -<module 'fileinfo' from 'fileinfo.pyc'>
                                                      - - - - - - - - - -
                                                      1 -As new modules are imported, they are added to sys.modules. This explains why importing the same module twice is very fast: Python has already loaded and cached the module in sys.modules, so importing the second time is simply a dictionary lookup. -
                                                      2 -Given the name (as a string) of any previously-imported module, you can get a reference to the module itself through the sys.modules dictionary. -
                                                      +>>> sys.modules["fileinfo"] +<module 'fileinfo' from 'fileinfo.pyc'>
                                                      +
                                                        +
                                                      1. As new modules are imported, they are added to sys.modules. This explains why importing the same module twice is very fast: Python has already loaded and cached the module in sys.modules, so importing the second time is simply a dictionary lookup. +
                                                      2. Given the name (as a string) of any previously-imported module, you can get a reference to the module itself through the sys.modules dictionary.

                                                        The next example shows how to use the __module__ class attribute with the sys.modules dictionary to get a reference to the module in which a class is defined. -

                                                        Example 6.14. The __module__ Class Attribute

                                                        >>> from fileinfo import MP3FileInfo
                                                        ->>> MP3FileInfo.__module__              1
                                                        +

                                                        Example 6.14. The __module__ Class Attribute

                                                        >>> from fileinfo import MP3FileInfo
                                                        +>>> MP3FileInfo.__module__              
                                                         'fileinfo'
                                                        ->>> sys.modules[MP3FileInfo.__module__] 2
                                                        -<module 'fileinfo' from 'fileinfo.pyc'>
                                                        - - - - - - - - - -
                                                        1 -Every Python class has a built-in class attribute __module__, which is the name of the module in which the class is defined. -
                                                        2 -Combining this with the sys.modules dictionary, you can get a reference to the module in which a class is defined. -
                                                        +>>> sys.modules[MP3FileInfo.__module__] +<module 'fileinfo' from 'fileinfo.pyc'>
                                                        +
                                                          +
                                                        1. Every Python class has a built-in class attribute __module__, which is the name of the module in which the class is defined. +
                                                        2. Combining this with the sys.modules dictionary, you can get a reference to the module in which a class is defined.

                                                          Now you're ready to see how sys.modules is used in fileinfo.py, the sample program introduced in Chapter 5. This example shows that portion of the code. -

                                                          Example 6.15. sys.modules in fileinfo.py

                                                          -    def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]):       1
                                                          +

                                                          Example 6.15. sys.modules in fileinfo.py

                                                          
                                                          +    def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]):       
                                                                   "get file info class from filename extension"           
                                                          -        subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:]        2
                                                          -        return hasattr(module, subclass) and getattr(module, subclass) or FileInfo 3
                                                          - - - - - - - - - - - - - -
                                                          1 -This is a function with two arguments; filename is required, but module is optional and defaults to the module that contains the FileInfo class. This looks inefficient, because you might expect Python to evaluate the sys.modules expression every time the function is called. In fact, Python evaluates default expressions only once, the first time the module is imported. As you'll see later, you never call this + subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:] + return hasattr(module, subclass) and getattr(module, subclass) or FileInfo
                                                          +
                                                            +
                                                          1. This is a function with two arguments; filename is required, but module is optional and defaults to the module that contains the FileInfo class. This looks inefficient, because you might expect Python to evaluate the sys.modules expression every time the function is called. In fact, Python evaluates default expressions only once, the first time the module is imported. As you'll see later, you never call this function with a module argument, so module serves as a function-level constant. -
                                                          2 -You'll plow through this line later, after you dive into the os module. For now, take it on faith that subclass ends up as the name of a class, like MP3FileInfo. -
                                                          3 -You already know about getattr, which gets a reference to an object by name. hasattr is a complementary function that checks whether an object has a particular attribute; in this case, whether a module has +
                                                        3. You'll plow through this line later, after you dive into the os module. For now, take it on faith that subclass ends up as the name of a class, like MP3FileInfo. +
                                                        4. You already know about getattr, which gets a reference to an object by name. hasattr is a complementary function that checks whether an object has a particular attribute; in this case, whether a module has a particular class (although it works for any object and any attribute, just like getattr). In English, this line of code says, “If this module has the class named by subclass then return it, otherwise return the base class FileInfo.” -
                                                        5. -
                                                          +

                                                          Further Reading on Modules

                                                          6.5. Working with Directories

                                                          The os.path module has several functions for manipulating files and directories. Here, we're looking at handling pathnames and listing the contents of a directory. -

                                                          Example 6.16. Constructing Pathnames

                                                          ->>> import os
                                                          ->>> os.path.join("c:\\music\\ap\\", "mahadeva.mp3") 1 2
                                                          +

                                                          Example 6.16. Constructing Pathnames

                                                          +>>> import os
                                                          +>>> os.path.join("c:\\music\\ap\\", "mahadeva.mp3")  
                                                           'c:\\music\\ap\\mahadeva.mp3'
                                                          ->>> os.path.join("c:\\music\\ap", "mahadeva.mp3")   3
                                                          +>>> os.path.join("c:\\music\\ap", "mahadeva.mp3")   
                                                           'c:\\music\\ap\\mahadeva.mp3'
                                                          ->>> os.path.expanduser("~")       4
                                                          +>>> os.path.expanduser("~")       
                                                           'c:\\Documents and Settings\\mpilgrim\\My Documents'
                                                          ->>> os.path.join(os.path.expanduser("~"), "Python") 5
                                                          -'c:\\Documents and Settings\\mpilgrim\\My Documents\\Python'
                                                          - - - - - - - - - - - - - - - - - - - - - -
                                                          1 -os.path is a reference to a module -- which module depends on your platform. Just as getpass encapsulates differences between platforms by setting getpass to a platform-specific function, os encapsulates differences between platforms by setting path to a platform-specific module. -
                                                          2 -The join function of os.path constructs a pathname out of one or more partial pathnames. In this case, it simply concatenates strings. (Note that dealing +>>> os.path.join(os.path.expanduser("~"), "Python") +'c:\\Documents and Settings\\mpilgrim\\My Documents\\Python'
                                                          +
                                                            +
                                                          1. os.path is a reference to a module -- which module depends on your platform. Just as getpass encapsulates differences between platforms by setting getpass to a platform-specific function, os encapsulates differences between platforms by setting path to a platform-specific module. +
                                                          2. The join function of os.path constructs a pathname out of one or more partial pathnames. In this case, it simply concatenates strings. (Note that dealing with pathnames on Windows is annoying because the backslash character must be escaped.) -
                                                          3 -In this slightly less trivial case, join will add an extra backslash to the pathname before joining it to the filename. I was overjoyed when I discovered this, since +
                                                        6. In this slightly less trivial case, join will add an extra backslash to the pathname before joining it to the filename. I was overjoyed when I discovered this, since addSlashIfNecessary is one of the stupid little functions I always need to write when building up my toolbox in a new language. Do not write this stupid little function in Python; smart people have already taken care of it for you. -
                                                        7. 4 -expanduser will expand a pathname that uses ~ to represent the current user's home directory. This works on any platform where users have a home directory, like Windows, +
                                                        8. expanduser will expand a pathname that uses ~ to represent the current user's home directory. This works on any platform where users have a home directory, like Windows, UNIX, and Mac OS X; it has no effect on Mac OS. -
                                                        9. 5 -Combining these techniques, you can easily construct pathnames for directories and files under the user's home directory.
                                                          -

                                                          Example 6.17. Splitting Pathnames

                                                          >>> os.path.split("c:\\music\\ap\\mahadeva.mp3")      1
                                                          +
                                                        10. Combining these techniques, you can easily construct pathnames for directories and files under the user's home directory. +

                                                          Example 6.17. Splitting Pathnames

                                                          >>> os.path.split("c:\\music\\ap\\mahadeva.mp3")      
                                                           ('c:\\music\\ap', 'mahadeva.mp3')
                                                          ->>> (filepath, filename) = os.path.split("c:\\music\\ap\\mahadeva.mp3") 2
                                                          ->>> filepath      3
                                                          +>>> (filepath, filename) = os.path.split("c:\\music\\ap\\mahadeva.mp3") 
                                                          +>>> filepath      
                                                           'c:\\music\\ap'
                                                          ->>> filename      4
                                                          +>>> filename      
                                                           'mahadeva.mp3'
                                                          ->>> (shortname, extension) = os.path.splitext(filename)                 5
                                                          ->>> shortname
                                                          +>>> (shortname, extension) = os.path.splitext(filename)                 
                                                          +>>> shortname
                                                           'mahadeva'
                                                          ->>> extension
                                                          -'.mp3'
                                                          - - - - - - - - - - - - - - - - - - - - - -
                                                          1 -The split function splits a full pathname and returns a tuple containing the path and filename. Remember when I said you could use +>>> extension +'.mp3'
                                                          +
                                                            +
                                                          1. The split function splits a full pathname and returns a tuple containing the path and filename. Remember when I said you could use multi-variable assignment to return multiple values from a function? Well, split is such a function. -
                                                          2 -You assign the return value of the split function into a tuple of two variables. Each variable receives the value of the corresponding element of the returned tuple. -
                                                          3 -The first variable, filepath, receives the value of the first element of the tuple returned from split, the file path. -
                                                          4 -The second variable, filename, receives the value of the second element of the tuple returned from split, the filename. -
                                                          5 -os.path also contains a function splitext, which splits a filename and returns a tuple containing the filename and the file extension. You use the same technique +
                                                        11. You assign the return value of the split function into a tuple of two variables. Each variable receives the value of the corresponding element of the returned tuple. +
                                                        12. The first variable, filepath, receives the value of the first element of the tuple returned from split, the file path. +
                                                        13. The second variable, filename, receives the value of the second element of the tuple returned from split, the filename. +
                                                        14. os.path also contains a function splitext, which splits a filename and returns a tuple containing the filename and the file extension. You use the same technique to assign each of them to separate variables. -
                                                        15. -

                                                          Example 6.18. Listing Directories

                                                          >>> os.listdir("c:\\music\\_singles\\")              1
                                                          -['a_time_long_forgotten_con.mp3', 'hellraiser.mp3',
                                                          +

                                                          Example 6.18. Listing Directories

                                                          >>> os.listdir("c:\\music\\_singles\\")              
                                                          +['a_time_long_forgotten_con.mp3', 'hellraiser.mp3',
                                                           'kairo.mp3', 'long_way_home1.mp3', 'sidewinder.mp3', 
                                                           'spinning.mp3']
                                                          ->>> dirname = "c:\\"
                                                          ->>> os.listdir(dirname)            2
                                                          -['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'cygwin',
                                                          +>>> dirname = "c:\\"
                                                          +>>> os.listdir(dirname)            
                                                          +['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'cygwin',
                                                           'docbook', 'Documents and Settings', 'Incoming', 'Inetpub', 'IO.SYS',
                                                           'MSDOS.SYS', 'Music', 'NTDETECT.COM', 'ntldr', 'pagefile.sys',
                                                           'Program Files', 'Python20', 'RECYCLER',
                                                           'System Volume Information', 'TEMP', 'WINNT']
                                                          ->>> [f for f in os.listdir(dirname)
                                                          -...    if os.path.isfile(os.path.join(dirname, f))] 3
                                                          -['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'IO.SYS', 'MSDOS.SYS',
                                                          +>>> [f for f in os.listdir(dirname)
                                                          +...    if os.path.isfile(os.path.join(dirname, f))] 
                                                          +['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'IO.SYS', 'MSDOS.SYS',
                                                           'NTDETECT.COM', 'ntldr', 'pagefile.sys']
                                                          ->>> [f for f in os.listdir(dirname)
                                                          -...    if os.path.isdir(os.path.join(dirname, f))]  4
                                                          -['cygwin', 'docbook', 'Documents and Settings', 'Incoming',
                                                          +>>> [f for f in os.listdir(dirname)
                                                          +...    if os.path.isdir(os.path.join(dirname, f))]  
                                                          +['cygwin', 'docbook', 'Documents and Settings', 'Incoming',
                                                           'Inetpub', 'Music', 'Program Files', 'Python20', 'RECYCLER',
                                                          -'System Volume Information', 'TEMP', 'WINNT']
                                                          - - - - - - - - - - - - - - - - - -
                                                          1 -The listdir function takes a pathname and returns a list of the contents of the directory. -
                                                          2 -listdir returns both files and folders, with no indication of which is which. -
                                                          3 -You can use list filtering and the isfile function of the os.path module to separate the files from the folders. isfile takes a pathname and returns 1 if the path represents a file, and 0 otherwise. Here you're using os.path.join to ensure a full pathname, but isfile also works with a partial path, relative to the current working directory. You can use os.getcwd() to get the current working directory. -
                                                          4 -os.path also has a isdir function which returns 1 if the path represents a directory, and 0 otherwise. You can use this to get a list of the subdirectories +'System Volume Information', 'TEMP', 'WINNT']
                                                          +
                                                            +
                                                          1. The listdir function takes a pathname and returns a list of the contents of the directory. +
                                                          2. listdir returns both files and folders, with no indication of which is which. +
                                                          3. You can use list filtering and the isfile function of the os.path module to separate the files from the folders. isfile takes a pathname and returns 1 if the path represents a file, and 0 otherwise. Here you're using os.path.join to ensure a full pathname, but isfile also works with a partial path, relative to the current working directory. You can use os.getcwd() to get the current working directory. +
                                                          4. os.path also has a isdir function which returns 1 if the path represents a directory, and 0 otherwise. You can use this to get a list of the subdirectories within a directory. -
                                                          -

                                                          Example 6.19. Listing Directories in fileinfo.py

                                                          +

                                                          Example 6.19. Listing Directories in fileinfo.py

                                                          
                                                           def listDirectory(directory, fileExtList):    
                                                               "get list of file info objects for files of particular extensions" 
                                                               fileList = [os.path.normcase(f)
                                                          -                for f in os.listdir(directory)]            1 2
                                                          +                for f in os.listdir(directory)]             
                                                               fileList = [os.path.join(directory, f) 
                                                                          for f in fileList
                                                          -                if os.path.splitext(f)[1] in fileExtList]  3 4 5
                                                          - - - - - - - - - - - - - - - - - - - - - -
                                                          1 -os.listdir(directory) returns a list of all the files and folders in directory. -
                                                          2 -Iterating through the list with f, you use os.path.normcase(f) to normalize the case according to operating system defaults. normcase is a useful little function that compensates for case-insensitive operating systems that think that mahadeva.mp3 and mahadeva.MP3 are the same file. For instance, on Windows and Mac OS, normcase will convert the entire filename to lowercase; on UNIX-compatible systems, it will return the filename unchanged. -
                                                          3 -Iterating through the normalized list with f again, you use os.path.splitext(f) to split each filename into name and extension. -
                                                          4 -For each file, you see if the extension is in the list of file extensions you care about (fileExtList, which was passed to the listDirectory function). -
                                                          5 -For each file you care about, you use os.path.join(directory, f) to construct the full pathname of the file, and return a list of the full pathnames. -
                                                          -
                                                          - - - - -
                                                          Note
                                                          Whenever possible, you should use the functions in os and os.path for file, directory, and path manipulations. These modules are wrappers for platform-specific modules, so functions like + if os.path.splitext(f)[1] in fileExtList]
                                                          +
                                                            +
                                                          1. os.listdir(directory) returns a list of all the files and folders in directory. +
                                                          2. Iterating through the list with f, you use os.path.normcase(f) to normalize the case according to operating system defaults. normcase is a useful little function that compensates for case-insensitive operating systems that think that mahadeva.mp3 and mahadeva.MP3 are the same file. For instance, on Windows and Mac OS, normcase will convert the entire filename to lowercase; on UNIX-compatible systems, it will return the filename unchanged. +
                                                          3. Iterating through the normalized list with f again, you use os.path.splitext(f) to split each filename into name and extension. +
                                                          4. For each file, you see if the extension is in the list of file extensions you care about (fileExtList, which was passed to the listDirectory function). +
                                                          5. For each file you care about, you use os.path.join(directory, f) to construct the full pathname of the file, and return a list of the full pathnames. + + + - -
                                                            NoteWhenever possible, you should use the functions in os and os.path for file, directory, and path manipulations. These modules are wrappers for platform-specific modules, so functions like os.path.split work on UNIX, Windows, Mac OS, and any other platform supported by Python. -

                                                            There is one other way to get the contents of a directory. It's very powerful, and it uses the sort of wildcards that you may already be familiar with from working on the command line. -

                                                            Example 6.20. Listing Directories with glob

                                                            ->>> os.listdir("c:\\music\\_singles\\")               1
                                                            -['a_time_long_forgotten_con.mp3', 'hellraiser.mp3',
                                                            +

                                                            Example 6.20. Listing Directories with glob

                                                            +>>> os.listdir("c:\\music\\_singles\\")               
                                                            +['a_time_long_forgotten_con.mp3', 'hellraiser.mp3',
                                                             'kairo.mp3', 'long_way_home1.mp3', 'sidewinder.mp3',
                                                             'spinning.mp3']
                                                            ->>> import glob
                                                            ->>> glob.glob('c:\\music\\_singles\\*.mp3')           2
                                                            -['c:\\music\\_singles\\a_time_long_forgotten_con.mp3',
                                                            +>>> import glob
                                                            +>>> glob.glob('c:\\music\\_singles\\*.mp3')           
                                                            +['c:\\music\\_singles\\a_time_long_forgotten_con.mp3',
                                                             'c:\\music\\_singles\\hellraiser.mp3',
                                                             'c:\\music\\_singles\\kairo.mp3',
                                                             'c:\\music\\_singles\\long_way_home1.mp3',
                                                             'c:\\music\\_singles\\sidewinder.mp3',
                                                             'c:\\music\\_singles\\spinning.mp3']
                                                            ->>> glob.glob('c:\\music\\_singles\\s*.mp3')          3
                                                            -['c:\\music\\_singles\\sidewinder.mp3',
                                                            +>>> glob.glob('c:\\music\\_singles\\s*.mp3')          
                                                            +['c:\\music\\_singles\\sidewinder.mp3',
                                                             'c:\\music\\_singles\\spinning.mp3']
                                                            ->>> glob.glob('c:\\music\\*\\*.mp3')4
                                                            -
                                                            - - - - - - - - - - - - - - - - - -
                                                            1 -As you saw earlier, os.listdir simply takes a directory path and lists all files and directories in that directory. -
                                                            2 -The glob module, on the other hand, takes a wildcard and returns the full path of all files and directories matching the wildcard. +>>> glob.glob('c:\\music\\*\\*.mp3') +
                                                            +
                                                              +
                                                            1. As you saw earlier, os.listdir simply takes a directory path and lists all files and directories in that directory. +
                                                            2. The glob module, on the other hand, takes a wildcard and returns the full path of all files and directories matching the wildcard. Here the wildcard is a directory path plus "*.mp3", which will match all .mp3 files. Note that each element of the returned list already includes the full path of the file. -
                                                            3 -If you want to find all the files in a specific directory that start with "s" and end with ".mp3", you can do that too.
                                                            4 -Now consider this scenario: you have a music directory, with several subdirectories within it, with .mp3 files within each subdirectory. You can get a list of all of those with a single call to glob, by using two wildcards at once. One wildcard is the "*.mp3" (to match .mp3 files), and one wildcard is within the directory path itself, to match any subdirectory within c:\music. That's a crazy amount of power packed into one deceptively simple-looking function! -
                                                            -
                                                            +
                                                          6. If you want to find all the files in a specific directory that start with "s" and end with ".mp3", you can do that too. +
                                                          7. Now consider this scenario: you have a music directory, with several subdirectories within it, with .mp3 files within each subdirectory. You can get a list of all of those with a single call to glob, by using two wildcards at once. One wildcard is the "*.mp3" (to match .mp3 files), and one wildcard is within the directory path itself, to match any subdirectory within c:\music. That's a crazy amount of power packed into one deceptively simple-looking function! +

                                                            Further Reading on the os Module

                                                            6.6. Putting It All Together

                                                            Once again, all the dominoes are in place. You've seen how each line of code works. Now let's step back and see how it all fits together. -

                                                            Example 6.21. listDirectory

                                                            -def listDirectory(directory, fileExtList):     1
                                                            +

                                                            Example 6.21. listDirectory

                                                            
                                                            +def listDirectory(directory, fileExtList):     
                                                                 "get list of file info objects for files of particular extensions"
                                                                 fileList = [os.path.normcase(f)
                                                                             for f in os.listdir(directory)]           
                                                                 fileList = [os.path.join(directory, f) 
                                                                            for f in fileList
                                                            -                if os.path.splitext(f)[1] in fileExtList]        2
                                                            -    def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]):       3
                                                            +                if os.path.splitext(f)[1] in fileExtList]        
                                                            +    def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]):       
                                                                     "get file info class from filename extension"           
                                                            -        subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:]        4
                                                            -        return hasattr(module, subclass) and getattr(module, subclass) or FileInfo 5
                                                            -    return [getFileInfoClass(f)(f) for f in fileList]            6
                                                            - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                            1 -listDirectory is the main attraction of this entire module. It takes a directory (like c:\music\_singles\ in my case) and a list of interesting file extensions (like ['.mp3']), and it returns a list of class instances that act like dictionaries that contain metadata about each interesting file in + subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:] + return hasattr(module, subclass) and getattr(module, subclass) or FileInfo + return [getFileInfoClass(f)(f) for f in fileList]
                                                            +
                                                              +
                                                            1. listDirectory is the main attraction of this entire module. It takes a directory (like c:\music\_singles\ in my case) and a list of interesting file extensions (like ['.mp3']), and it returns a list of class instances that act like dictionaries that contain metadata about each interesting file in that directory. And it does it in just a few straightforward lines of code. -
                                                            2 -As you saw in the previous section, this line of code gets a list of the full pathnames of all the files in directory that have an interesting file extension (as specified by fileExtList). -
                                                            3 -Old-school Pascal programmers may be familiar with them, but most people give me a blank stare when I tell them that Python supports nested functions -- literally, a function within a function. The nested function getFileInfoClass can be called only from the function in which it is defined, listDirectory. As with any other function, you don't need an interface declaration or anything fancy; just define the function and code +
                                                          8. As you saw in the previous section, this line of code gets a list of the full pathnames of all the files in directory that have an interesting file extension (as specified by fileExtList). +
                                                          9. Old-school Pascal programmers may be familiar with them, but most people give me a blank stare when I tell them that Python supports nested functions -- literally, a function within a function. The nested function getFileInfoClass can be called only from the function in which it is defined, listDirectory. As with any other function, you don't need an interface declaration or anything fancy; just define the function and code it. -
                                                          10. 4 -Now that you've seen the os module, this line should make more sense. It gets the extension of the file (os.path.splitext(filename)[1]), forces it to uppercase (.upper()), slices off the dot ([1:]), and constructs a class name out of it with string formatting. So c:\music\ap\mahadeva.mp3 becomes .mp3 becomes .MP3 becomes MP3 becomes MP3FileInfo. -
                                                            5 -Having constructed the name of the handler class that would handle this file, you check to see if that handler class actually +
                                                          11. Now that you've seen the os module, this line should make more sense. It gets the extension of the file (os.path.splitext(filename)[1]), forces it to uppercase (.upper()), slices off the dot ([1:]), and constructs a class name out of it with string formatting. So c:\music\ap\mahadeva.mp3 becomes .mp3 becomes .MP3 becomes MP3 becomes MP3FileInfo. +
                                                          12. Having constructed the name of the handler class that would handle this file, you check to see if that handler class actually exists in this module. If it does, you return the class, otherwise you return the base class FileInfo. This is a very important point: this function returns a class. Not an instance of a class, but the class itself. -
                                                          13. 6 -For each file in the “interesting files” list (fileList), you call getFileInfoClass with the filename (f). Calling getFileInfoClass(f) returns a class; you don't know exactly which class, but you don't care. You then create an instance of this class (whatever +
                                                          14. For each file in the “interesting files” list (fileList), you call getFileInfoClass with the filename (f). Calling getFileInfoClass(f) returns a class; you don't know exactly which class, but you don't care. You then create an instance of this class (whatever it is) and pass the filename (f again), to the __init__ method. As you saw earlier in this chapter, the __init__ method of FileInfo sets self["name"], which triggers __setitem__, which is overridden in the descendant (MP3FileInfo) to parse the file appropriately to pull out the file's metadata. You do all that for each interesting file and return a list of the resulting instances. -
                                                          15. Note that listDirectory is completely generic. It doesn't know ahead of time which types of files it will be getting, or which classes are defined that could potentially handle those files. It inspects the directory for the files to process, and then introspects its own module to see what special handler classes (like MP3FileInfo) are defined. You can extend this program to handle other types of files simply by defining an appropriately-named class: HTMLFileInfo for HTML files, DOCFileInfo for Word .doc files, and so forth. listDirectory will handle them all, without modification, by handing off the real work to the appropriate classes and collating the results.

                                                            6.7. Summary

                                                            The fileinfo.py program introduced in Chapter 5 should now make perfect sense. -

                                                            +
                                                            
                                                             """Framework for getting filetype-specific metadata.
                                                             
                                                             Instantiate appropriate class with filename. Returned object acts like a
                                                            @@ -4283,9 +2815,9 @@ def listDirectory(directory, fileExtList):
                                                             if __name__ == "__main__":
                                                                 for info in listDirectory("/music/_singles/", [".mp3"]):
                                                                     print "\n".join(["%s=%s" % (k, v) for k, v in info.items()])
                                                            -        print
                                                            + print

                                                            Before diving into the next chapter, make sure you're comfortable doing the following things: -

                                                            +
                                                            -
                                                            -

                                                            Chapter 7. Regular Expressions

                                                            -

                                                            Regular expressions are a powerful and standardized way of searching, replacing, and parsing text with complex patterns of -characters. If you've used regular expressions in other languages (like Perl), the syntax will be very familiar, and you get by just reading the summary of the re module to get an overview of the available functions and their arguments. -

                                                            7.1. Diving In

                                                            -

                                                            Strings have methods for searching (index, find, and count), replacing (replace), and parsing (split), but they are limited to the simplest of cases. The search methods look for a single, hard-coded substring, and they are -always case-sensitive. To do case-insensitive searches of a string s, you must call s.lower() or s.upper() and make sure your search strings are the appropriate case to match. The replace and split methods have the same limitations. -

                                                            If what you're trying to do can be accomplished with string functions, you should use them. They're fast and simple and easy - to read, and there's a lot to be said for fast, simple, readable code. But if you find yourself using a lot of different - string functions with if statements to handle special cases, or if you're combining them with split and join and list comprehensions in weird unreadable ways, you may need to move up to regular expressions. -

                                                            Although the regular expression syntax is tight and unlike normal code, the result can end up being more readable than a hand-rolled solution that uses a long chain of string functions. There are even ways of embedding comments -within regular expressions to make them practically self-documenting. -

                                                            7.2. Case Study: Street Addresses

                                                            -

                                                            This series of examples was inspired by a real-life problem I had in my day job several years ago, when I needed to scrub - and standardize street addresses exported from a legacy system before importing them into a newer system. (See, I don't just - make this stuff up; it's actually useful.) This example shows how I approached the problem. -

                                                            Example 7.1. Matching at the End of a String

                                                            ->>> s = '100 NORTH MAIN ROAD'
                                                            ->>> s.replace('ROAD', 'RD.')               1
                                                            -'100 NORTH MAIN RD.'
                                                            ->>> s = '100 NORTH BROAD ROAD'
                                                            ->>> s.replace('ROAD', 'RD.')               2
                                                            -'100 NORTH BRD. RD.'
                                                            ->>> s[:-4] + s[-4:].replace('ROAD', 'RD.') 3
                                                            -'100 NORTH BROAD RD.'
                                                            ->>> import re            4
                                                            ->>> re.sub('ROAD$', 'RD.', s)              5 6
                                                            -'100 NORTH BROAD RD.'
                                                            - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                            1 -My goal is to standardize a street address so that 'ROAD' is always abbreviated as 'RD.'. At first glance, I thought this was simple enough that I could just use the string method replace. After all, all the data was already uppercase, so case mismatches would not be a problem. And the search string, 'ROAD', was a constant. And in this deceptively simple example, s.replace does indeed work. -
                                                            2 -Life, unfortunately, is full of counterexamples, and I quickly discovered this one. The problem here is that 'ROAD' appears twice in the address, once as part of the street name 'BROAD' and once as its own word. The replace method sees these two occurrences and blindly replaces both of them; meanwhile, I see my addresses getting destroyed. -
                                                            3 -To solve the problem of addresses with more than one 'ROAD' substring, you could resort to something like this: only search and replace 'ROAD' in the last four characters of the address (s[-4:]), and leave the string alone (s[:-4]). But you can see that this is already getting unwieldy. For example, the pattern is dependent on the length of the string - you're replacing (if you were replacing 'STREET' with 'ST.', you would need to use s[:-6] and s[-6:].replace(...)). Would you like to come back in six months and debug this? I know I wouldn't. -
                                                            4 -It's time to move up to regular expressions. In Python, all functionality related to regular expressions is contained in the re module. -
                                                            5 -Take a look at the first parameter: 'ROAD$'. This is a simple regular expression that matches 'ROAD' only when it occurs at the end of a string. The $ means “end of the string”. (There is a corresponding character, the caret ^, which means “beginning of the string”.) -
                                                            6 -Using the re.sub function, you search the string s for the regular expression 'ROAD$' and replace it with 'RD.'. This matches the ROAD at the end of the string s, but does not match the ROAD that's part of the word BROAD, because that's in the middle of s. -
                                                            -

                                                            Continuing with my story of scrubbing addresses, I soon discovered that the previous example, matching 'ROAD' at the end of the address, was not good enough, because not all addresses included a street designation at all; some just -ended with the street name. Most of the time, I got away with it, but if the street name was 'BROAD', then the regular expression would match 'ROAD' at the end of the string as part of the word 'BROAD', which is not what I wanted. -

                                                            Example 7.2. Matching Whole Words

                                                            ->>> s = '100 BROAD'
                                                            ->>> re.sub('ROAD$', 'RD.', s)
                                                            -'100 BRD.'
                                                            ->>> re.sub('\\bROAD$', 'RD.', s)  1
                                                            -'100 BROAD'
                                                            ->>> re.sub(r'\bROAD$', 'RD.', s)  2
                                                            -'100 BROAD'
                                                            ->>> s = '100 BROAD ROAD APT. 3'
                                                            ->>> re.sub(r'\bROAD$', 'RD.', s)  3
                                                            -'100 BROAD ROAD APT. 3'
                                                            ->>> re.sub(r'\bROAD\b', 'RD.', s) 4
                                                            -'100 BROAD RD. APT 3'
                                                            - - - - - - - - - - - - - - - - - -
                                                            1 -What I really wanted was to match 'ROAD' when it was at the end of the string and it was its own whole word, not a part of some larger word. To express this in a regular expression, you use \b, which means “a word boundary must occur right here”. In Python, this is complicated by the fact that the '\' character in a string must itself be escaped. This is sometimes referred to as the backslash plague, and it is one reason - why regular expressions are easier in Perl than in Python. On the down side, Perl mixes regular expressions with other syntax, so if you have a bug, it may be hard to tell whether it's a bug in syntax or - a bug in your regular expression. -
                                                            2 -To work around the backslash plague, you can use what is called a raw string, by prefixing the string with the letter r. This tells Python that nothing in this string should be escaped; '\t' is a tab character, but r'\t' is really the backslash character \ followed by the letter t. I recommend always using raw strings when dealing with regular expressions; otherwise, things get too confusing too quickly - (and regular expressions get confusing quickly enough all by themselves). -
                                                            3 -*sigh* Unfortunately, I soon found more cases that contradicted my logic. In this case, the street address contained the word -'ROAD' as a whole word by itself, but it wasn't at the end, because the address had an apartment number after the street designation. - Because 'ROAD' isn't at the very end of the string, it doesn't match, so the entire call to re.sub ends up replacing nothing at all, and you get the original string back, which is not what you want. -
                                                            4 -To solve this problem, I removed the $ character and added another \b. Now the regular expression reads “match 'ROAD' when it's a whole word by itself anywhere in the string,” whether at the end, the beginning, or somewhere in the middle. -
                                                            -

                                                            7.3. Case Study: Roman Numerals

                                                            -

                                                            You've most likely seen Roman numerals, even if you didn't recognize them. You may have seen them in copyrights of old movies - and television shows (“Copyright MCMXLVI” instead of “Copyright 1946”), or on the dedication walls of libraries or universities (“established MDCCCLXXXVIII” instead of “established 1888”). You may also have seen them in outlines and bibliographical references. It's a system of representing numbers that really - does date back to the ancient Roman empire (hence the name). -

                                                            In Roman numerals, there are seven characters that are repeated and combined in various ways to represent numbers. -

                                                            -
                                                              -
                                                            • I = 1 -
                                                            • V = 5 -
                                                            • X = 10 -
                                                            • L = 50 -
                                                            • C = 100 -
                                                            • D = 500 -
                                                            • M = 1000 -
                                                            -

                                                            The following are some general rules for constructing Roman numerals: -

                                                            -
                                                              -
                                                            • Characters are additive. I is 1, II is 2, and III is 3. VI is 6 (literally, “5 and 1”), VII is 7, and VIII is 8. - -
                                                            • The tens characters (I, X, C, and M) can be repeated up to three times. At 4, you need to subtract from the next highest fives character. You can't represent 4 as IIII; instead, it is represented as IV (“1 less than 5”). The number 40 is written as XL (10 less than 50), 41 as XLI, 42 as XLII, 43 as XLIII, and then 44 as XLIV (10 less than 50, then 1 less than 5). - -
                                                            • Similarly, at 9, you need to subtract from the next highest tens character: 8 is VIII, but 9 is IX (1 less than 10), not VIIII (since the I character can not be repeated four times). The number 90 is XC, 900 is CM. - -
                                                            • The fives characters can not be repeated. The number 10 is always represented as X, never as VV. The number 100 is always C, never LL. - -
                                                            • Roman numerals are always written highest to lowest, and read left to right, so the order the of characters matters very much. - DC is 600; CD is a completely different number (400, 100 less than 500). CI is 101; IC is not even a valid Roman numeral (because you can't subtract 1 directly from 100; you would need to write it as XCIX, for 10 less than 100, then 1 less than 10). - -
                                                            -

                                                            7.3.1. Checking for Thousands

                                                            -

                                                            What would it take to validate that an arbitrary string is a valid Roman numeral? Let's take it one digit at a time. Since - Roman numerals are always written highest to lowest, let's start with the highest: the thousands place. For numbers 1000 - and higher, the thousands are represented by a series of M characters. -

                                                            Example 7.3. Checking for Thousands

                                                            ->>> import re
                                                            ->>> pattern = '^M?M?M?$'       1
                                                            ->>> re.search(pattern, 'M')    2
                                                            -<SRE_Match object at 0106FB58>
                                                            ->>> re.search(pattern, 'MM')   3
                                                            -<SRE_Match object at 0106C290>
                                                            ->>> re.search(pattern, 'MMM')  4
                                                            -<SRE_Match object at 0106AA38>
                                                            ->>> re.search(pattern, 'MMMM') 5
                                                            ->>> re.search(pattern, '')     6
                                                            -<SRE_Match object at 0106F4A8>
                                                            - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                            1 -This pattern has three parts: -
                                                            -
                                                              -
                                                            • ^ to match what follows only at the beginning of the string. If this were not specified, the pattern would match no matter - where the M characters were, which is not what you want. You want to make sure that the M characters, if they're there, are at the beginning of the string. - -
                                                            • M? to optionally match a single M character. Since this is repeated three times, you're matching anywhere from zero to three M characters in a row. - -
                                                            • $ to match what precedes only at the end of the string. When combined with the ^ character at the beginning, this means that the pattern must match the entire string, with no other characters before or - after the M characters. - -
                                                            -
                                                            2 -The essence of the re module is the search function, that takes a regular expression (pattern) and a string ('M') to try to match against the regular expression. If a match is found, search returns an object which has various methods to describe the match; if no match is found, search returns None, the Python null value. All you care about at the moment is whether the pattern matches, which you can tell by just looking at the return - value of search. 'M' matches this regular expression, because the first optional M matches and the second and third optional M characters are ignored. -
                                                            3 -'MM' matches because the first and second optional M characters match and the third M is ignored. -
                                                            4 -'MMM' matches because all three M characters match. -
                                                            5 -'MMMM' does not match. All three M characters match, but then the regular expression insists on the string ending (because of the $ character), and the string doesn't end yet (because of the fourth M). So search returns None. -
                                                            6 -Interestingly, an empty string also matches this regular expression, since all the M characters are optional. -
                                                            -

                                                            7.3.2. Checking for Hundreds

                                                            -

                                                            The hundreds place is more difficult than the thousands, because there are several mutually exclusive ways it could be expressed, - depending on its value. -

                                                            -
                                                              -
                                                            • 100 = C -
                                                            • 200 = CC -
                                                            • 300 = CCC -
                                                            • 400 = CD -
                                                            • 500 = D -
                                                            • 600 = DC -
                                                            • 700 = DCC -
                                                            • 800 = DCCC -
                                                            • 900 = CM -
                                                            -

                                                            So there are four possible patterns: -

                                                            -
                                                              -
                                                            • CM -
                                                            • CD -
                                                            • Zero to three C characters (zero if the hundreds place is 0) - -
                                                            • D, followed by zero to three C characters - -
                                                            -

                                                            The last two patterns can be combined: -

                                                            -
                                                              -
                                                            • an optional D, followed by zero to three C characters - -
                                                            -

                                                            This example shows how to validate the hundreds place of a Roman numeral. -

                                                            Example 7.4. Checking for Hundreds

                                                            ->>> import re
                                                            ->>> pattern = '^M?M?M?(CM|CD|D?C?C?C?)$' 1
                                                            ->>> re.search(pattern, 'MCM')            2
                                                            -<SRE_Match object at 01070390>
                                                            ->>> re.search(pattern, 'MD')             3
                                                            -<SRE_Match object at 01073A50>
                                                            ->>> re.search(pattern, 'MMMCCC')         4
                                                            -<SRE_Match object at 010748A8>
                                                            ->>> re.search(pattern, 'MCMC')           5
                                                            ->>> re.search(pattern, '')               6
                                                            -<SRE_Match object at 01071D98>
                                                            - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                            1 -This pattern starts out the same as the previous one, checking for the beginning of the string (^), then the thousands place (M?M?M?). Then it has the new part, in parentheses, which defines a set of three mutually exclusive patterns, separated by vertical - bars: CM, CD, and D?C?C?C? (which is an optional D followed by zero to three optional C characters). The regular expression parser checks for each of these patterns in order (from left to right), takes the first - one that matches, and ignores the rest. -
                                                            2 -'MCM' matches because the first M matches, the second and third M characters are ignored, and the CM matches (so the CD and D?C?C?C? patterns are never even considered). MCM is the Roman numeral representation of 1900. -
                                                            3 -'MD' matches because the first M matches, the second and third M characters are ignored, and the D?C?C?C? pattern matches D (each of the three C characters are optional and are ignored). MD is the Roman numeral representation of 1500. -
                                                            4 -'MMMCCC' matches because all three M characters match, and the D?C?C?C? pattern matches CCC (the D is optional and is ignored). MMMCCC is the Roman numeral representation of 3300. -
                                                            5 -'MCMC' does not match. The first M matches, the second and third M characters are ignored, and the CM matches, but then the $ does not match because you're not at the end of the string yet (you still have an unmatched C character). The C does not match as part of the D?C?C?C? pattern, because the mutually exclusive CM pattern has already matched. -
                                                            6 -Interestingly, an empty string still matches this pattern, because all the M characters are optional and ignored, and the empty string matches the D?C?C?C? pattern where all the characters are optional and ignored. -
                                                            -

                                                            Whew! See how quickly regular expressions can get nasty? And you've only covered the thousands and hundreds places of Roman - numerals. But if you followed all that, the tens and ones places are easy, because they're exactly the same pattern. But - let's look at another way to express the pattern. -

                                                            7.4. Using the {n,m} Syntax

                                                            -

                                                            In the previous section, you were dealing with a pattern where the same character could be repeated up to three times. There is another way to express - this in regular expressions, which some people find more readable. First look at the method we already used in the previous - example. -

                                                            Example 7.5. The Old Way: Every Character Optional

                                                            ->>> import re
                                                            ->>> pattern = '^M?M?M?$'
                                                            ->>> re.search(pattern, 'M')    1
                                                            -<_sre.SRE_Match object at 0x008EE090>
                                                            ->>> pattern = '^M?M?M?$'
                                                            ->>> re.search(pattern, 'MM')   2
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> pattern = '^M?M?M?$'
                                                            ->>> re.search(pattern, 'MMM')  3
                                                            -<_sre.SRE_Match object at 0x008EE090>
                                                            ->>> re.search(pattern, 'MMMM') 4
                                                            ->>> 
                                                            -
                                                            - - - - - - - - - - - - - - - - - -
                                                            1 -This matches the start of the string, and then the first optional M, but not the second and third M (but that's okay because they're optional), and then the end of the string. -
                                                            2 -This matches the start of the string, and then the first and second optional M, but not the third M (but that's okay because it's optional), and then the end of the string. -
                                                            3 -This matches the start of the string, and then all three optional M, and then the end of the string. -
                                                            4 -This matches the start of the string, and then all three optional M, but then does not match the the end of the string (because there is still one unmatched M), so the pattern does not match and returns None. -
                                                            -

                                                            Example 7.6. The New Way: From n o m

                                                            ->>> pattern = '^M{0,3}$'       1
                                                            ->>> re.search(pattern, 'M')    2
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MM')   3
                                                            -<_sre.SRE_Match object at 0x008EE090>
                                                            ->>> re.search(pattern, 'MMM')  4
                                                            -<_sre.SRE_Match object at 0x008EEDA8>
                                                            ->>> re.search(pattern, 'MMMM') 5
                                                            ->>> 
                                                            -
                                                            - - - - - - - - - - - - - - - - - - - - - -
                                                            1 -This pattern says: “Match the start of the string, then anywhere from zero to three M characters, then the end of the string.” The 0 and 3 can be any numbers; if you want to match at least one but no more than three M characters, you could say M{1,3}. -
                                                            2 -This matches the start of the string, then one M out of a possible three, then the end of the string. -
                                                            3 -This matches the start of the string, then two M out of a possible three, then the end of the string. -
                                                            4 -This matches the start of the string, then three M out of a possible three, then the end of the string. -
                                                            5 -This matches the start of the string, then three M out of a possible three, but then does not match the end of the string. The regular expression allows for up to only three M characters before the end of the string, but you have four, so the pattern does not match and returns None. -
                                                            -
                                                            - - - - - - -
                                                            Note
                                                            There is no way to programmatically determine that two regular expressions are equivalent. The best you can do is write a - lot of test cases to make sure they behave the same way on all relevant inputs. You'll talk more about writing test cases - later in this book. -
                                                            -

                                                            7.4.1. Checking for Tens and Ones

                                                            -

                                                            Now let's expand the Roman numeral regular expression to cover the tens and ones place. This example shows the check for - tens. -

                                                            Example 7.7. Checking for Tens

                                                            ->>> pattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)$'
                                                            ->>> re.search(pattern, 'MCMXL')    1
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MCML')     2
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MCMLX')    3
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MCMLXXX')  4
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MCMLXXXX') 5
                                                            ->>> 
                                                            -
                                                            - - - - - - - - - - - - - - - - - - - - - -
                                                            1 -This matches the start of the string, then the first optional M, then CM, then XL, then the end of the string. Remember, the (A|B|C) syntax means “match exactly one of A, B, or C”. You match XL, so you ignore the XC and L?X?X?X? choices, and then move on to the end of the string. MCML is the Roman numeral representation of 1940. -
                                                            2 -This matches the start of the string, then the first optional M, then CM, then L?X?X?X?. Of the L?X?X?X?, it matches the L and skips all three optional X characters. Then you move to the end of the string. MCML is the Roman numeral representation of 1950. -
                                                            3 -This matches the start of the string, then the first optional M, then CM, then the optional L and the first optional X, skips the second and third optional X, then the end of the string. MCMLX is the Roman numeral representation of 1960. -
                                                            4 -This matches the start of the string, then the first optional M, then CM, then the optional L and all three optional X characters, then the end of the string. MCMLXXX is the Roman numeral representation of 1980. -
                                                            5 -This matches the start of the string, then the first optional M, then CM, then the optional L and all three optional X characters, then fails to match the end of the string because there is still one more X unaccounted for. So the entire pattern fails to match, and returns None. MCMLXXXX is not a valid Roman numeral. -
                                                            -

                                                            The expression for the ones place follows the same pattern. I'll spare you the details and show you the end result. -

                                                            ->>> pattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'
                                                            -

                                                            So what does that look like using this alternate {n,m} syntax? This example shows the new syntax. -

                                                            Example 7.8. Validating Roman Numerals with {n,m}

                                                            ->>> pattern = '^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
                                                            ->>> re.search(pattern, 'MDLV')             1
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MMDCLXVI')         2
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MMMMDCCCLXXXVIII') 3
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'I')                4
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            -
                                                            - - - - - - - - - - - - - - - - - -
                                                            1 -This matches the start of the string, then one of a possible four M characters, then D?C{0,3}. Of that, it matches the optional D and zero of three possible C characters. Moving on, it matches L?X{0,3} by matching the optional L and zero of three possible X characters. Then it matches V?I{0,3} by matching the optional V and zero of three possible I characters, and finally the end of the string. MDLV is the Roman numeral representation of 1555. -
                                                            2 -This matches the start of the string, then two of a possible four M characters, then the D?C{0,3} with a D and one of three possible C characters; then L?X{0,3} with an L and one of three possible X characters; then V?I{0,3} with a V and one of three possible I characters; then the end of the string. MMDCLXVI is the Roman numeral representation of 2666. -
                                                            3 -This matches the start of the string, then four out of four M characters, then D?C{0,3} with a D and three out of three C characters; then L?X{0,3} with an L and three out of three X characters; then V?I{0,3} with a V and three out of three I characters; then the end of the string. MMMMDCCCLXXXVIII is the Roman numeral representation of 3888, and it's the longest Roman numeral you can write without extended syntax. -
                                                            4 -Watch closely. (I feel like a magician. “Watch closely, kids, I'm going to pull a rabbit out of my hat.”) This matches the start of the string, then zero out of four M, then matches D?C{0,3} by skipping the optional D and matching zero out of three C, then matches L?X{0,3} by skipping the optional L and matching zero out of three X, then matches V?I{0,3} by skipping the optional V and matching one out of three I. Then the end of the string. Whoa. -
                                                            -

                                                            If you followed all that and understood it on the first try, you're doing better than I did. Now imagine trying to understand - someone else's regular expressions, in the middle of a critical function of a large program. Or even imagine coming back - to your own regular expressions a few months later. I've done it, and it's not a pretty sight. -

                                                            In the next section you'll explore an alternate syntax that can help keep your expressions maintainable. -

                                                            7.5. Verbose Regular Expressions

                                                            -

                                                            So far you've just been dealing with what I'll call “compact” regular expressions. As you've seen, they are difficult to read, and even if you figure out what one does, that's no guarantee - that you'll be able to understand it six months later. What you really need is inline documentation. -

                                                            Python allows you to do this with something called verbose regular expressions. A verbose regular expression is different from a compact regular expression in two ways: -

                                                            -
                                                              -
                                                            • Whitespace is ignored. Spaces, tabs, and carriage returns are not matched as spaces, tabs, and carriage returns. They're - not matched at all. (If you want to match a space in a verbose regular expression, you'll need to escape it by putting a - backslash in front of it.) - -
                                                            • Comments are ignored. A comment in a verbose regular expression is just like a comment in Python code: it starts with a # character and goes until the end of the line. In this case it's a comment within a multi-line string instead of within your - source code, but it works the same way. - -
                                                            -

                                                            This will be more clear with an example. Let's revisit the compact regular expression you've been working with, and make -it a verbose regular expression. This example shows how. -

                                                            Example 7.9. Regular Expressions with Inline Comments

                                                            ->>> pattern = """
                                                            -    ^ # beginning of string
                                                            -    M{0,4}              # thousands - 0 to 4 M's
                                                            -    (CM|CD|D?C{0,3})    # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),
                                                            -      #            or 500-800 (D, followed by 0 to 3 C's)
                                                            -    (XC|XL|L?X{0,3})    # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),
                                                            -      #        or 50-80 (L, followed by 0 to 3 X's)
                                                            -    (IX|IV|V?I{0,3})    # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
                                                            -      #        or 5-8 (V, followed by 0 to 3 I's)
                                                            -    $ # end of string
                                                            -    """
                                                            ->>> re.search(pattern, 'M', re.VERBOSE)                1
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MCMLXXXIX', re.VERBOSE)        2
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'MMMMDCCCLXXXVIII', re.VERBOSE) 3
                                                            -<_sre.SRE_Match object at 0x008EEB48>
                                                            ->>> re.search(pattern, 'M')          4
                                                            -
                                                            - - - - - - - - - - - - - - - - - -
                                                            1 -The most important thing to remember when using verbose regular expressions is that you need to pass an extra argument when - working with them: re.VERBOSE is a constant defined in the re module that signals that the pattern should be treated as a verbose regular expression. As you can see, this pattern has - quite a bit of whitespace (all of which is ignored), and several comments (all of which are ignored). Once you ignore the - whitespace and the comments, this is exactly the same regular expression as you saw in the previous section, but it's a lot more readable. -
                                                            2 -This matches the start of the string, then one of a possible four M, then CM, then L and three of a possible three X, then IX, then the end of the string. -
                                                            3 -This matches the start of the string, then four of a possible four M, then D and three of a possible three C, then L and three of a possible three X, then V and three of a possible three I, then the end of the string. -
                                                            4 -This does not match. Why? Because it doesn't have the re.VERBOSE flag, so the re.search function is treating the pattern as a compact regular expression, with significant whitespace and literal hash marks. Python can't auto-detect whether a regular expression is verbose or not. Python assumes every regular expression is compact unless you explicitly state that it is verbose. -
                                                            -

                                                            7.6. Case study: Parsing Phone Numbers

                                                            -

                                                            So far you've concentrated on matching whole patterns. Either the pattern matches, or it doesn't. But regular expressions - are much more powerful than that. When a regular expression does match, you can pick out specific pieces of it. You can find out what matched where. -

                                                            This example came from another real-world problem I encountered, again from a previous day job. The problem: parsing an American -phone number. The client wanted to be able to enter the number free-form (in a single field), but then wanted to store the -area code, trunk, number, and optionally an extension separately in the company's database. I scoured the Web and found many -examples of regular expressions that purported to do this, but none of them were permissive enough. -

                                                            Here are the phone numbers I needed to be able to accept: -

                                                            -
                                                              -
                                                            • 800-555-1212 -
                                                            • 800 555 1212 -
                                                            • 800.555.1212 -
                                                            • (800) 555-1212 -
                                                            • 1-800-555-1212 -
                                                            • 800-555-1212-1234 -
                                                            • 800-555-1212x1234 -
                                                            • 800-555-1212 ext. 1234 -
                                                            • work 1-(800) 555.1212 #1234 -
                                                            -

                                                            Quite a variety! In each of these cases, I need to know that the area code was 800, the trunk was 555, and the rest of the phone number was 1212. For those with an extension, I need to know that the extension was 1234. -

                                                            Let's work through developing a solution for phone number parsing. This example shows the first step. -

                                                            Example 7.10. Finding Numbers

                                                            ->>> phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})$') 1
                                                            ->>> phonePattern.search('800-555-1212').groups()            2
                                                            -('800', '555', '1212')
                                                            ->>> phonePattern.search('800-555-1212-1234')                3
                                                            ->>> 
                                                            -
                                                            - - - - - - - - - - - - - -
                                                            1 -Always read regular expressions from left to right. This one matches the beginning of the string, and then (\d{3}). What's \d{3}? Well, the {3} means “match exactly three numeric digits”; it's a variation on the {n,m} syntax you saw earlier. \d means “any numeric digit” (0 through 9). Putting it in parentheses means “match exactly three numeric digits, and then remember them as a group that I can ask for later”. Then match a literal hyphen. Then match another group of exactly three digits. Then another literal hyphen. Then another - group of exactly four digits. Then match the end of the string. -
                                                            2 -To get access to the groups that the regular expression parser remembered along the way, use the groups() method on the object that the search function returns. It will return a tuple of however many groups were defined in the regular expression. In this case, you - defined three groups, one with three digits, one with three digits, and one with four digits. -
                                                            3 -This regular expression is not the final answer, because it doesn't handle a phone number with an extension on the end. For - that, you'll need to expand the regular expression. -
                                                            -

                                                            Example 7.11. Finding the Extension

                                                            ->>> phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})-(\d+)$') 1
                                                            ->>> phonePattern.search('800-555-1212-1234').groups()             2
                                                            -('800', '555', '1212', '1234')
                                                            ->>> phonePattern.search('800 555 1212 1234')    3
                                                            ->>> 
                                                            ->>> phonePattern.search('800-555-1212')         4
                                                            ->>> 
                                                            -
                                                            - - - - - - - - - - - - - - - - - -
                                                            1 -This regular expression is almost identical to the previous one. Just as before, you match the beginning of the string, then - a remembered group of three digits, then a hyphen, then a remembered group of three digits, then a hyphen, then a remembered - group of four digits. What's new is that you then match another hyphen, and a remembered group of one or more digits, then - the end of the string. -
                                                            2 -The groups() method now returns a tuple of four elements, since the regular expression now defines four groups to remember. -
                                                            3 -Unfortunately, this regular expression is not the final answer either, because it assumes that the different parts of the - phone number are separated by hyphens. What if they're separated by spaces, or commas, or dots? You need a more general - solution to match several different types of separators. -
                                                            4 -Oops! Not only does this regular expression not do everything you want, it's actually a step backwards, because now you can't - parse phone numbers without an extension. That's not what you wanted at all; if the extension is there, you want to know what it is, but if it's not - there, you still want to know what the different parts of the main number are. -
                                                            -

                                                            The next example shows the regular expression to handle separators between the different parts of the phone number. -

                                                            Example 7.12. Handling Different Separators

                                                            ->>> phonePattern = re.compile(r'^(\d{3})\D+(\d{3})\D+(\d{4})\D+(\d+)$') 1
                                                            ->>> phonePattern.search('800 555 1212 1234').groups() 2
                                                            -('800', '555', '1212', '1234')
                                                            ->>> phonePattern.search('800-555-1212-1234').groups() 3
                                                            -('800', '555', '1212', '1234')
                                                            ->>> phonePattern.search('80055512121234')             4
                                                            ->>> 
                                                            ->>> phonePattern.search('800-555-1212')               5
                                                            ->>> 
                                                            -
                                                            - - - - - - - - - - - - - - - - - - - - - -
                                                            1 -Hang on to your hat. You're matching the beginning of the string, then a group of three digits, then \D+. What the heck is that? Well, \D matches any character except a numeric digit, and + means “1 or more”. So \D+ matches one or more characters that are not digits. This is what you're using instead of a literal hyphen, to try to match - different separators. -
                                                            2 -Using \D+ instead of - means you can now match phone numbers where the parts are separated by spaces instead of hyphens. -
                                                            3 -Of course, phone numbers separated by hyphens still work too.
                                                            4 -Unfortunately, this is still not the final answer, because it assumes that there is a separator at all. What if the phone - number is entered without any spaces or hyphens at all? -
                                                            4 -Oops! This still hasn't fixed the problem of requiring extensions. Now you have two problems, but you can solve both of - them with the same technique. -
                                                            -

                                                            The next example shows the regular expression for handling phone numbers without separators. -

                                                            Example 7.13. Handling Numbers Without Separators

                                                            ->>> phonePattern = re.compile(r'^(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$') 1
                                                            ->>> phonePattern.search('80055512121234').groups()    2
                                                            -('800', '555', '1212', '1234')
                                                            ->>> phonePattern.search('800.555.1212 x1234').groups()3
                                                            -('800', '555', '1212', '1234')
                                                            ->>> phonePattern.search('800-555-1212').groups()      4
                                                            -('800', '555', '1212', '')
                                                            ->>> phonePattern.search('(800)5551212 x1234')         5
                                                            ->>> 
                                                            -
                                                            - - - - - - - - - - - - - - - - - - - - - -
                                                            1 -The only change you've made since that last step is changing all the + to *. Instead of \D+ between the parts of the phone number, you now match on \D*. Remember that + means “1 or more”? Well, * means “zero or more”. So now you should be able to parse phone numbers even when there is no separator character at all. -
                                                            2 -Lo and behold, it actually works. Why? You matched the beginning of the string, then a remembered group of three digits - (800), then zero non-numeric characters, then a remembered group of three digits (555), then zero non-numeric characters, then a remembered group of four digits (1212), then zero non-numeric characters, then a remembered group of an arbitrary number of digits (1234), then the end of the string. -
                                                            3 -Other variations work now too: dots instead of hyphens, and both a space and an x before the extension. -
                                                            4 -Finally, you've solved the other long-standing problem: extensions are optional again. If no extension is found, the groups() method still returns a tuple of four elements, but the fourth element is just an empty string. -
                                                            5 -I hate to be the bearer of bad news, but you're not finished yet. What's the problem here? There's an extra character before - the area code, but the regular expression assumes that the area code is the first thing at the beginning of the string. No - problem, you can use the same technique of “zero or more non-numeric characters” to skip over the leading characters before the area code. -
                                                            -

                                                            The next example shows how to handle leading characters in phone numbers. -

                                                            Example 7.14. Handling Leading Characters

                                                            ->>> phonePattern = re.compile(r'^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$') 1
                                                            ->>> phonePattern.search('(800)5551212 ext. 1234').groups()                 2
                                                            -('800', '555', '1212', '1234')
                                                            ->>> phonePattern.search('800-555-1212').groups()         3
                                                            -('800', '555', '1212', '')
                                                            ->>> phonePattern.search('work 1-(800) 555.1212 #1234')   4
                                                            ->>> 
                                                            -
                                                            - - - - - - - - - - - - - - - - - -
                                                            1 -This is the same as in the previous example, except now you're matching \D*, zero or more non-numeric characters, before the first remembered group (the area code). Notice that you're not remembering - these non-numeric characters (they're not in parentheses). If you find them, you'll just skip over them and then start remembering - the area code whenever you get to it. -
                                                            2 -You can successfully parse the phone number, even with the leading left parenthesis before the area code. (The right parenthesis - after the area code is already handled; it's treated as a non-numeric separator and matched by the \D* after the first remembered group.) -
                                                            3 -Just a sanity check to make sure you haven't broken anything that used to work. Since the leading characters are entirely - optional, this matches the beginning of the string, then zero non-numeric characters, then a remembered group of three digits - (800), then one non-numeric character (the hyphen), then a remembered group of three digits (555), then one non-numeric character (the hyphen), then a remembered group of four digits (1212), then zero non-numeric characters, then a remembered group of zero digits, then the end of the string. -
                                                            4 -This is where regular expressions make me want to gouge my eyes out with a blunt object. Why doesn't this phone number match? - Because there's a 1 before the area code, but you assumed that all the leading characters before the area code were non-numeric characters (\D*). Aargh. -
                                                            -

                                                            Let's back up for a second. So far the regular expressions have all matched from the beginning of the string. But now you -see that there may be an indeterminate amount of stuff at the beginning of the string that you want to ignore. Rather than -trying to match it all just so you can skip over it, let's take a different approach: don't explicitly match the beginning -of the string at all. This approach is shown in the next example. -

                                                            Example 7.15. Phone Number, Wherever I May Find Ye

                                                            ->>> phonePattern = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$') 1
                                                            ->>> phonePattern.search('work 1-(800) 555.1212 #1234').groups()        2
                                                            -('800', '555', '1212', '1234')
                                                            ->>> phonePattern.search('800-555-1212')              3
                                                            -('800', '555', '1212', '')
                                                            ->>> phonePattern.search('80055512121234')            4
                                                            -('800', '555', '1212', '1234')
                                                            -
                                                            - - - - - - - - - - - - - - - - - -
                                                            1 -Note the lack of ^ in this regular expression. You are not matching the beginning of the string anymore. There's nothing that says you need - to match the entire input with your regular expression. The regular expression engine will do the hard work of figuring out - where the input string starts to match, and go from there. -
                                                            2 -Now you can successfully parse a phone number that includes leading characters and a leading digit, plus any number of any - kind of separators around each part of the phone number. -
                                                            3 -Sanity check. this still works.
                                                            4 -That still works too.
                                                            -

                                                            See how quickly a regular expression can get out of control? Take a quick glance at any of the previous iterations. Can -you tell the difference between one and the next? -

                                                            While you still understand the final answer (and it is the final answer; if you've discovered a case it doesn't handle, I -don't want to know about it), let's write it out as a verbose regular expression, before you forget why you made the choices -you made. -

                                                            Example 7.16. Parsing Phone Numbers (Final Version)

                                                            ->>> phonePattern = re.compile(r'''
                                                            -                # don't match beginning of string, number can start anywhere
                                                            -    (\d{3})     # area code is 3 digits (e.g. '800')
                                                            -    \D*         # optional separator is any number of non-digits
                                                            -    (\d{3})     # trunk is 3 digits (e.g. '555')
                                                            -    \D*         # optional separator
                                                            -    (\d{4})     # rest of number is 4 digits (e.g. '1212')
                                                            -    \D*         # optional separator
                                                            -    (\d*)       # extension is optional and can be any number of digits
                                                            -    $           # end of string
                                                            -    ''', re.VERBOSE)
                                                            ->>> phonePattern.search('work 1-(800) 555.1212 #1234').groups()        1
                                                            -('800', '555', '1212', '1234')
                                                            ->>> phonePattern.search('800-555-1212')              2
                                                            -('800', '555', '1212', '')
                                                            -
                                                            - - - - - - - - - -
                                                            1 -Other than being spread out over multiple lines, this is exactly the same regular expression as the last step, so it's no - surprise that it parses the same inputs. -
                                                            2 -Final sanity check. Yes, this still works. You're done.
                                                            -
                                                            -

                                                            Further Reading on Regular Expressions

                                                            - -

                                                            7.7. Summary

                                                            -

                                                            This is just the tiniest tip of the iceberg of what regular expressions can do. In other words, even though you're completely - overwhelmed by them now, believe me, you ain't seen nothing yet. -

                                                            You should now be familiar with the following techniques: -

                                                            -
                                                              -
                                                            • ^ matches the beginning of a string. - -
                                                            • $ matches the end of a string. - -
                                                            • \b matches a word boundary. - -
                                                            • \d matches any numeric digit. - -
                                                            • \D matches any non-numeric character. - -
                                                            • x? matches an optional x character (in other words, it matches an x zero or one times). - -
                                                            • x* matches x zero or more times. - -
                                                            • x+ matches x one or more times. - -
                                                            • x{n,m} matches an x character at least n times, but not more than m times. - -
                                                            • (a|b|c) matches either a or b or c. - -
                                                            • (x) in general is a remembered group. You can get the value of what matched by using the groups() method of the object returned by re.search. - -
                                                            -

                                                            Regular expressions are extremely powerful, but they are not the correct solution for every problem. You should learn enough -about them to know when they are appropriate, when they will solve your problems, and when they will cause more problems than -they solve. -

                                                            - - - - - - - - - - -
                                                            -

                                                            Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. -

                                                            --Jamie Zawinski, in comp.emacs.xemacs
                                                            -
                                                            +

                                                            Chapter 8. HTML Processing

                                                            8.1. Diving in

                                                            I often see questions on comp.lang.python like “How can I list all the [headers|images|links] in my HTML document?” “How do I parse/translate/munge the text of my HTML document but leave the tags alone?” “How can I add/remove/quote attributes of all my HTML tags at once?” This chapter will answer all of these questions.

                                                            Here is a complete, working Python program in two parts. The first part, BaseHTMLProcessor.py, is a generic tool to help you process HTML files by walking through the tags and text blocks. The second part, dialect.py, is an example of how to use BaseHTMLProcessor.py to translate the text of an HTML document but leave the tags alone. Read the docstrings and comments to get an overview of what's going on. Most of it will seem like black magic, because it's not obvious how any of these class methods ever get called. Don't worry, all will be revealed in due time. -

                                                            Example 8.1. BaseHTMLProcessor.py

                                                            -

                                                            If you have not already done so, you can download this and other examples used in this book.

                                                            +

                                                            Example 8.1. BaseHTMLProcessor.py

                                                            +

                                                            If you have not already done so, you can download this and other examples used in this book. +

                                                            
                                                             from sgmllib import SGMLParser
                                                             import htmlentitydefs
                                                             
                                                            @@ -5227,7 +2849,7 @@ class BaseHTMLProcessor(SGMLParser):
                                                                 def unknown_starttag(self, tag, attrs):
                                                                     # called for each start tag
                                                                     # attrs is a list of (attr, value) tuples
                                                            -        # e.g. for <pre class="screen">, tag="pre", attrs=[("class", "screen")]
                                                            +        # e.g. for <pre class=screen>, tag="pre", attrs=[("class", "screen")]
                                                                     # Ideally we would like to reconstruct original tag and attributes, but
                                                                     # we may end up quoting attribute values that weren't quoted in the source
                                                                     # document, or we may change the type of quotes around the attribute value
                                                            @@ -5285,7 +2907,7 @@ class BaseHTMLProcessor(SGMLParser):
                                                             
                                                                 def output(self):              
                                                                     """Return processed HTML as a single string"""
                                                            -        return "".join(self.pieces)

                                                            Example 8.2. dialect.py

                                                            +        return "".join(self.pieces)

                                                            Example 8.2. dialect.py

                                                            
                                                             import re
                                                             from BaseHTMLProcessor import BaseHTMLProcessor
                                                             
                                                            @@ -5438,15 +3060,16 @@ def test(url):
                                                                     webbrowser.open_new(outfile)
                                                             
                                                             if __name__ == "__main__":
                                                            -    test("http://diveintopython3.org/odbchelper_list.html")

                                                            Example 8.3. Output of dialect.py

                                                            -

                                                            Running this script will translate Section 3.2, “Introducing Lists” into mock Swedish Chef-speak (from The Muppets), mock Elmer Fudd-speak (from Bugs Bunny cartoons), and mock Middle English (loosely based on Chaucer's The Canterbury Tales). If you look at the HTML source of the output pages, you'll see that all the HTML tags and attributes are untouched, but the text between the tags has been “translated” into the mock language. If you look closer, you'll see that, in fact, only the titles and paragraphs were translated; the - code listings and screen examples were left untouched.

                                                            -<div class="abstract">
                                                            -<p>Lists awe <span class="application">Pydon</span>'s wowkhowse datatype.
                                                            +    test("http://diveintopython3.org/odbchelper_list.html")

                                                            Example 8.3. Output of dialect.py

                                                            +

                                                            Running this script will translate Section 3.2, “Introducing Lists” into mock Swedish Chef-speak (from The Muppets), mock Elmer Fudd-speak (from Bugs Bunny cartoons), and mock Middle English (loosely based on Chaucer's The Canterbury Tales). If you look at the HTML source of the output pages, you'll see that all the HTML tags and attributes are untouched, but the text between the tags has been “translated” into the mock language. If you look closer, you'll see that, in fact, only the titles and paragraphs were translated; the + code listings and screen examples were left untouched. +

                                                            
                                                            +<div class=abstract>
                                                            +<p>Lists awe <span class=application>Pydon</span>'s wowkhowse datatype.
                                                             If youw onwy expewience wif wists is awways in
                                                            -<span class="application">Visuaw Basic</span> ow (God fowbid) de datastowe
                                                            -in <span class="application">Powewbuiwdew</span>, bwace youwsewf fow
                                                            -<span class="application">Pydon</span> wists.</p>
                                                            +<span class=application>Visuaw Basic</span> ow (God fowbid) de datastowe
                                                            +in <span class=application>Powewbuiwdew</span>, bwace youwsewf fow
                                                            +<span class=application>Pydon</span> wists.</p>
                                                             </div>
                                                             

                                                            8.2. Introducing sgmllib.py

                                                            HTML processing is broken into three steps: breaking down the HTML into its constituent pieces, fiddling with the pieces, and reconstructing the pieces into HTML again. The first step is done by sgmllib.py, a part of the standard Python library. @@ -5455,7 +3078,7 @@ and end tags. Usually you don't work with HTML this way; you

                                                            sgmllib.py contains one important class: SGMLParser. SGMLParser parses HTML into useful pieces, like start tags and end tags. As soon as it succeeds in breaking down some data into a useful piece, it calls a method on itself based on what it found. In order to use the parser, you subclass the SGMLParser class and override these methods. This is what I meant when I said that it presents HTML structurally: the structure of the HTML determines the sequence of method calls and the arguments passed to each method.

                                                            SGMLParser parses HTML into 8 kinds of data, and calls a separate method for each of them: -

                                                            +
                                                            Start tag
                                                            An HTML tag that starts a block, like <html>, <head>, <body>, or <pre>, or a standalone tag like <br> or <img>. When it finds a start tag tagname, SGMLParser will look for a method called start_tagname or do_tagname. For instance, when it finds a <pre> tag, it will look for a start_pre or do_pre method. If found, SGMLParser calls this method with a list of the tag's attributes; otherwise, it calls unknown_starttag with the tag name and list of attributes. @@ -5482,28 +3105,18 @@ it calls a method on itself based on what it found. In order to use the parser,
                                                            A block of text. Anything that doesn't fit into the other 7 categories. When found, SGMLParser calls handle_data with the text.
                                                            -
                                                            - - - - - - -
                                                            Important
                                                            Python 2.0 had a bug where SGMLParser would not recognize declarations at all (handle_decl would never be called), which meant that DOCTYPEs were silently ignored. This is fixed in Python 2.1. -
                                                            + + +
                                                            ImportantPython 2.0 had a bug where SGMLParser would not recognize declarations at all (handle_decl would never be called), which meant that DOCTYPEs were silently ignored. This is fixed in Python 2.1.

                                                            sgmllib.py comes with a test suite to illustrate this. You can run sgmllib.py, passing the name of an HTML file on the command line, and it will print out the tags and other elements as it parses them. It does this by subclassing -the SGMLParser class and defining unknown_starttag, unknown_endtag, handle_data and other methods which simply print their arguments. - - - - - - -
                                                            Tip
                                                            In the ActivePython IDE on Windows, you can specify command line arguments in the “Run script” dialog. Separate multiple arguments with spaces. -
                                                            -

                                                            Example 8.4. Sample test of sgmllib.py

                                                            -

                                                            Here is a snippet from the table of contents of the HTML version of this book. Of course your paths may vary. (If you haven't downloaded the HTML version of the book, you can do so at http://diveintopython3.org/.

                                                            -c:\python23\lib> type "c:\downloads\diveintopython3\html\toc\index.html"
                                                            +the SGMLParser class and defining unknown_starttag, unknown_endtag, handle_data and other methods which simply print their arguments.
                                                            +
                                                            +
                                                            +
                                                            TipIn the ActivePython IDE on Windows, you can specify command line arguments in the “Run script” dialog. Separate multiple arguments with spaces. +

                                                            Example 8.4. Sample test of sgmllib.py

                                                            +

                                                            Here is a snippet from the table of contents of the HTML version of this book. Of course your paths may vary. (If you haven't downloaded the HTML version of the book, you can do so at http://diveintopython3.org/. +

                                                            +c:\python23\lib> type "c:\downloads\diveintopython3\html\toc\index.html"
                                                             
                                                             <!DOCTYPE html
                                                               PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                                                            @@ -5515,9 +3128,9 @@ the SGMLParser class and defining unknown_starttag, 
                                                             
                                                             ... rest of file omitted for brevity ...
                                                            -

                                                            Running this through the test suite of sgmllib.py yields this output:

                                                            -c:\python23\lib> python sgmllib.py "c:\downloads\diveintopython3\html\toc\index.html"
                                                            -data: '\n\n'
                                                            +

                                                            Running this through the test suite of sgmllib.py yields this output:

                                                            +c:\python23\lib> python sgmllib.py "c:\downloads\diveintopython3\html\toc\index.html"
                                                            +data: '\n\n'
                                                             start tag: <html >
                                                             data: '\n   '
                                                             start tag: <head>
                                                            @@ -5533,7 +3146,7 @@ data: '\n      '
                                                             
                                                             ... rest of output omitted for brevity ...
                                                             

                                                            Here's the roadmap for the rest of the chapter: -

                                                            +
                                                            • Subclass SGMLParser to create classes that extract interesting data out of HTML documents. @@ -5550,13 +3163,13 @@ data: '\n '

                                                              8.3. Extracting data from HTML documents

                                                              To extract data from HTML documents, subclass the SGMLParser class and define methods for each tag or entity you want to capture.

                                                              The first step to extracting data from an HTML document is getting some HTML. If you have some HTML lying around on your hard drive, you can use file functions to read it, but the real fun begins when you get HTML from live web pages. -

                                                              Example 8.5. Introducing urllib

                                                              ->>> import urllib   1
                                                              ->>> sock = urllib.urlopen("http://diveintopython3.org/") 2
                                                              ->>> htmlSource = sock.read()          3
                                                              ->>> sock.close()    4
                                                              ->>> print htmlSource5
                                                              -<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head>
                                                              +

                                                              Example 8.5. Introducing urllib

                                                              +>>> import urllib   
                                                              +>>> sock = urllib.urlopen("http://diveintopython3.org/") 
                                                              +>>> htmlSource = sock.read()          
                                                              +>>> sock.close()    
                                                              +>>> print htmlSource
                                                              +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head>
                                                                     <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
                                                                  <title>Dive Into Python</title>
                                                               <link rel='stylesheet' href='diveintopython3.css' type='text/css'>
                                                              @@ -5570,88 +3183,42 @@ data: '\n      '
                                                               <td width='99%' align='right'><hr size='1' noshade></td></tr>
                                                               <tr><td class='tagline' colspan='2'>Python&nbsp;for&nbsp;experienced&nbsp;programmers</td></tr>
                                                               
                                                              -[...snip...]
                                                              - - - - - - - - - - - - - - - - - - - - - -
                                                              1 -The urllib module is part of the standard Python library. It contains functions for getting information about and actually retrieving data from Internet-based URLs (mainly web pages). -
                                                              2 -The simplest use of urllib is to retrieve the entire text of a web page using the urlopen function. Opening a URL is similar to opening a file. The return value of urlopen is a file-like object, which has some of the same methods as a file object. -
                                                              3 -The simplest thing to do with the file-like object returned by urlopen is read, which reads the entire HTML of the web page into a single string. The object also supports readlines, which reads the text line by line into a list. -
                                                              4 -When you're done with the object, make sure to close it, just like a normal file object. -
                                                              5 -You now have the complete HTML of the home page of http://diveintopython3.org/ in a string, and you're ready to parse it. -
                                                              -
                                                              -

                                                              If you have not already done so, you can download this and other examples used in this book.

                                                              +[...snip...]
                                                              +
                                                                +
                                                              1. The urllib module is part of the standard Python library. It contains functions for getting information about and actually retrieving data from Internet-based URLs (mainly web pages). +
                                                              2. The simplest use of urllib is to retrieve the entire text of a web page using the urlopen function. Opening a URL is similar to opening a file. The return value of urlopen is a file-like object, which has some of the same methods as a file object. +
                                                              3. The simplest thing to do with the file-like object returned by urlopen is read, which reads the entire HTML of the web page into a single string. The object also supports readlines, which reads the text line by line into a list. +
                                                              4. When you're done with the object, make sure to close it, just like a normal file object. +
                                                              5. You now have the complete HTML of the home page of http://diveintopython3.org/ in a string, and you're ready to parse it. +
                                                                +

                                                                If you have not already done so, you can download this and other examples used in this book. +

                                                                
                                                                 from sgmllib import SGMLParser
                                                                 
                                                                 class URLLister(SGMLParser):
                                                                -    def reset(self):            1
                                                                +    def reset(self):            
                                                                         SGMLParser.reset(self)
                                                                         self.urls = []
                                                                 
                                                                -    def start_a(self, attrs):   2
                                                                -        href = [v for k, v in attrs if k=='href'] 3 4
                                                                +    def start_a(self, attrs):   
                                                                +        href = [v for k, v in attrs if k=='href']  
                                                                         if href:
                                                                -            self.urls.extend(href)
                                                                - - - - - - - - - - - - - - - - - -
                                                                1 -reset is called by the __init__ method of SGMLParser, and it can also be called manually once an instance of the parser has been created. So if you need to do any initialization, + self.urls.extend(href)
                                                                +
                                                                  +
                                                                1. reset is called by the __init__ method of SGMLParser, and it can also be called manually once an instance of the parser has been created. So if you need to do any initialization, do it in reset, not in __init__, so that it will be re-initialized properly when someone re-uses a parser instance. -
                                                                2 -start_a is called by SGMLParser whenever it finds an <a> tag. The tag may contain an href attribute, and/or other attributes, like name or title. The attrs parameter is a list of tuples, [(attribute, value), (attribute, value), ...]. Or it may be just an <a>, a valid (if useless) HTML tag, in which case attrs would be an empty list. -
                                                                3 -You can find out whether this <a> tag has an href attribute with a simple multi-variable list comprehension. -
                                                                4 -String comparisons like k=='href' are always case-sensitive, but that's safe in this case, because SGMLParser converts attribute names to lowercase while building attrs. -
                                                                -

                                                                Example 8.7. Using urllister.py

                                                                ->>> import urllib, urllister
                                                                ->>> usock = urllib.urlopen("http://diveintopython3.org/")
                                                                ->>> parser = urllister.URLLister()
                                                                ->>> parser.feed(usock.read())         1
                                                                ->>> usock.close()   2
                                                                ->>> parser.close()  3
                                                                ->>> for url in parser.urls: print url 4
                                                                -toc/index.html
                                                                +
                                                              6. start_a is called by SGMLParser whenever it finds an <a> tag. The tag may contain an href attribute, and/or other attributes, like name or title. The attrs parameter is a list of tuples, [(attribute, value), (attribute, value), ...]. Or it may be just an <a>, a valid (if useless) HTML tag, in which case attrs would be an empty list. +
                                                              7. You can find out whether this <a> tag has an href attribute with a simple multi-variable list comprehension. +
                                                              8. String comparisons like k=='href' are always case-sensitive, but that's safe in this case, because SGMLParser converts attribute names to lowercase while building attrs. +

                                                                Example 8.7. Using urllister.py

                                                                +>>> import urllib, urllister
                                                                +>>> usock = urllib.urlopen("http://diveintopython3.org/")
                                                                +>>> parser = urllister.URLLister()
                                                                +>>> parser.feed(usock.read())         
                                                                +>>> usock.close()   
                                                                +>>> parser.close()  
                                                                +>>> for url in parser.urls: print url 
                                                                +toc/index.html
                                                                 #download
                                                                 #languages
                                                                 toc/index.html
                                                                @@ -5665,161 +3232,84 @@ download/diveintopython3-xml-5.0.zip
                                                                 download/diveintopython3-common-5.0.zip
                                                                 
                                                                 
                                                                -... rest of output omitted for brevity ...
                                                                - - - - - - - - - - - - - - - - - -
                                                                1 -Call the feed method, defined in SGMLParser, to get HTML into the parser.[1] It takes a string, which is what usock.read() returns. -
                                                                2 -Like files, you should close your URL objects as soon as you're done with them. -
                                                                3 -You should close your parser object, too, but for a different reason. You've read all the data and fed it to the parser, but the feed method isn't guaranteed to have actually processed all the HTML you give it; it may buffer it, waiting for more. Be sure to call close to flush the buffer and force everything to be fully parsed. -
                                                                4 -Once the parser is closed, the parsing is complete, and parser.urls contains a list of all the linked URLs in the HTML document. (Your output may look different, if the download links have been updated by the time you read this.) -
                                                                +... rest of output omitted for brevity ...
                                                              9. +
                                                                  +
                                                                1. Call the feed method, defined in SGMLParser, to get HTML into the parser. +[1] It takes a string, which is what usock.read() returns. +
                                                                2. Like files, you should close your URL objects as soon as you're done with them. +
                                                                3. You should close your parser object, too, but for a different reason. You've read all the data and fed it to the parser, but the feed method isn't guaranteed to have actually processed all the HTML you give it; it may buffer it, waiting for more. Be sure to call close to flush the buffer and force everything to be fully parsed. +
                                                                4. Once the parser is closed, the parsing is complete, and parser.urls contains a list of all the linked URLs in the HTML document. (Your output may look different, if the download links have been updated by the time you read this.)

                                                                  8.4. Introducing BaseHTMLProcessor.py

                                                                  SGMLParser doesn't produce anything by itself. It parses and parses and parses, and it calls a method for each interesting thing it finds, but the methods don't do anything. SGMLParser is an HTML consumer: it takes HTML and breaks it down into small, structured pieces. As you saw in the previous section, you can subclass SGMLParser to define classes that catch specific tags and produce useful things, like a list of all the links on a web page. Now you'll take this one step further by defining a class that catches everything SGMLParser throws at it and reconstructs the complete HTML document. In technical terms, this class will be an HTML producer.

                                                                  BaseHTMLProcessor subclasses SGMLParser and provides all 8 essential handler methods: unknown_starttag, unknown_endtag, handle_charref, handle_entityref, handle_comment, handle_pi, handle_decl, and handle_data. -

                                                                  Example 8.8. Introducing BaseHTMLProcessor

                                                                  +

                                                                  Example 8.8. Introducing BaseHTMLProcessor

                                                                  
                                                                   class BaseHTMLProcessor(SGMLParser):
                                                                  -    def reset(self):      1
                                                                  +    def reset(self):      
                                                                           self.pieces = []
                                                                           SGMLParser.reset(self)
                                                                   
                                                                  -    def unknown_starttag(self, tag, attrs): 2
                                                                  +    def unknown_starttag(self, tag, attrs): 
                                                                           strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs])
                                                                           self.pieces.append("<%(tag)s%(strattrs)s>" % locals())
                                                                   
                                                                  -    def unknown_endtag(self, tag):          3
                                                                  +    def unknown_endtag(self, tag):          
                                                                           self.pieces.append("</%(tag)s>" % locals())
                                                                   
                                                                  -    def handle_charref(self, ref):          4
                                                                  +    def handle_charref(self, ref):          
                                                                           self.pieces.append("&#%(ref)s;" % locals())
                                                                   
                                                                  -    def handle_entityref(self, ref):        5
                                                                  +    def handle_entityref(self, ref):        
                                                                           self.pieces.append("&%(ref)s" % locals())
                                                                           if htmlentitydefs.entitydefs.has_key(ref):
                                                                               self.pieces.append(";")
                                                                   
                                                                  -    def handle_data(self, text):            6
                                                                  +    def handle_data(self, text):            
                                                                           self.pieces.append(text)
                                                                   
                                                                  -    def handle_comment(self, text):         7
                                                                  +    def handle_comment(self, text):         
                                                                           self.pieces.append("<!--%(text)s-->" % locals())
                                                                   
                                                                  -    def handle_pi(self, text):              8
                                                                  +    def handle_pi(self, text):              
                                                                           self.pieces.append("<?%(text)s>" % locals())
                                                                   
                                                                       def handle_decl(self, text):
                                                                  -        self.pieces.append("<!%(text)s>" % locals())
                                                                  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                  1 -reset, called by SGMLParser.__init__, initializes self.pieces as an empty list before calling the ancestor method. self.pieces is a data attribute which will hold the pieces of the HTML document you're constructing. Each handler method will reconstruct the HTML that SGMLParser parsed, and each method will append that string to self.pieces. Note that self.pieces is a list. You might be tempted to define it as a string and just keep appending each piece to it. That would work, but -Python is much more efficient at dealing with lists.[2]
                                                                  2 -Since BaseHTMLProcessor does not define any methods for specific tags (like the start_a method in URLLister), SGMLParser will call unknown_starttag for every start tag. This method takes the tag (tag) and the list of attribute name/value pairs (attrs), reconstructs the original HTML, and appends it to self.pieces. The string formatting here is a little strange; you'll untangle that (and also the odd-looking locals function) later in this chapter. -
                                                                  3 -Reconstructing end tags is much simpler; just take the tag name and wrap it in the </...> brackets. -
                                                                  4 -When SGMLParser finds a character reference, it calls handle_charref with the bare reference. If the HTML document contains the reference &#160;, ref will be 160. Reconstructing the original complete character reference just involves wrapping ref in &#...; characters. -
                                                                  5 -Entity references are similar to character references, but without the hash mark. Reconstructing the original entity reference + self.pieces.append("<!%(text)s>" % locals())
                                                                  +
                                                                    +
                                                                  1. reset, called by SGMLParser.__init__, initializes self.pieces as an empty list before calling the ancestor method. self.pieces is a data attribute which will hold the pieces of the HTML document you're constructing. Each handler method will reconstruct the HTML that SGMLParser parsed, and each method will append that string to self.pieces. Note that self.pieces is a list. You might be tempted to define it as a string and just keep appending each piece to it. That would work, but +Python is much more efficient at dealing with lists. +[2]
                                                                  2. Since BaseHTMLProcessor does not define any methods for specific tags (like the start_a method in URLLister), SGMLParser will call unknown_starttag for every start tag. This method takes the tag (tag) and the list of attribute name/value pairs (attrs), reconstructs the original HTML, and appends it to self.pieces. The string formatting here is a little strange; you'll untangle that (and also the odd-looking locals function) later in this chapter. +
                                                                  3. Reconstructing end tags is much simpler; just take the tag name and wrap it in the </...> brackets. +
                                                                  4. When SGMLParser finds a character reference, it calls handle_charref with the bare reference. If the HTML document contains the reference &#160;, ref will be 160. Reconstructing the original complete character reference just involves wrapping ref in &#...; characters. +
                                                                  5. Entity references are similar to character references, but without the hash mark. Reconstructing the original entity reference requires wrapping ref in &...; characters. (Actually, as an erudite reader pointed out to me, it's slightly more complicated than this. Only certain standard HTML entites end in a semicolon; other similar-looking entities do not. Luckily for us, the set of standard HTML entities is defined in a dictionary in a Python module called htmlentitydefs. Hence the extra if statement.) -
                                                                  6 -Blocks of text are simply appended to self.pieces unaltered. -
                                                                  7 -HTML comments are wrapped in <!--...--> characters. -
                                                                  8 -Processing instructions are wrapped in <?...> characters. -
                                                                  -
                                                                  - - - - -
                                                                  Important
                                                                  The HTML specification requires that all non-HTML (like client-side JavaScript) must be enclosed in HTML comments, but not all web pages do this properly (and all modern web browsers are forgiving if they don't). BaseHTMLProcessor is not forgiving; if script is improperly embedded, it will be parsed as if it were HTML. For instance, if the script contains less-than and equals signs, SGMLParser may incorrectly think that it has found tags and attributes. SGMLParser always converts tags and attribute names to lowercase, which may break the script, and BaseHTMLProcessor always encloses attribute values in double quotes (even if the original HTML document used single quotes or no quotes), which will certainly break the script. Always protect your client-side script +
                                                                5. Blocks of text are simply appended to self.pieces unaltered. +
                                                                6. HTML comments are wrapped in <!--...--> characters. +
                                                                7. Processing instructions are wrapped in <?...> characters. + + + - -
                                                                  ImportantThe HTML specification requires that all non-HTML (like client-side JavaScript) must be enclosed in HTML comments, but not all web pages do this properly (and all modern web browsers are forgiving if they don't). BaseHTMLProcessor is not forgiving; if script is improperly embedded, it will be parsed as if it were HTML. For instance, if the script contains less-than and equals signs, SGMLParser may incorrectly think that it has found tags and attributes. SGMLParser always converts tags and attribute names to lowercase, which may break the script, and BaseHTMLProcessor always encloses attribute values in double quotes (even if the original HTML document used single quotes or no quotes), which will certainly break the script. Always protect your client-side script within HTML comments. -
                                                                  -

                                                                  Example 8.9. BaseHTMLProcessor output

                                                                  -    def output(self):               1
                                                                  +

                                                                  Example 8.9. BaseHTMLProcessor output

                                                                  
                                                                  +    def output(self):               
                                                                           """Return processed HTML as a single string"""
                                                                  -        return "".join(self.pieces) 2
                                                                  - - - - - - - - - -
                                                                  1 -This is the one method in BaseHTMLProcessor that is never called by the ancestor SGMLParser. Since the other handler methods store their reconstructed HTML in self.pieces, this function is needed to join all those pieces into one string. As noted before, Python is great at lists and mediocre at strings, so you only create the complete string when somebody explicitly asks for it. -
                                                                  2 -If you prefer, you could use the join method of the string module instead: string.join(self.pieces, "")
                                                                  -
                                                                  + return "".join(self.pieces)
                                                                  +
                                                                    +
                                                                  1. This is the one method in BaseHTMLProcessor that is never called by the ancestor SGMLParser. Since the other handler methods store their reconstructed HTML in self.pieces, this function is needed to join all those pieces into one string. As noted before, Python is great at lists and mediocre at strings, so you only create the complete string when somebody explicitly asks for it. +
                                                                  2. If you prefer, you could use the join method of the string module instead: string.join(self.pieces, "")

                                                                    Further reading

                                                                    8.5. locals and globals

                                                                    Let's digress from HTML processing for a minute and talk about how Python handles variables. Python has two built-in functions, locals and globals, which provide dictionary-based access to local and global variables.

                                                                    Remember locals? You first saw it here: -

                                                                    +
                                                                    
                                                                         def unknown_starttag(self, tag, attrs):
                                                                             strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs])
                                                                             self.pieces.append("<%(tag)s%(strattrs)s>" % locals())
                                                                    @@ -5832,7 +3322,7 @@ own namespace, called the global namespace, which keeps track of the module's va
                                                                     other imported modules, and module-level variables and constants. And there is the built-in namespace, accessible from any
                                                                     module, which holds built-in functions and exceptions.
                                                                     

                                                                    When a line of code asks for the value of a variable x, Python will search for that variable in all the available namespaces, in order: -

                                                                    +
                                                                    1. local namespace - specific to the current function or class method. If the function defines a local variable x, or has an argument x, Python will use this and stop searching. @@ -5841,216 +3331,113 @@ module, which holds built-in functions and exceptions.
                                                                    2. built-in namespace - global to all modules. As a last resort, Python will assume that x is the name of built-in function or variable.
                                                                    -

                                                                    If Python doesn't find x in any of these namespaces, it gives up and raises a NameError with the message There is no variable named 'x', which you saw back in Example 3.18, “Referencing an Unbound Variable”, but you didn't appreciate how much work Python was doing before giving you that error. - - - - - - -
                                                                    Important
                                                                    Python 2.2 introduced a subtle but important change that affects the namespace search order: nested scopes. In versions of Python prior to 2.2, when you reference a variable within a nested function or lambda function, Python will search for that variable in the current (nested or lambda) function's namespace, then in the module's namespace. Python 2.2 will search for the variable in the current (nested or lambda) function's namespace, then in the parent function's namespace, then in the module's namespace. Python 2.1 can work either way; by default, it works like Python 2.0, but you can add the following line of code at the top of your module to make your module work like Python 2.2:
                                                                    -from __future__ import nested_scopes
                                                                    -

                                                                    Are you confused yet? Don't despair! This is really cool, I promise. Like many things in Python, namespaces are directly accessible at run-time. How? Well, the local namespace is accessible via the built-in locals function, and the global (module level) namespace is accessible via the built-in globals function. -

                                                                    Example 8.10. Introducing locals

                                                                    >>> def foo(arg): 1
                                                                    -...    x = 1
                                                                    -...    print locals()
                                                                    -...    
                                                                    ->>> foo(7)        2
                                                                    +

                                                                    If Python doesn't find x in any of these namespaces, it gives up and raises a NameError with the message There is no variable named 'x', which you saw back in Example 3.18, “Referencing an Unbound Variable”, but you didn't appreciate how much work Python was doing before giving you that error. + + +
                                                                    ImportantPython 2.2 introduced a subtle but important change that affects the namespace search order: nested scopes. In versions of Python prior to 2.2, when you reference a variable within a nested function or lambda function, Python will search for that variable in the current (nested or lambda) function's namespace, then in the module's namespace. Python 2.2 will search for the variable in the current (nested or lambda) function's namespace, then in the parent function's namespace, then in the module's namespace. Python 2.1 can work either way; by default, it works like Python 2.0, but you can add the following line of code at the top of your module to make your module work like Python 2.2:
                                                                    
                                                                    +from __future__ import nested_scopes

                                                                    Are you confused yet? Don't despair! This is really cool, I promise. Like many things in Python, namespaces are directly accessible at run-time. How? Well, the local namespace is accessible via the built-in locals function, and the global (module level) namespace is accessible via the built-in globals function. +

                                                                    Example 8.10. Introducing locals

                                                                    >>> def foo(arg): 
                                                                    +...    x = 1
                                                                    +...    print locals()
                                                                    +...    
                                                                    +>>> foo(7)        
                                                                     {'arg': 7, 'x': 1}
                                                                    ->>> foo('bar')    3
                                                                    -{'arg': 'bar', 'x': 1}
                                                                    - - - - - - - - - - - - - -
                                                                    1 -The function foo has two variables in its local namespace: arg, whose value is passed in to the function, and x, which is defined within the function. -
                                                                    2 -locals returns a dictionary of name/value pairs. The keys of this dictionary are the names of the variables as strings; the values +>>> foo('bar') +{'arg': 'bar', 'x': 1}
                                                                    +
                                                                      +
                                                                    1. The function foo has two variables in its local namespace: arg, whose value is passed in to the function, and x, which is defined within the function. +
                                                                    2. locals returns a dictionary of name/value pairs. The keys of this dictionary are the names of the variables as strings; the values of the dictionary are the actual values of the variables. So calling foo with 7 prints the dictionary containing the function's two local variables: arg (7) and x (1). -
                                                                    3 -Remember, Python has dynamic typing, so you could just as easily pass a string in for arg; the function (and the call to locals) would still work just as well. locals works with all variables of all datatypes. -
                                                                    -

                                                                    What locals does for the local (function) namespace, globals does for the global (module) namespace. globals is more exciting, though, because a module's namespace is more exciting.[3] Not only does the module's namespace include module-level variables and constants, it includes all the functions and classes +

                                                                  3. Remember, Python has dynamic typing, so you could just as easily pass a string in for arg; the function (and the call to locals) would still work just as well. locals works with all variables of all datatypes. +

                                                                    What locals does for the local (function) namespace, globals does for the global (module) namespace. globals is more exciting, though, because a module's namespace is more exciting. +[3] Not only does the module's namespace include module-level variables and constants, it includes all the functions and classes defined in the module. Plus, it includes anything that was imported into the module.

                                                                    Remember the difference between from module import and import module? With import module, the module itself is imported, but it retains its own namespace, which is why you need to use the module name to access any of its functions or attributes: module.function. But with from module import, you're actually importing specific functions and attributes from another module into your own namespace, which is why you access them directly without referencing the original module they came from. With the globals function, you can actually see this happen. -

                                                                    Example 8.11. Introducing globals

                                                                    -

                                                                    Look at the following block of code at the bottom of BaseHTMLProcessor.py:

                                                                    +

                                                                    Example 8.11. Introducing globals

                                                                    +

                                                                    Look at the following block of code at the bottom of BaseHTMLProcessor.py:

                                                                    
                                                                     if __name__ == "__main__":
                                                                    -    for k, v in globals().items():             1
                                                                    -        print k, "=", v
                                                                    - - - - - -
                                                                    1 -Just so you don't get intimidated, remember that you've seen all this before. The globals function returns a dictionary, and you're iterating through the dictionary using the items method and multi-variable assignment. The only thing new here is the globals function. -
                                                                    + for k, v in globals().items(): + print k, "=", v
                                                                    +
                                                                      +
                                                                    1. Just so you don't get intimidated, remember that you've seen all this before. The globals function returns a dictionary, and you're iterating through the dictionary using the items method and multi-variable assignment. The only thing new here is the globals function.

                                                                      Now running the script from the command line gives this output (note that your output may be slightly different, depending - on your platform and where you installed Python):

                                                                      c:\docbook\dip\py> python BaseHTMLProcessor.py
                                                                      -SGMLParser = sgmllib.SGMLParser                1
                                                                      -htmlentitydefs = <module 'htmlentitydefs' from 'C:\Python23\lib\htmlentitydefs.py'> 2
                                                                      -BaseHTMLProcessor = __main__.BaseHTMLProcessor 3
                                                                      -__name__ = __main__          4
                                                                      -... rest of output omitted for brevity...
                                                                      - - - - - - - - - - - - - - - - - -
                                                                      1 -SGMLParser was imported from sgmllib, using from module import. That means that it was imported directly into the module's namespace, and here it is. -
                                                                      2 -Contrast this with htmlentitydefs, which was imported using import. That means that the htmlentitydefs module itself is in the namespace, but the entitydefs variable defined within htmlentitydefs is not. -
                                                                      3 -This module only defines one class, BaseHTMLProcessor, and here it is. Note that the value here is the class itself, not a specific instance of the class. -
                                                                      4 -Remember the if __name__ trick? When running a module (as opposed to importing it from another module), the built-in __name__ attribute is a special value, __main__. Since you ran this module as a script from the command line, __name__ is __main__, which is why the little test code to print the globals got executed. -
                                                                      -
                                                                      - - - - -
                                                                      Note
                                                                      Using the locals and globals functions, you can get the value of arbitrary variables dynamically, providing the variable name as a string. This mirrors + on your platform and where you installed Python):
                                                                      c:\docbook\dip\py> python BaseHTMLProcessor.py
                                                                      
                                                                      +SGMLParser = sgmllib.SGMLParser                
                                                                      +htmlentitydefs = <module 'htmlentitydefs' from 'C:\Python23\lib\htmlentitydefs.py'> 
                                                                      +BaseHTMLProcessor = __main__.BaseHTMLProcessor 
                                                                      +__name__ = __main__          
                                                                      +... rest of output omitted for brevity...
                                                                      +
                                                                        +
                                                                      1. SGMLParser was imported from sgmllib, using from module import. That means that it was imported directly into the module's namespace, and here it is. +
                                                                      2. Contrast this with htmlentitydefs, which was imported using import. That means that the htmlentitydefs module itself is in the namespace, but the entitydefs variable defined within htmlentitydefs is not. +
                                                                      3. This module only defines one class, BaseHTMLProcessor, and here it is. Note that the value here is the class itself, not a specific instance of the class. +
                                                                      4. Remember the if __name__ trick? When running a module (as opposed to importing it from another module), the built-in __name__ attribute is a special value, __main__. Since you ran this module as a script from the command line, __name__ is __main__, which is why the little test code to print the globals got executed. + + + - -
                                                                        NoteUsing the locals and globals functions, you can get the value of arbitrary variables dynamically, providing the variable name as a string. This mirrors the functionality of the getattr function, which allows you to access arbitrary functions dynamically by providing the function name as a string. -

                                                                        There is one other important difference between the locals and globals functions, which you should learn now before it bites you. It will bite you anyway, but at least then you'll remember learning it. -

                                                                        Example 8.12. locals is read-only, globals is not

                                                                        +

                                                                        Example 8.12. locals is read-only, globals is not

                                                                        
                                                                         def foo(arg):
                                                                             x = 1
                                                                        -    print locals()    1
                                                                        -    locals()["x"] = 2 2
                                                                        -    print "x=",x      3
                                                                        +    print locals()    
                                                                        +    locals()["x"] = 2 
                                                                        +    print "x=",x      
                                                                         
                                                                         z = 7
                                                                         print "z=",z
                                                                         foo(3)
                                                                        -globals()["z"] = 8    4
                                                                        -print "z=",z          5
                                                                        -
                                                                        - - - - - - - - - - - - - - - - - - - - - -
                                                                        1 -Since foo is called with 3, this will print {'arg': 3, 'x': 1}. This should not be a surprise. -
                                                                        2 -locals is a function that returns a dictionary, and here you are setting a value in that dictionary. You might think that this +globals()["z"] = 8 +print "z=",z +
                                                                        +
                                                                          +
                                                                        1. Since foo is called with 3, this will print {'arg': 3, 'x': 1}. This should not be a surprise. +
                                                                        2. locals is a function that returns a dictionary, and here you are setting a value in that dictionary. You might think that this would change the value of the local variable x to 2, but it doesn't. locals does not actually return the local namespace, it returns a copy. So changing it does nothing to the value of the variables in the local namespace. -
                                                                        3 -This prints x= 1, not x= 2. -
                                                                        4 -After being burned by locals, you might think that this wouldn't change the value of z, but it does. Due to internal differences in how Python is implemented (which I'd rather not go into, since I don't fully understand them myself), globals returns the actual global namespace, not a copy: the exact opposite behavior of locals. So any changes to the dictionary returned by globals directly affect your global variables. -
                                                                        5 -This prints z= 8, not z= 7. -
                                                                        +
                                                                      5. This prints x= 1, not x= 2. +
                                                                      6. After being burned by locals, you might think that this wouldn't change the value of z, but it does. Due to internal differences in how Python is implemented (which I'd rather not go into, since I don't fully understand them myself), globals returns the actual global namespace, not a copy: the exact opposite behavior of locals. So any changes to the dictionary returned by globals directly affect your global variables. +
                                                                      7. This prints z= 8, not z= 7.

                                                                        8.6. Dictionary-based string formatting

                                                                        Why did you learn about locals and globals? So you can learn about dictionary-based string formatting. As you recall, regular string formatting provides an easy way to insert values into strings. Values are listed in a tuple and inserted in order into the string in place of each formatting marker. While this is efficient, it is not always the easiest code to read, especially when multiple values are being inserted. You can't simply scan through the string in one pass and understand what the result will be; you're constantly switching between reading the string and reading the tuple of values.

                                                                        There is an alternative form of string formatting that uses dictionaries instead of tuples of values. -

                                                                        Example 8.13. Introducing dictionary-based string formatting

                                                                        ->>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
                                                                        ->>> "%(pwd)s" % params1
                                                                        +

                                                                        Example 8.13. Introducing dictionary-based string formatting

                                                                        +>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
                                                                        +>>> "%(pwd)s" % params
                                                                         'secret'
                                                                        ->>> "%(pwd)s is not a good password for %(uid)s" % params 2
                                                                        +>>> "%(pwd)s is not a good password for %(uid)s" % params 
                                                                         'secret is not a good password for sa'
                                                                        ->>> "%(database)s of mind, %(database)s of body" % params 3
                                                                        -'master of mind, master of body'
                                                                        - - - - - - - - - - - - - -
                                                                        1 -Instead of a tuple of explicit values, this form of string formatting uses a dictionary, params. And instead of a simple %s marker in the string, the marker contains a name in parentheses. This name is used as a key in the params dictionary and subsitutes the corresponding value, secret, in place of the %(pwd)s marker. -
                                                                        2 -Dictionary-based string formatting works with any number of named keys. Each key must exist in the given dictionary, or the +>>> "%(database)s of mind, %(database)s of body" % params +'master of mind, master of body'
                                                                        +
                                                                          +
                                                                        1. Instead of a tuple of explicit values, this form of string formatting uses a dictionary, params. And instead of a simple %s marker in the string, the marker contains a name in parentheses. This name is used as a key in the params dictionary and subsitutes the corresponding value, secret, in place of the %(pwd)s marker. +
                                                                        2. Dictionary-based string formatting works with any number of named keys. Each key must exist in the given dictionary, or the formatting will fail with a KeyError. -
                                                                        3 -You can even specify the same key twice; each occurrence will be replaced with the same value.
                                                                        +
                                                                      8. You can even specify the same key twice; each occurrence will be replaced with the same value.

                                                                        So why would you use dictionary-based string formatting? Well, it does seem like overkill to set up a dictionary of keys and values simply to do string formatting in the next line; it's really most useful when you happen to have a dictionary of meaningful keys and values already. Like locals. -

                                                                        Example 8.14. Dictionary-based string formatting in BaseHTMLProcessor.py

                                                                        +

                                                                        Example 8.14. Dictionary-based string formatting in BaseHTMLProcessor.py

                                                                        
                                                                             def handle_comment(self, text):        
                                                                        -        self.pieces.append("<!--%(text)s-->" % locals()) 1
                                                                        -
                                                                        - - - - - -
                                                                        1 -Using the built-in locals function is the most common use of dictionary-based string formatting. It means that you can use the names of local variables + self.pieces.append("<!--%(text)s-->" % locals()) +
                                                                        +
                                                                          +
                                                                        1. Using the built-in locals function is the most common use of dictionary-based string formatting. It means that you can use the names of local variables within your string (in this case, text, which was passed to the class method as an argument) and each named variable will be replaced by its value. If text is 'Begin page footer', the string formatting "<!--%(text)s-->" % locals() will resolve to the string '<!--Begin page footer-->'. -
                                                                        -

                                                                        Example 8.15. More dictionary-based string formatting

                                                                        +

                                                                        Example 8.15. More dictionary-based string formatting

                                                                        
                                                                             def unknown_starttag(self, tag, attrs):
                                                                        -        strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs]) 1
                                                                        -        self.pieces.append("<%(tag)s%(strattrs)s>" % locals())    2
                                                                        -
                                                                        - - - - - - - - - -
                                                                        1 -When this method is called, attrs is a list of key/value tuples, just like the items of a dictionary, which means you can use multi-variable assignment to iterate through it. This should be a familiar pattern by now, but there's a lot going on here, so let's break it down: -
                                                                        + strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs]) + self.pieces.append("<%(tag)s%(strattrs)s>" % locals()) +
                                                                        +
                                                                          +
                                                                        1. When this method is called, attrs is a list of key/value tuples, just like the items of a dictionary, which means you can use multi-variable assignment to iterate through it. This should be a familiar pattern by now, but there's a lot going on here, so let's break it down: +
                                                                          1. Suppose attrs is [('href', 'index.html'), ('title', 'Go to home page')]. @@ -6065,49 +3452,35 @@ meaningful keys and values already. Like .
                                                                          -
                                                                        2 -Now, using dictionary-based string formatting, you insert the value of tag and strattrs into a string. So if tag is 'a', the final result would be '<a href="index.html" title="Go to home page">', and that is what gets appended to self.pieces. -
                                                                        -
                                                                        - - - - -
                                                                        Important
                                                                        Using dictionary-based string formatting with locals is a convenient way of making complex string formatting expressions more readable, but it comes with a price. There is a +
                                                                      9. Now, using dictionary-based string formatting, you insert the value of tag and strattrs into a string. So if tag is 'a', the final result would be '<a href="index.html" title="Go to home page">', and that is what gets appended to self.pieces. + + + - -
                                                                        ImportantUsing dictionary-based string formatting with locals is a convenient way of making complex string formatting expressions more readable, but it comes with a price. There is a slight performance hit in making the call to locals, since locals builds a copy of the local namespace. -

                                                                        8.7. Quoting attribute values

                                                                        A common question on comp.lang.python is “I have a bunch of HTML documents with unquoted attribute values, and I want to properly quote them all. How can I do this?”[4] (This is generally precipitated by a project manager who has found the HTML-is-a-standard religion joining a large project and proclaiming that all pages must validate against an HTML validator. Unquoted attribute values are a common violation of the HTML standard.) Whatever the reason, unquoted attribute values are easy to fix by feeding HTML through BaseHTMLProcessor.

                                                                        BaseHTMLProcessor consumes HTML (since it's descended from SGMLParser) and produces equivalent HTML, but the HTML output is not identical to the input. Tags and attribute names will end up in lowercase, even if they started in uppercase or mixed case, and attribute values will be enclosed in double quotes, even if they started in single quotes or with no quotes at all. It is this last side effect that you can take advantage of. -

                                                                        Example 8.16. Quoting attribute values

                                                                        ->>> htmlSource = """        1
                                                                        -...    <html>
                                                                        -...    <head>
                                                                        -...    <title>Test page</title>
                                                                        -...    </head>
                                                                        -...    <body>
                                                                        -...    <ul>
                                                                        -...    <li><a href=index.html>Home</a></li>
                                                                        -...    <li><a href=toc.html>Table of contents</a></li>
                                                                        -...    <li><a href=history.html>Revision history</a></li>
                                                                        -...    </body>
                                                                        -...    </html>
                                                                        -...    """
                                                                        ->>> from BaseHTMLProcessor import BaseHTMLProcessor
                                                                        ->>> parser = BaseHTMLProcessor()
                                                                        ->>> parser.feed(htmlSource) 2
                                                                        ->>> print parser.output()   3
                                                                        -<html>
                                                                        +

                                                                        Example 8.16. Quoting attribute values

                                                                        +>>> htmlSource = """        
                                                                        +...    <html>
                                                                        +...    <head>
                                                                        +...    <title>Test page</title>
                                                                        +...    </head>
                                                                        +...    <body>
                                                                        +...    <ul>
                                                                        +...    <li><a href=index.html>Home</a></li>
                                                                        +...    <li><a href=toc.html>Table of contents</a></li>
                                                                        +...    <li><a href=history.html>Revision history</a></li>
                                                                        +...    </body>
                                                                        +...    </html>
                                                                        +...    """
                                                                        +>>> from BaseHTMLProcessor import BaseHTMLProcessor
                                                                        +>>> parser = BaseHTMLProcessor()
                                                                        +>>> parser.feed(htmlSource) 
                                                                        +>>> print parser.output()   
                                                                        +<html>
                                                                         <head>
                                                                         <title>Test page</title>
                                                                         </head>
                                                                        @@ -6117,238 +3490,109 @@ at all. It is this last side effect that you can take advantage of.
                                                                         <li><a href="toc.html">Table of contents</a></li>
                                                                         <li><a href="history.html">Revision history</a></li>
                                                                         </body>
                                                                        -</html>
                                                                        - - - - - - - - - - - - - -
                                                                        1 -Note that the attribute values of the href attributes in the <a> tags are not properly quoted. (Also note that you're using triple quotes for something other than a docstring. And directly in the IDE, no less. They're very useful.) -
                                                                        2 -Feed the parser.
                                                                        3 -Using the output function defined in BaseHTMLProcessor, you get the output as a single string, complete with quoted attribute values. While this may seem anti-climactic, think +</html>
                                                                        +
                                                                          +
                                                                        1. Note that the attribute values of the href attributes in the <a> tags are not properly quoted. (Also note that you're using triple quotes for something other than a docstring. And directly in the IDE, no less. They're very useful.) +
                                                                        2. Feed the parser. +
                                                                        3. Using the output function defined in BaseHTMLProcessor, you get the output as a single string, complete with quoted attribute values. While this may seem anti-climactic, think about how much has actually happened here: SGMLParser parsed the entire HTML document, breaking it down into tags, refs, data, and so forth; BaseHTMLProcessor used those elements to reconstruct pieces of HTML (which are still stored in parser.pieces, if you want to see them); finally, you called parser.output, which joined all the pieces of HTML into one string. -

                                                                        8.8. Introducing dialect.py

                                                                        Dialectizer is a simple (and silly) descendant of BaseHTMLProcessor. It runs blocks of text through a series of substitutions, but it makes sure that anything within a <pre>...</pre> block passes through unaltered.

                                                                        To handle the <pre> blocks, you define two methods in Dialectizer: start_pre and end_pre. -

                                                                        Example 8.17. Handling specific tags

                                                                        -    def start_pre(self, attrs):             1
                                                                        -        self.verbatim += 12
                                                                        -        self.unknown_starttag("pre", attrs) 3
                                                                        +

                                                                        Example 8.17. Handling specific tags

                                                                        
                                                                        +    def start_pre(self, attrs):             
                                                                        +        self.verbatim += 1
                                                                        +        self.unknown_starttag("pre", attrs) 
                                                                         
                                                                        -    def end_pre(self):    4
                                                                        -        self.unknown_endtag("pre")          5
                                                                        -        self.verbatim -= 16
                                                                        - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                        1 -start_pre is called every time SGMLParser finds a <pre> tag in the HTML source. (In a minute, you'll see exactly how this happens.) The method takes a single parameter, attrs, which contains the attributes of the tag (if any). attrs is a list of key/value tuples, just like unknown_starttag takes. -
                                                                        2 -In the reset method, you initialize a data attribute that serves as a counter for <pre> tags. Every time you hit a <pre> tag, you increment the counter; every time you hit a </pre> tag, you'll decrement the counter. (You could just use this as a flag and set it to 1 and reset it to 0, but it's just as easy to do it this way, and this handles the odd (but possible) case of nested <pre> tags.) In a minute, you'll see how this counter is put to good use. -
                                                                        3 -That's it, that's the only special processing you do for <pre> tags. Now you pass the list of attributes along to unknown_starttag so it can do the default processing. -
                                                                        4 -end_pre is called every time SGMLParser finds a </pre> tag. Since end tags can not contain attributes, the method takes no parameters. -
                                                                        5 -First, you want to do the default processing, just like any other end tag.
                                                                        6 -Second, you decrement your counter to signal that this <pre> block has been closed. -
                                                                        + def end_pre(self): + self.unknown_endtag("pre") + self.verbatim -= 1
                                                                        +
                                                                          +
                                                                        1. start_pre is called every time SGMLParser finds a <pre> tag in the HTML source. (In a minute, you'll see exactly how this happens.) The method takes a single parameter, attrs, which contains the attributes of the tag (if any). attrs is a list of key/value tuples, just like unknown_starttag takes. +
                                                                        2. In the reset method, you initialize a data attribute that serves as a counter for <pre> tags. Every time you hit a <pre> tag, you increment the counter; every time you hit a </pre> tag, you'll decrement the counter. (You could just use this as a flag and set it to 1 and reset it to 0, but it's just as easy to do it this way, and this handles the odd (but possible) case of nested <pre> tags.) In a minute, you'll see how this counter is put to good use. +
                                                                        3. That's it, that's the only special processing you do for <pre> tags. Now you pass the list of attributes along to unknown_starttag so it can do the default processing. +
                                                                        4. end_pre is called every time SGMLParser finds a </pre> tag. Since end tags can not contain attributes, the method takes no parameters. +
                                                                        5. First, you want to do the default processing, just like any other end tag. +
                                                                        6. Second, you decrement your counter to signal that this <pre> block has been closed.

                                                                          At this point, it's worth digging a little further into SGMLParser. I've claimed repeatedly (and you've taken it on faith so far) that SGMLParser looks for and calls specific methods for each tag, if they exist. For instance, you just saw the definition of start_pre and end_pre to handle <pre> and </pre>. But how does this happen? Well, it's not magic, it's just good Python coding. -

                                                                          Example 8.18. SGMLParser

                                                                          -    def finish_starttag(self, tag, attrs):               1
                                                                          +

                                                                          Example 8.18. SGMLParser

                                                                          
                                                                          +    def finish_starttag(self, tag, attrs):               
                                                                                   try:        
                                                                          -            method = getattr(self, 'start_' + tag)       2
                                                                          -        except AttributeError:         3
                                                                          +            method = getattr(self, 'start_' + tag)       
                                                                          +        except AttributeError:         
                                                                                       try:    
                                                                          -                method = getattr(self, 'do_' + tag)      4
                                                                          +                method = getattr(self, 'do_' + tag)      
                                                                                       except AttributeError:    
                                                                          -                self.unknown_starttag(tag, attrs)        5
                                                                          +                self.unknown_starttag(tag, attrs)        
                                                                                           return -1             
                                                                                       else:   
                                                                          -                self.handle_starttag(tag, method, attrs) 6
                                                                          +                self.handle_starttag(tag, method, attrs) 
                                                                                           return 0              
                                                                                   else:       
                                                                                       self.stack.append(tag)    
                                                                                       self.handle_starttag(tag, method, attrs)    
                                                                          -            return 1 7
                                                                          +            return 1 
                                                                           
                                                                               def handle_starttag(self, tag, method, attrs):      
                                                                          -        method(attrs)8
                                                                          - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                          1 -At this point, SGMLParser has already found a start tag and parsed the attribute list. The only thing left to do is figure out whether there is a + method(attrs)
                                                                          +
                                                                            +
                                                                          1. At this point, SGMLParser has already found a start tag and parsed the attribute list. The only thing left to do is figure out whether there is a specific handler method for this tag, or whether you should fall back on the default method (unknown_starttag). -
                                                                          2 -The “magic” of SGMLParser is nothing more than your old friend, getattr. What you may not have realized before is that getattr will find methods defined in descendants of an object as well as the object itself. Here the object is self, the current instance. So if tag is 'pre', this call to getattr will look for a start_pre method on the current instance, which is an instance of the Dialectizer class. -
                                                                          3 -getattr raises an AttributeError if the method it's looking for doesn't exist in the object (or any of its descendants), but that's okay, because you wrapped +
                                                                        7. The “magic” of SGMLParser is nothing more than your old friend, getattr. What you may not have realized before is that getattr will find methods defined in descendants of an object as well as the object itself. Here the object is self, the current instance. So if tag is 'pre', this call to getattr will look for a start_pre method on the current instance, which is an instance of the Dialectizer class. +
                                                                        8. getattr raises an AttributeError if the method it's looking for doesn't exist in the object (or any of its descendants), but that's okay, because you wrapped the call to getattr inside a try...except block and explicitly caught the AttributeError. -
                                                                        9. 4 -Since you didn't find a start_xxx method, you'll also look for a do_xxx method before giving up. This alternate naming scheme is generally used for standalone tags, like <br>, which have no corresponding end tag. But you can use either naming scheme; as you can see, SGMLParser tries both for every tag. (You shouldn't define both a start_xxx and do_xxx handler method for the same tag, though; only the start_xxx method will get called.) -
                                                                          5 -Another AttributeError, which means that the call to getattr failed with do_xxx. Since you found neither a start_xxx nor a do_xxx method for this tag, you catch the exception and fall back on the default method, unknown_starttag. -
                                                                          6 -Remember, try...except blocks can have an else clause, which is called if no exception is raised during the try...except block. Logically, that means that you did find a do_xxx method for this tag, so you're going to call it. -
                                                                          7 -By the way, don't worry about these different return values; in theory they mean something, but they're never actually used. +
                                                                        10. Since you didn't find a start_xxx method, you'll also look for a do_xxx method before giving up. This alternate naming scheme is generally used for standalone tags, like <br>, which have no corresponding end tag. But you can use either naming scheme; as you can see, SGMLParser tries both for every tag. (You shouldn't define both a start_xxx and do_xxx handler method for the same tag, though; only the start_xxx method will get called.) +
                                                                        11. Another AttributeError, which means that the call to getattr failed with do_xxx. Since you found neither a start_xxx nor a do_xxx method for this tag, you catch the exception and fall back on the default method, unknown_starttag. +
                                                                        12. Remember, try...except blocks can have an else clause, which is called if no exception is raised during the try...except block. Logically, that means that you did find a do_xxx method for this tag, so you're going to call it. +
                                                                        13. By the way, don't worry about these different return values; in theory they mean something, but they're never actually used. Don't worry about the self.stack.append(tag) either; SGMLParser keeps track internally of whether your start tags are balanced by appropriate end tags, but it doesn't do anything with this information either. In theory, you could use this module to validate that your tags were fully balanced, but it's probably not worth it, and it's beyond the scope of this chapter. You have better things to worry about right now. -
                                                                        14. 8 -start_xxx and do_xxx methods are not called directly; the tag, method, and attributes are passed to this function, handle_starttag, so that descendants can override it and change the way all start tags are dispatched. You don't need that level of control, so you just let this method do its thing, which is to call +
                                                                        15. start_xxx and do_xxx methods are not called directly; the tag, method, and attributes are passed to this function, handle_starttag, so that descendants can override it and change the way all start tags are dispatched. You don't need that level of control, so you just let this method do its thing, which is to call the method (start_xxx or do_xxx) with the list of attributes. Remember, method is a function, returned from getattr, and functions are objects. (I know you're getting tired of hearing it, and I promise I'll stop saying it as soon as I run out of ways to use it to my advantage.) Here, the function object is passed into this dispatch method as an argument, and this method turns around and calls the function. At this point, you don't need to know what the function is, what it's named, or where it's defined; the only thing you need to know about the function is that it is called with one argument, attrs. -
                                                                        16. Now back to our regularly scheduled program: Dialectizer. When you left, you were in the process of defining specific handler methods for <pre> and </pre> tags. There's only one thing left to do, and that is to process text blocks with the pre-defined substitutions. For that, you need to override the handle_data method. -

                                                                          Example 8.19. Overriding the handle_data method

                                                                          -    def handle_data(self, text):     1
                                                                          -        self.pieces.append(self.verbatim and text or self.process(text)) 2
                                                                          - - - - - - - - - -
                                                                          1 -handle_data is called with only one argument, the text to process. -
                                                                          2 -In the ancestor BaseHTMLProcessor, the handle_data method simply appended the text to the output buffer, self.pieces. Here the logic is only slightly more complicated. If you're in the middle of a <pre>...</pre> block, self.verbatim will be some value greater than 0, and you want to put the text in the output buffer unaltered. Otherwise, you will call a separate method to process the +

                                                                          Example 8.19. Overriding the handle_data method

                                                                          
                                                                          +    def handle_data(self, text):     
                                                                          +        self.pieces.append(self.verbatim and text or self.process(text)) 
                                                                          +
                                                                            +
                                                                          1. handle_data is called with only one argument, the text to process. +
                                                                          2. In the ancestor BaseHTMLProcessor, the handle_data method simply appended the text to the output buffer, self.pieces. Here the logic is only slightly more complicated. If you're in the middle of a <pre>...</pre> block, self.verbatim will be some value greater than 0, and you want to put the text in the output buffer unaltered. Otherwise, you will call a separate method to process the substitutions, then put the result of that into the output buffer. In Python, this is a one-liner, using the and-or trick. -

                                                                          You're close to completely understanding Dialectizer. The only missing link is the nature of the text substitutions themselves. If you know any Perl, you know that when complex text substitutions are required, the only real solution is regular expressions. The classes later in dialect.py define a series of regular expressions that operate on the text between the HTML tags. But you just had a whole chapter on regular expressions. You don't really want to slog through regular expressions again, do you? God knows I don't. I think you've learned enough for one chapter.

                                                                          8.9. Putting it all together

                                                                          It's time to put everything you've learned so far to good use. I hope you were paying attention. -

                                                                          Example 8.20. The translate function, part 1

                                                                          -def translate(url, dialectName="chef"): 1
                                                                          -    import urllib     2
                                                                          -    sock = urllib.urlopen(url)          3
                                                                          +

                                                                          Example 8.20. The translate function, part 1

                                                                          
                                                                          +def translate(url, dialectName="chef"): 
                                                                          +    import urllib     
                                                                          +    sock = urllib.urlopen(url)          
                                                                               htmlSource = sock.read()           
                                                                               sock.close()     
                                                                          -
                                                                          - - - - - - - - - - - - - -
                                                                          1 -The translate function has an optional argument dialectName, which is a string that specifies the dialect you'll be using. You'll see how this is used in a minute. -
                                                                          2 -Hey, wait a minute, there's an import statement in this function! That's perfectly legal in Python. You're used to seeing import statements at the top of a program, which means that the imported module is available anywhere in the program. But you can +
                                                                          +
                                                                            +
                                                                          1. The translate function has an optional argument dialectName, which is a string that specifies the dialect you'll be using. You'll see how this is used in a minute. +
                                                                          2. Hey, wait a minute, there's an import statement in this function! That's perfectly legal in Python. You're used to seeing import statements at the top of a program, which means that the imported module is available anywhere in the program. But you can also import modules within a function, which means that the imported module is only available within the function. If you have a module that is only ever used in one function, this is an easy way to make your code more modular. (When you find that your weekend hack has turned into an 800-line work of art and decide to split it up into a dozen reusable modules, you'll appreciate this.) -
                                                                          3 -Now you get the source of the given URL. -
                                                                          -

                                                                          Example 8.21. The translate function, part 2: curiouser and curiouser

                                                                          -    parserName = "%sDialectizer" % dialectName.capitalize() 1
                                                                          -    parserClass = globals()[parserName]   2
                                                                          -    parser = parserClass()                3
                                                                          -
                                                                          - - - - - - - - - - - - - -
                                                                          1 -capitalize is a string method you haven't seen before; it simply capitalizes the first letter of a string and forces everything else +
                                                                        17. Now you get the source of the given URL. +

                                                                          Example 8.21. The translate function, part 2: curiouser and curiouser

                                                                          
                                                                          +    parserName = "%sDialectizer" % dialectName.capitalize() 
                                                                          +    parserClass = globals()[parserName]   
                                                                          +    parser = parserClass()                
                                                                          +
                                                                          +
                                                                            +
                                                                          1. capitalize is a string method you haven't seen before; it simply capitalizes the first letter of a string and forces everything else to lowercase. Combined with some string formatting, you've taken the name of a dialect and transformed it into the name of the corresponding Dialectizer class. If dialectName is the string 'chef', parserName will be the string 'ChefDialectizer'. -
                                                                        18. 2 -You have the name of a class as a string (parserName), and you have the global namespace as a dictionary (globals()). Combined, you can get a reference to the class which the string names. (Remember, classes are objects, and they can be assigned to variables just like any other object.) If parserName is the string 'ChefDialectizer', parserClass will be the class ChefDialectizer. -
                                                                          3 -Finally, you have a class object (parserClass), and you want an instance of the class. Well, you already know how to do that: call the class like a function. The fact that the class is being stored in a local variable makes absolutely no difference; you just call the local variable +
                                                                        19. You have the name of a class as a string (parserName), and you have the global namespace as a dictionary (globals()). Combined, you can get a reference to the class which the string names. (Remember, classes are objects, and they can be assigned to variables just like any other object.) If parserName is the string 'ChefDialectizer', parserClass will be the class ChefDialectizer. +
                                                                        20. Finally, you have a class object (parserClass), and you want an instance of the class. Well, you already know how to do that: call the class like a function. The fact that the class is being stored in a local variable makes absolutely no difference; you just call the local variable like a function, and out pops an instance of the class. If parserClass is the class ChefDialectizer, parser will be an instance of the class ChefDialectizer. -
                                                                        21. Why bother? After all, there are only 3 Dialectizer classes; why not just use a case statement? (Well, there's no case statement in Python, but why not just use a series of if statements?) One reason: extensibility. The translate function has absolutely no idea how many Dialectizer classes you've defined. Imagine if you defined a new FooDialectizer tomorrow; translate would work by passing 'foo' as the dialectName.

                                                                          Even better, imagine putting FooDialectizer in a separate module, and importing it with from module import. You've already seen that this includes it in globals(), so translate would still work without modification, even though FooDialectizer was in a separate file.

                                                                          Now imagine that the name of the dialect is coming from somewhere outside the program, maybe from a database or from a user-inputted @@ -6356,35 +3600,19 @@ value on a form. You can use any number of server-side Python scripting architec

                                                                          Finally, imagine a Dialectizer framework with a plug-in architecture. You could put each Dialectizer class in a separate file, leaving only the translate function in dialect.py. Assuming a consistent naming scheme, the translate function could dynamic import the appropiate class from the appropriate file, given nothing but the dialect name. (You haven't seen dynamic importing yet, but I promise to cover it in a later chapter.) To add a new dialect, you would simply add an appropriately-named file in the plug-ins directory (like foodialect.py which contains the FooDialectizer class). Calling the translate function with the dialect name 'foo' would find the module foodialect.py, import the class FooDialectizer, and away you go. -

                                                                          Example 8.22. The translate function, part 3

                                                                          -    parser.feed(htmlSource) 1
                                                                          -    parser.close()          2
                                                                          -    return parser.output()  3
                                                                          -
                                                                          - - - - - - - - - - - - - -
                                                                          1 -After all that imagining, this is going to seem pretty boring, but the feed function is what does the entire transformation. You had the entire HTML source in a single string, so you only had to call feed once. However, you can call feed as often as you want, and the parser will just keep parsing. So if you were worried about memory usage (or you knew you +

                                                                          Example 8.22. The translate function, part 3

                                                                          
                                                                          +    parser.feed(htmlSource) 
                                                                          +    parser.close()          
                                                                          +    return parser.output()  
                                                                          +
                                                                          +
                                                                            +
                                                                          1. After all that imagining, this is going to seem pretty boring, but the feed function is what does the entire transformation. You had the entire HTML source in a single string, so you only had to call feed once. However, you can call feed as often as you want, and the parser will just keep parsing. So if you were worried about memory usage (or you knew you were going to be dealing with very large HTML pages), you could set this up in a loop, where you read a few bytes of HTML and fed it to the parser. The result would be the same. -
                                                                          2 -Because feed maintains an internal buffer, you should always call the parser's close method when you're done (even if you fed it all at once, like you did). Otherwise you may find that your output is missing +
                                                                        22. Because feed maintains an internal buffer, you should always call the parser's close method when you're done (even if you fed it all at once, like you did). Otherwise you may find that your output is missing the last few bytes. -
                                                                        23. 3 -Remember, output is the function you defined on BaseHTMLProcessor that joins all the pieces of output you've buffered and returns them in a single string. -
                                                                          +
                                                                        24. Remember, output is the function you defined on BaseHTMLProcessor that joins all the pieces of output you've buffered and returns them in a single string.

                                                                          And just like that, you've “translated” a web page, given nothing but a URL and the name of a dialect. -

                                                                          +

                                                                          Further reading

                                                                          • You thought I was kidding about the server-side scripting idea. So did I, until I found this web-based dialectizer. Unfortunately, source code does not appear to be available. @@ -6392,7 +3620,7 @@ appropriately-named file in the plug-ins directory (like foodialect.py

                                                                            8.10. Summary

                                                                            Python provides you with a powerful tool, sgmllib.py, to manipulate HTML by turning its structure into an object model. You can use this tool in many different ways. -

                                                                            +
                                                                            • parsing the HTML looking for something specific @@ -6401,29 +3629,29 @@ appropriately-named file in the plug-ins directory (like foodialect.pytransforming the HTML into something else by manipulating the text while leaving the tags alone, like the Dialectizer

                                                                            Along with these examples, you should be comfortable doing all of the following things: -

                                                                            +
                                                                            -


                                                                            -
                                                                            +


                                                                            +

                                                                            [1] The technical term for a parser like SGMLParser is a consumer: it consumes HTML and breaks it down. Presumably, the name feed was chosen to fit into the whole “consumer” motif. Personally, it makes me think of an exhibit in the zoo where there's just a dark cage with no trees or plants or evidence of life of any kind, but if you stand perfectly still and look really closely you can make out two beady eyes staring back at you from the far left corner, but you convince yourself that that's just your mind playing tricks on you, and the only way you can tell that the whole thing isn't just an empty cage is a small innocuous sign on the railing that reads, “Do not feed the parser.” But maybe that's just me. In any event, it's an interesting mental image. -

                                                                            +

                                                                            [2] The reason Python is better at lists than strings is that lists are mutable but strings are immutable. This means that appending to a list just adds the element and updates the index. Since strings can not be changed after they are created, code like s = s + newpiece will create an entirely new string out of the concatenation of the original and the new piece, then throw away the original string. This involves a lot of expensive memory management, and the amount of effort involved increases as the string gets longer, so doing s = s + newpiece in a loop is deadly. In technical terms, appending n items to a list is O(n), while appending n items to a string is O(n2). -

                                                                            +

                                                                            [3] I don't get out much. -

                                                                            +

                                                                            [4] All right, it's not that common a question. It's not up there with “What editor should I use to write Python code?” (answer: Emacs) or “Is Python better or worse than Perl?” (answer: “Perl is worse than Python because people wanted it worse.” -Larry Wall, 10/14/1998) But questions about HTML processing pop up in one form or another about once a month, and among those questions, this is a popular one. -

                                                                            +

                                                                            Chapter 9. XML Processing

                                                                            9.1. Diving in

                                                                            These next two chapters are about XML processing in Python. It would be helpful if you already knew what an XML document looks like, that it's made up of structured tags to form a hierarchy of elements, and so on. If this doesn't make @@ -6435,8 +3663,9 @@ science.

                                                                            There are two basic ways to work with XML. One is called SAX (“Simple API for XML”), and it works by reading the XML a little bit at a time and calling a method for each element it finds. (If you read Chapter 8, HTML Processing, this should sound familiar, because that's how the sgmllib module works.) The other is called DOM (“Document Object Model”), and it works by reading in the entire XML document at once and creating an internal representation of it using native Python classes linked in a tree structure. Python has standard modules for both kinds of parsing, but this chapter will only deal with using the DOM.

                                                                            The following is a complete Python program which generates pseudo-random output based on a context-free grammar defined in an XML format. Don't worry yet if you don't understand what that means; you'll examine both the program's input and its output in more depth throughout these next two chapters. -

                                                                            Example 9.1. kgp.py

                                                                            -

                                                                            If you have not already done so, you can download this and other examples used in this book.

                                                                            +

                                                                            Example 9.1. kgp.py

                                                                            +

                                                                            If you have not already done so, you can download this and other examples used in this book. +

                                                                            
                                                                             """Kant Generator for Python
                                                                             
                                                                             Generates mock philosophy based on a context-free grammar
                                                                            @@ -6677,7 +3906,7 @@ def main(argv):
                                                                             
                                                                             if __name__ == "__main__":
                                                                                 main(sys.argv[1:])
                                                                            -

                                                                            Example 9.2. toolbox.py

                                                                            +

                                                                            Example 9.2. toolbox.py

                                                                            
                                                                             """Miscellaneous utility functions"""
                                                                             
                                                                             def openAnything(source):            
                                                                            @@ -6725,8 +3954,8 @@ def openAnything(source):
                                                                                 import StringIO     
                                                                                 return StringIO.StringIO(str(source)) 
                                                                             

                                                                            Run the program kgp.py by itself, and it will parse the default XML-based grammar, in kant.xml, and print several paragraphs worth of philosophy in the style of Immanuel Kant. -

                                                                            Example 9.3. Sample output of kgp.py

                                                                            [you@localhost kgp]$ python kgp.py
                                                                            -     As is shown in the writings of Hume, our a priori concepts, in
                                                                            +

                                                                            Example 9.3. Sample output of kgp.py

                                                                            [you@localhost kgp]$ python kgp.py
                                                                            +     As is shown in the writings of Hume, our a priori concepts, in
                                                                             reference to ends, abstract from all content of knowledge; in the study
                                                                             of space, the discipline of human reason, in accordance with the
                                                                             principles of philosophy, is the clue to the discovery of the
                                                                            @@ -6766,29 +3995,23 @@ But all of it is in the style of Immanuel Kant.
                                                                             

                                                                            The interesting thing about this program is that there is nothing Kant-specific about it. All the content in the previous example was derived from the grammar file, kant.xml. If you tell the program to use a different grammar file (which you can specify on the command line), the output will be completely different. -

                                                                            Example 9.4. Simpler output from kgp.py

                                                                            [you@localhost kgp]$ python kgp.py -g binary.xml
                                                                            +

                                                                            Example 9.4. Simpler output from kgp.py

                                                                            [you@localhost kgp]$ python kgp.py -g binary.xml
                                                                             00101001
                                                                            -[you@localhost kgp]$ python kgp.py -g binary.xml
                                                                            +[you@localhost kgp]$ python kgp.py -g binary.xml
                                                                             10110100

                                                                            You will take a closer look at the structure of the grammar file later in this chapter. For now, all you need to know is that the grammar file defines the structure of the output, and the kgp.py program reads through the grammar and makes random decisions about which words to plug in where.

                                                                            9.2. Packages

                                                                            Actually parsing an XML document is very simple: one line of code. However, before you get to that line of code, you need to take a short detour to talk about packages. -

                                                                            Example 9.5. Loading an XML document (a sneak peek)

                                                                            ->>> from xml.dom import minidom 1
                                                                            ->>> xmldoc = minidom.parse('~/diveintopython3/common/py/kgp/binary.xml')
                                                                            - - - - - -
                                                                            1 -This is a syntax you haven't seen before. It looks almost like the from module import you know and love, but the "." gives it away as something above and beyond a simple import. In fact, xml is what is known as a package, dom is a nested package within xml, and minidom is a module within xml.dom. -
                                                                            +

                                                                            Example 9.5. Loading an XML document (a sneak peek)

                                                                            +>>> from xml.dom import minidom 
                                                                            +>>> xmldoc = minidom.parse('~/diveintopython3/common/py/kgp/binary.xml')
                                                                            +
                                                                              +
                                                                            1. This is a syntax you haven't seen before. It looks almost like the from module import you know and love, but the "." gives it away as something above and beyond a simple import. In fact, xml is what is known as a package, dom is a nested package within xml, and minidom is a module within xml.dom.

                                                                              That sounds complicated, but it's really not. Looking at the actual implementation may help. Packages are little more than directories of modules; nested packages are subdirectories. The modules within a package (or a nested package) are still just .py files, like always, except that they're in a subdirectory instead of the main lib/ directory of your Python installation. -

                                                                              Example 9.6. File layout of a package

                                                                              Python21/           root Python installation (home of the executable)
                                                                              +

                                                                              Example 9.6. File layout of a package

                                                                              Python21/           root Python installation (home of the executable)
                                                                               |
                                                                               +--lib/             library directory (home of the standard library modules)
                                                                                  |
                                                                              @@ -6801,73 +4024,47 @@ just .py files, like always, except that they're in a subdirectory
                                                                                      +--parsers/  xml.parsers package (used internally)

                                                                              So when you say from xml.dom import minidom, Python figures out that that means “look in the xml directory for a dom directory, and look in that for the minidom module, and import it as minidom”. But Python is even smarter than that; not only can you import entire modules contained within a package, you can selectively import specific classes or functions from a module contained within a package. You can also import the package itself as a module. The syntax is all the same; Python figures out what you mean based on the file layout of the package, and automatically does the right thing. -

                                                                              Example 9.7. Packages are modules, too

                                                                              >>> from xml.dom import minidom         1
                                                                              ->>> minidom
                                                                              +

                                                                              Example 9.7. Packages are modules, too

                                                                              >>> from xml.dom import minidom         
                                                                              +>>> minidom
                                                                               <module 'xml.dom.minidom' from 'C:\Python21\lib\xml\dom\minidom.pyc'>
                                                                              ->>> minidom.Element
                                                                              +>>> minidom.Element
                                                                               <class xml.dom.minidom.Element at 01095744>
                                                                              ->>> from xml.dom.minidom import Element 2
                                                                              ->>> Element
                                                                              +>>> from xml.dom.minidom import Element 
                                                                              +>>> Element
                                                                               <class xml.dom.minidom.Element at 01095744>
                                                                              ->>> minidom.Element
                                                                              +>>> minidom.Element
                                                                               <class xml.dom.minidom.Element at 01095744>
                                                                              ->>> from xml import dom                 3
                                                                              ->>> dom
                                                                              +>>> from xml import dom                 
                                                                              +>>> dom
                                                                               <module 'xml.dom' from 'C:\Python21\lib\xml\dom\__init__.pyc'>
                                                                              ->>> import xml        4
                                                                              ->>> xml
                                                                              -<module 'xml' from 'C:\Python21\lib\xml\__init__.pyc'>
                                                                              - - - - - - - - - - - - - - - - - -
                                                                              1 -Here you're importing a module (minidom) from a nested package (xml.dom). The result is that minidom is imported into your namespace, and in order to reference classes within the minidom module (like Element), you need to preface them with the module name. -
                                                                              2 -Here you are importing a class (Element) from a module (minidom) from a nested package (xml.dom). The result is that Element is imported directly into your namespace. Note that this does not interfere with the previous import; the Element class can now be referenced in two ways (but it's all still the same class). -
                                                                              3 -Here you are importing the dom package (a nested package of xml) as a module in and of itself. Any level of a package can be treated as a module, as you'll see in a moment. It can even +>>> import xml +>>> xml +<module 'xml' from 'C:\Python21\lib\xml\__init__.pyc'>
                                                                              +
                                                                                +
                                                                              1. Here you're importing a module (minidom) from a nested package (xml.dom). The result is that minidom is imported into your namespace, and in order to reference classes within the minidom module (like Element), you need to preface them with the module name. +
                                                                              2. Here you are importing a class (Element) from a module (minidom) from a nested package (xml.dom). The result is that Element is imported directly into your namespace. Note that this does not interfere with the previous import; the Element class can now be referenced in two ways (but it's all still the same class). +
                                                                              3. Here you are importing the dom package (a nested package of xml) as a module in and of itself. Any level of a package can be treated as a module, as you'll see in a moment. It can even have its own attributes and methods, just the modules you've seen before. -
                                                                              4 -Here you are importing the root level xml package as a module. -
                                                                              +
                                                                            2. Here you are importing the root level xml package as a module.

                                                                              So how can a package (which is just a directory on disk) be imported and treated as a module (which is always a file on disk)? -The answer is the magical __init__.py file. You see, packages are not simply directories; they are directories with a specific file, __init__.py, inside. This file defines the attributes and methods of the package. For instance, xml.dom contains a Node class, which is defined in xml/dom/__init__.py. When you import a package as a module (like dom from xml), you're really importing its __init__.py file. - - - - -
                                                                              Note
                                                                              A package is a directory with the special __init__.py file in it. The __init__.py file defines the attributes and methods of the package. It doesn't need to define anything; it can just be an empty file, +The answer is the magical __init__.py file. You see, packages are not simply directories; they are directories with a specific file, __init__.py, inside. This file defines the attributes and methods of the package. For instance, xml.dom contains a Node class, which is defined in xml/dom/__init__.py. When you import a package as a module (like dom from xml), you're really importing its __init__.py file. + + + - -
                                                                              NoteA package is a directory with the special __init__.py file in it. The __init__.py file defines the attributes and methods of the package. It doesn't need to define anything; it can just be an empty file, but it has to exist. But if __init__.py doesn't exist, the directory is just a directory, not a package, and it can't be imported or contain modules or nested packages. -

                                                                              So why bother with packages? Well, they provide a way to logically group related modules. Instead of having an xml package with sax and dom packages inside, the authors could have chosen to put all the sax functionality in xmlsax.py and all the dom functionality in xmldom.py, or even put all of it in a single module. But that would have been unwieldy (as of this writing, the XML package has over 3000 lines of code) and difficult to manage (separate source files mean multiple people can work on different areas simultaneously).

                                                                              If you ever find yourself writing a large subsystem in Python (or, more likely, when you realize that your small subsystem has grown into a large one), invest some time designing a good package architecture. It's one of the many things Python is good at, so take advantage of it.

                                                                              9.3. Parsing XML

                                                                              As I was saying, actually parsing an XML document is very simple: one line of code. Where you go from there is up to you. -

                                                                              Example 9.8. Loading an XML document (for real this time)

                                                                              ->>> from xml.dom import minidom      1
                                                                              ->>> xmldoc = minidom.parse('~/diveintopython3/common/py/kgp/binary.xml')  2
                                                                              ->>> xmldoc         3
                                                                              +

                                                                              Example 9.8. Loading an XML document (for real this time)

                                                                              +>>> from xml.dom import minidom      
                                                                              +>>> xmldoc = minidom.parse('~/diveintopython3/common/py/kgp/binary.xml')  
                                                                              +>>> xmldoc         
                                                                               <xml.dom.minidom.Document instance at 010BE87C>
                                                                              ->>> print xmldoc.toxml()             4
                                                                              -<?xml version="1.0" ?>
                                                                              +>>> print xmldoc.toxml()             
                                                                              +<?xml version="1.0" ?>
                                                                               <grammar>
                                                                               <ref id="bit">
                                                                                 <p>0</p>
                                                                              @@ -6877,67 +4074,30 @@ package architecture. It's one of the many things Python is good at, so take adv
                                                                                 <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
                                                                               <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
                                                                               </ref>
                                                                              -</grammar>
                                                                              - - - - - - - - - - - - - - - - - -
                                                                              1 -As you saw in the previous section, this imports the minidom module from the xml.dom package. -
                                                                              2 -Here is the one line of code that does all the work: minidom.parse takes one argument and returns a parsed representation of the XML document. The argument can be many things; in this case, it's simply a filename of an XML document on my local disk. (To follow along, you'll need to change the path to point to your downloaded examples directory.) +</grammar>
                                                                              +
                                                                                +
                                                                              1. As you saw in the previous section, this imports the minidom module from the xml.dom package. +
                                                                              2. Here is the one line of code that does all the work: minidom.parse takes one argument and returns a parsed representation of the XML document. The argument can be many things; in this case, it's simply a filename of an XML document on my local disk. (To follow along, you'll need to change the path to point to your downloaded examples directory.) But you can also pass a file object, or even a file-like object. You'll take advantage of this flexibility later in this chapter. -
                                                                              3 -The object returned from minidom.parse is a Document object, a descendant of the Node class. This Document object is the root level of a complex tree-like structure of interlocking Python objects that completely represent the XML document you passed to minidom.parse. -
                                                                              4 -toxml is a method of the Node class (and is therefore available on the Document object you got from minidom.parse). toxml prints out the XML that this Node represents. For the Document node, this prints out the entire XML document. -
                                                                              +
                                                                            3. The object returned from minidom.parse is a Document object, a descendant of the Node class. This Document object is the root level of a complex tree-like structure of interlocking Python objects that completely represent the XML document you passed to minidom.parse. +
                                                                            4. toxml is a method of the Node class (and is therefore available on the Document object you got from minidom.parse). toxml prints out the XML that this Node represents. For the Document node, this prints out the entire XML document.

                                                                              Now that you have an XML document in memory, you can start traversing through it. -

                                                                              Example 9.9. Getting child nodes

                                                                              ->>> xmldoc.childNodes    1
                                                                              +

                                                                              Example 9.9. Getting child nodes

                                                                              +>>> xmldoc.childNodes    
                                                                               [<DOM Element: grammar at 17538908>]
                                                                              ->>> xmldoc.childNodes[0] 2
                                                                              +>>> xmldoc.childNodes[0] 
                                                                               <DOM Element: grammar at 17538908>
                                                                              ->>> xmldoc.firstChild    3
                                                                              -<DOM Element: grammar at 17538908>
                                                                              - - - - - - - - - - - - - -
                                                                              1 -Every Node has a childNodes attribute, which is a list of the Node objects. A Document always has only one child node, the root element of the XML document (in this case, the grammar element). -
                                                                              2 -To get the first (and in this case, the only) child node, just use regular list syntax. Remember, there is nothing special +>>> xmldoc.firstChild +<DOM Element: grammar at 17538908>
                                                                              +
                                                                                +
                                                                              1. Every Node has a childNodes attribute, which is a list of the Node objects. A Document always has only one child node, the root element of the XML document (in this case, the grammar element). +
                                                                              2. To get the first (and in this case, the only) child node, just use regular list syntax. Remember, there is nothing special going on here; this is just a regular Python list of regular Python objects. -
                                                                              3 -Since getting the first child node of a node is a useful and common activity, the Node class has a firstChild attribute, which is synonymous with childNodes[0]. (There is also a lastChild attribute, which is synonymous with childNodes[-1].) -
                                                                              -

                                                                              Example 9.10. toxml works on any node

                                                                              ->>> grammarNode = xmldoc.firstChild
                                                                              ->>> print grammarNode.toxml() 1
                                                                              -<grammar>
                                                                              +
                                                                            5. Since getting the first child node of a node is a useful and common activity, the Node class has a firstChild attribute, which is synonymous with childNodes[0]. (There is also a lastChild attribute, which is synonymous with childNodes[-1].) +

                                                                              Example 9.10. toxml works on any node

                                                                              +>>> grammarNode = xmldoc.firstChild
                                                                              +>>> print grammarNode.toxml() 
                                                                              +<grammar>
                                                                               <ref id="bit">
                                                                                 <p>0</p>
                                                                                 <p>1</p>
                                                                              @@ -6946,120 +4106,62 @@ package architecture. It's one of the many things Python is good at, so take adv
                                                                                 <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
                                                                               <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
                                                                               </ref>
                                                                              -</grammar>
                                                                              - - - - - -
                                                                              1 -Since the toxml method is defined in the Node class, it is available on any XML node, not just the Document element. -
                                                                              -

                                                                              Example 9.11. Child nodes can be text

                                                                              ->>> grammarNode.childNodes1
                                                                              -[<DOM Text node "\n">, <DOM Element: ref at 17533332>, \
                                                                              +</grammar>
                                                                              +
                                                                                +
                                                                              1. Since the toxml method is defined in the Node class, it is available on any XML node, not just the Document element. +

                                                                                Example 9.11. Child nodes can be text

                                                                                +>>> grammarNode.childNodes
                                                                                +[<DOM Text node "\n">, <DOM Element: ref at 17533332>, \
                                                                                 <DOM Text node "\n">, <DOM Element: ref at 17549660>, <DOM Text node "\n">]
                                                                                ->>> print grammarNode.firstChild.toxml()    2
                                                                                -
                                                                                +>>> print grammarNode.firstChild.toxml()    
                                                                                +
                                                                                 
                                                                                 
                                                                                ->>> print grammarNode.childNodes[1].toxml() 3
                                                                                -<ref id="bit">
                                                                                +>>> print grammarNode.childNodes[1].toxml() 
                                                                                +<ref id="bit">
                                                                                   <p>0</p>
                                                                                   <p>1</p>
                                                                                 </ref>
                                                                                ->>> print grammarNode.childNodes[3].toxml() 4
                                                                                -<ref id="byte">
                                                                                +>>> print grammarNode.childNodes[3].toxml() 
                                                                                +<ref id="byte">
                                                                                   <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
                                                                                 <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
                                                                                 </ref>
                                                                                ->>> print grammarNode.lastChild.toxml()     5
                                                                                -
                                                                                +>>> print grammarNode.lastChild.toxml()     
                                                                                +
                                                                                 
                                                                                -
                                                                                - - - - - - - - - - - - - - - - - - - - - -
                                                                                1 -Looking at the XML in binary.xml, you might think that the grammar has only two child nodes, the two ref elements. But you're missing something: the carriage returns! After the '<grammar>' and before the first '<ref>' is a carriage return, and this text counts as a child node of the grammar element. Similarly, there is a carriage return after each '</ref>'; these also count as child nodes. So grammar.childNodes is actually a list of 5 objects: 3 Text objects and 2 Element objects. -
                                                                                2 -The first child is a Text object representing the carriage return after the '<grammar>' tag and before the first '<ref>' tag. -
                                                                                3 -The second child is an Element object representing the first ref element. -
                                                                                4 -The fourth child is an Element object representing the second ref element. -
                                                                                5 -The last child is a Text object representing the carriage return after the '</ref>' end tag and before the '</grammar>' end tag. -
                                                                                -

                                                                                Example 9.12. Drilling down all the way to text

                                                                                ->>> grammarNode
                                                                                +
                                                                                +
                                                                                  +
                                                                                1. Looking at the XML in binary.xml, you might think that the grammar has only two child nodes, the two ref elements. But you're missing something: the carriage returns! After the '<grammar>' and before the first '<ref>' is a carriage return, and this text counts as a child node of the grammar element. Similarly, there is a carriage return after each '</ref>'; these also count as child nodes. So grammar.childNodes is actually a list of 5 objects: 3 Text objects and 2 Element objects. +
                                                                                2. The first child is a Text object representing the carriage return after the '<grammar>' tag and before the first '<ref>' tag. +
                                                                                3. The second child is an Element object representing the first ref element. +
                                                                                4. The fourth child is an Element object representing the second ref element. +
                                                                                5. The last child is a Text object representing the carriage return after the '</ref>' end tag and before the '</grammar>' end tag. +

                                                                                  Example 9.12. Drilling down all the way to text

                                                                                  +>>> grammarNode
                                                                                   <DOM Element: grammar at 19167148>
                                                                                  ->>> refNode = grammarNode.childNodes[1] 1
                                                                                  ->>> refNode
                                                                                  +>>> refNode = grammarNode.childNodes[1] 
                                                                                  +>>> refNode
                                                                                   <DOM Element: ref at 17987740>
                                                                                  ->>> refNode.childNodes2
                                                                                  -[<DOM Text node "\n">, <DOM Text node "  ">, <DOM Element: p at 19315844>, \
                                                                                  +>>> refNode.childNodes
                                                                                  +[<DOM Text node "\n">, <DOM Text node "  ">, <DOM Element: p at 19315844>, \
                                                                                   <DOM Text node "\n">, <DOM Text node "  ">, \
                                                                                   <DOM Element: p at 19462036>, <DOM Text node "\n">]
                                                                                  ->>> pNode = refNode.childNodes[2]
                                                                                  ->>> pNode
                                                                                  +>>> pNode = refNode.childNodes[2]
                                                                                  +>>> pNode
                                                                                   <DOM Element: p at 19315844>
                                                                                  ->>> print pNode.toxml()                 3
                                                                                  +>>> print pNode.toxml()                 
                                                                                   <p>0</p>
                                                                                  ->>> pNode.firstChild  4
                                                                                  +>>> pNode.firstChild  
                                                                                   <DOM Text node "0">
                                                                                  ->>> pNode.firstChild.data               5
                                                                                  -u'0'
                                                                                  - - - - - - - - - - - - - - - - - - - - - -
                                                                                  1 -As you saw in the previous example, the first ref element is grammarNode.childNodes[1], since childNodes[0] is a Text node for the carriage return. -
                                                                                  2 -The ref element has its own set of child nodes, one for the carriage return, a separate one for the spaces, one for the p element, and so forth. -
                                                                                  3 -You can even use the toxml method here, deeply nested within the document. -
                                                                                  4 -The p element has only one child node (you can't tell that from this example, but look at pNode.childNodes if you don't believe me), and it is a Text node for the single character '0'. -
                                                                                  5 -The .data attribute of a Text node gives you the actual string that the text node represents. But what is that 'u' in front of the string? The answer to that deserves its own section. -
                                                                                  +>>> pNode.firstChild.data +u'0'
                                                                            6. +
                                                                                +
                                                                              1. As you saw in the previous example, the first ref element is grammarNode.childNodes[1], since childNodes[0] is a Text node for the carriage return. +
                                                                              2. The ref element has its own set of child nodes, one for the carriage return, a separate one for the spaces, one for the p element, and so forth. +
                                                                              3. You can even use the toxml method here, deeply nested within the document. +
                                                                              4. The p element has only one child node (you can't tell that from this example, but look at pNode.childNodes if you don't believe me), and it is a Text node for the single character '0'. +
                                                                              5. The .data attribute of a Text node gives you the actual string that the text node represents. But what is that 'u' in front of the string? The answer to that deserves its own section.

                                                                                9.4. Unicode

                                                                                Unicode is a system to represent characters from all the world's different languages. When Python parses an XML document, all data is stored in memory as unicode.

                                                                                You'll get to all that in a minute, but first, some background. @@ -7073,7 +4175,8 @@ the character encoding alongside each piece of text, and make sure to pass it ar Then think about multilingual documents, with characters from multiple languages in the same document. (They typically used escape codes to switch modes; poof, you're in Russian koi8-r mode, so character 241 means this; poof, now you're in Mac Greek mode, so character 241 means something else. And so on.) These are the problems which unicode was designed to solve. -

                                                                                To solve these problems, unicode represents each character as a 2-byte number, from 0 to 65535.[5] Each 2-byte number represents a unique character used in at least one of the world's languages. (Characters that are used +

                                                                                To solve these problems, unicode represents each character as a 2-byte number, from 0 to 65535. +[5] Each 2-byte number represents a unique character used in at least one of the world's languages. (Characters that are used in multiple languages have the same numeric code.) There is exactly 1 number per character, and exactly 1 character per number. Unicode data is never ambiguous.

                                                                                Of course, there is still the matter of all these legacy encoding systems. 7-bit ASCII, for instance, which stores English characters as numbers ranging from 0 to 127. (65 is capital “A”, 97 is lowercase “a”, and so forth.) English has a very simple alphabet, so it can be completely expressed in 7-bit ASCII. Western European languages like French, Spanish, and German all use an encoding system called ISO-8859-1 (also called “latin-1”), which uses the 7-bit ASCII characters for the numbers 0 through 127, but then extends into the 128-255 range for characters like n-with-a-tilde-over-it @@ -7084,180 +4187,94 @@ systems. For instance, to integrate with some other computer system which expect scheme, or to print it to a non-unicode-aware terminal or printer. Or to store it in an XML document which explicitly specifies the encoding scheme.

                                                                                And on that note, let's get back to Python.

                                                                                Python has had unicode support throughout the language since version 2.0. The XML package uses unicode to store all parsed XML data, but you can use unicode anywhere. -

                                                                                Example 9.13. Introducing unicode

                                                                                ->>> s = u'Dive in'            1
                                                                                ->>> s
                                                                                +

                                                                                Example 9.13. Introducing unicode

                                                                                +>>> s = u'Dive in'            
                                                                                +>>> s
                                                                                 u'Dive in'
                                                                                ->>> print s 2
                                                                                -Dive in
                                                                                - - - - - - - - - -
                                                                                1 -To create a unicode string instead of a regular ASCII string, add the letter “u” before the string. Note that this particular string doesn't have any non-ASCII characters. That's fine; unicode is a superset of ASCII (a very large superset at that), so any regular ASCII string can also be stored as unicode. -
                                                                                2 -When printing a string, Python will attempt to convert it to your default encoding, which is usually ASCII. (More on this in a minute.) Since this unicode string is made up of characters that are also ASCII characters, printing it has the same result as printing a normal ASCII string; the conversion is seamless, and if you didn't know that s was a unicode string, you'd never notice the difference. -
                                                                                -

                                                                                Example 9.14. Storing non-ASCII characters

                                                                                ->>> s = u'La Pe\xf1a'         1
                                                                                ->>> print s 2
                                                                                -Traceback (innermost last):
                                                                                +>>> print s 
                                                                                +Dive in
                                                                                +
                                                                                  +
                                                                                1. To create a unicode string instead of a regular ASCII string, add the letter “u” before the string. Note that this particular string doesn't have any non-ASCII characters. That's fine; unicode is a superset of ASCII (a very large superset at that), so any regular ASCII string can also be stored as unicode. +
                                                                                2. When printing a string, Python will attempt to convert it to your default encoding, which is usually ASCII. (More on this in a minute.) Since this unicode string is made up of characters that are also ASCII characters, printing it has the same result as printing a normal ASCII string; the conversion is seamless, and if you didn't know that s was a unicode string, you'd never notice the difference. +

                                                                                  Example 9.14. Storing non-ASCII characters

                                                                                  +>>> s = u'La Pe\xf1a'         
                                                                                  +>>> print s 
                                                                                  +Traceback (innermost last):
                                                                                     File "<interactive input>", line 1, in ?
                                                                                   UnicodeError: ASCII encoding error: ordinal not in range(128)
                                                                                  ->>> print s.encode('latin-1') 3
                                                                                  -La Peña
                                                                                  - - - - - - - - - - - - - -
                                                                                  1 -The real advantage of unicode, of course, is its ability to store non-ASCII characters, like the Spanish “ñ” (n with a tilde over it). The unicode character code for the tilde-n is 0xf1 in hexadecimal (241 in decimal), which you can type like this: \xf1. -
                                                                                  2 -Remember I said that the print function attempts to convert a unicode string to ASCII so it can print it? Well, that's not going to work here, because your unicode string contains non-ASCII characters, so Python raises a UnicodeError error. -
                                                                                  3 -Here's where the conversion-from-unicode-to-other-encoding-schemes comes in. s is a unicode string, but print can only print a regular string. To solve this problem, you call the encode method, available on every unicode string, to convert the unicode string to a regular string in the given encoding scheme, +>>> print s.encode('latin-1') +La Peña
                                                                                  +
                                                                                    +
                                                                                  1. The real advantage of unicode, of course, is its ability to store non-ASCII characters, like the Spanish “ñ” (n with a tilde over it). The unicode character code for the tilde-n is 0xf1 in hexadecimal (241 in decimal), which you can type like this: \xf1. +
                                                                                  2. Remember I said that the print function attempts to convert a unicode string to ASCII so it can print it? Well, that's not going to work here, because your unicode string contains non-ASCII characters, so Python raises a UnicodeError error. +
                                                                                  3. Here's where the conversion-from-unicode-to-other-encoding-schemes comes in. s is a unicode string, but print can only print a regular string. To solve this problem, you call the encode method, available on every unicode string, to convert the unicode string to a regular string in the given encoding scheme, which you pass as a parameter. In this case, you're using latin-1 (also known as iso-8859-1), which includes the tilde-n (whereas the default ASCII encoding scheme did not, since it only includes characters numbered 0 through 127). -

                                                                                  Remember I said Python usually converted unicode to ASCII whenever it needed to make a regular string out of a unicode string? Well, this default encoding scheme is an option which you can customize. -

                                                                                  Example 9.15. sitecustomize.py

                                                                                  -# sitecustomize.py 1
                                                                                  +

                                                                                  Example 9.15. sitecustomize.py

                                                                                  
                                                                                  +# sitecustomize.py 
                                                                                   # this file can be anywhere in your Python path,
                                                                                   # but it usually goes in ${pythondir}/lib/site-packages/
                                                                                   import sys
                                                                                  -sys.setdefaultencoding('iso-8859-1') 2
                                                                                  -
                                                                                  - - - - - - - - - -
                                                                                  1 -sitecustomize.py is a special script; Python will try to import it on startup, so any code in it will be run automatically. As the comment mentions, it can go anywhere +sys.setdefaultencoding('iso-8859-1') +
                                                                                  +
                                                                                    +
                                                                                  1. sitecustomize.py is a special script; Python will try to import it on startup, so any code in it will be run automatically. As the comment mentions, it can go anywhere (as long as import can find it), but it usually goes in the site-packages directory within your Python lib directory. -
                                                                                  2 -setdefaultencoding function sets, well, the default encoding. This is the encoding scheme that Python will try to use whenever it needs to auto-coerce a unicode string into a regular string. -
                                                                                  -

                                                                                  Example 9.16. Effects of setting the default encoding

                                                                                  ->>> import sys
                                                                                  ->>> sys.getdefaultencoding() 1
                                                                                  +
                                                                                3. setdefaultencoding function sets, well, the default encoding. This is the encoding scheme that Python will try to use whenever it needs to auto-coerce a unicode string into a regular string. +

                                                                                  Example 9.16. Effects of setting the default encoding

                                                                                  +>>> import sys
                                                                                  +>>> sys.getdefaultencoding() 
                                                                                   'iso-8859-1'
                                                                                  ->>> s = u'La Pe\xf1a'
                                                                                  ->>> print s2
                                                                                  -La Peña
                                                                                  - - - - - - - - - -
                                                                                  1 -This example assumes that you have made the changes listed in the previous example to your sitecustomize.py file, and restarted Python. If your default encoding still says 'ascii', you didn't set up your sitecustomize.py properly, or you didn't restart Python. The default encoding can only be changed during Python startup; you can't change it later. (Due to some wacky programming tricks that I won't get into right now, you can't even +>>> s = u'La Pe\xf1a' +>>> print s +La Peña
                                                                                  +
                                                                                    +
                                                                                  1. This example assumes that you have made the changes listed in the previous example to your sitecustomize.py file, and restarted Python. If your default encoding still says 'ascii', you didn't set up your sitecustomize.py properly, or you didn't restart Python. The default encoding can only be changed during Python startup; you can't change it later. (Due to some wacky programming tricks that I won't get into right now, you can't even call sys.setdefaultencoding after Python has started up. Dig into site.py and search for “setdefaultencoding” to find out how.) -
                                                                                  2 -Now that the default encoding scheme includes all the characters you use in your string, Python has no problem auto-coercing the string and printing it. -
                                                                                  -

                                                                                  Example 9.17. Specifying encoding in .py files

                                                                                  -

                                                                                  If you are going to be storing non-ASCII strings within your Python code, you'll need to specify the encoding of each individual .py file by putting an encoding declaration at the top of each file. This declaration defines the .py file to be UTF-8:

                                                                                  +
                                                                                4. Now that the default encoding scheme includes all the characters you use in your string, Python has no problem auto-coercing the string and printing it. +

                                                                                  Example 9.17. Specifying encoding in .py files

                                                                                  +

                                                                                  If you are going to be storing non-ASCII strings within your Python code, you'll need to specify the encoding of each individual .py file by putting an encoding declaration at the top of each file. This declaration defines the .py file to be UTF-8:

                                                                                  
                                                                                   #!/usr/bin/env python
                                                                                   # -*- coding: UTF-8 -*-
                                                                                   

                                                                                  Now, what about XML? Well, every XML document is in a specific encoding. Again, ISO-8859-1 is a popular encoding for data in Western European languages. KOI8-R is popular for Russian texts. The encoding, if specified, is in the header of the XML document. -

                                                                                  Example 9.18. russiansample.xml

                                                                                  
                                                                                  -<?xml version="1.0" encoding="koi8-r"?>       1
                                                                                  +

                                                                                  Example 9.18. russiansample.xml

                                                                                  
                                                                                  +<?xml version="1.0" encoding="koi8-r"?>       
                                                                                   <preface>
                                                                                  -<title>Предисловие</title>  2
                                                                                  -</preface>
                                                                                  - - - - - - - - - -
                                                                                  1 -This is a sample extract from a real Russian XML document; it's part of a Russian translation of this very book. Note the encoding, koi8-r, specified in the header. -
                                                                                  2 -These are Cyrillic characters which, as far as I know, spell the Russian word for “Preface”. If you open this file in a regular text editor, the characters will most likely like gibberish, because they're encoded +<title>Предисловие</title> +</preface>
                                                                                  +
                                                                                    +
                                                                                  1. This is a sample extract from a real Russian XML document; it's part of a Russian translation of this very book. Note the encoding, koi8-r, specified in the header. +
                                                                                  2. These are Cyrillic characters which, as far as I know, spell the Russian word for “Preface”. If you open this file in a regular text editor, the characters will most likely like gibberish, because they're encoded using the koi8-r encoding scheme, but they're being displayed in iso-8859-1. -
                                                                                  -

                                                                                  Example 9.19. Parsing russiansample.xml

                                                                                  ->>> from xml.dom import minidom
                                                                                  ->>> xmldoc = minidom.parse('russiansample.xml') 1
                                                                                  ->>> title = xmldoc.getElementsByTagName('title')[0].firstChild.data
                                                                                  ->>> title   2
                                                                                  +

                                                                                  Example 9.19. Parsing russiansample.xml

                                                                                  +>>> from xml.dom import minidom
                                                                                  +>>> xmldoc = minidom.parse('russiansample.xml') 
                                                                                  +>>> title = xmldoc.getElementsByTagName('title')[0].firstChild.data
                                                                                  +>>> title   
                                                                                   u'\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435'
                                                                                  ->>> print title               3
                                                                                  -Traceback (innermost last):
                                                                                  +>>> print title               
                                                                                  +Traceback (innermost last):
                                                                                     File "<interactive input>", line 1, in ?
                                                                                   UnicodeError: ASCII encoding error: ordinal not in range(128)
                                                                                  ->>> convertedtitle = title.encode('koi8-r')     4
                                                                                  ->>> convertedtitle
                                                                                  +>>> convertedtitle = title.encode('koi8-r')     
                                                                                  +>>> convertedtitle
                                                                                   '\xf0\xd2\xc5\xc4\xc9\xd3\xcc\xcf\xd7\xc9\xc5'
                                                                                  ->>> print convertedtitle      5
                                                                                  -Предисловие
                                                                                  - - - - - - - - - - - - - - - - - - - - - -
                                                                                  1 -I'm assuming here that you saved the previous example as russiansample.xml in the current directory. I am also, for the sake of completeness, assuming that you've changed your default encoding back +>>> print convertedtitle +Предисловие
                                                                                  +
                                                                                    +
                                                                                  1. I'm assuming here that you saved the previous example as russiansample.xml in the current directory. I am also, for the sake of completeness, assuming that you've changed your default encoding back to 'ascii' by removing your sitecustomize.py file, or at least commenting out the setdefaultencoding line. -
                                                                                  2 -Note that the text data of the title tag (now in the title variable, thanks to that long concatenation of Python functions which I hastily skipped over and, annoyingly, won't explain until the next section) -- the text data inside the +
                                                                                5. Note that the text data of the title tag (now in the title variable, thanks to that long concatenation of Python functions which I hastily skipped over and, annoyingly, won't explain until the next section) -- the text data inside the XML document's title element is stored in unicode. -
                                                                                6. 3 -Printing the title is not possible, because this unicode string contains non-ASCII characters, so Python can't convert it to ASCII because that doesn't make sense. -
                                                                                  4 -You can, however, explicitly convert it to koi8-r, in which case you get a (regular, not unicode) string of single-byte characters (f0, d2, c5, and so forth) that are the koi8-r-encoded versions of the characters in the original unicode string. -
                                                                                  5 -Printing the koi8-r-encoded string will probably show gibberish on your screen, because your Python IDE is interpreting those characters as iso-8859-1, not koi8-r. But at least they do print. (And, if you look carefully, it's the same gibberish that you saw when you opened the original +
                                                                                7. Printing the title is not possible, because this unicode string contains non-ASCII characters, so Python can't convert it to ASCII because that doesn't make sense. +
                                                                                8. You can, however, explicitly convert it to koi8-r, in which case you get a (regular, not unicode) string of single-byte characters (f0, d2, c5, and so forth) that are the koi8-r-encoded versions of the characters in the original unicode string. +
                                                                                9. Printing the koi8-r-encoded string will probably show gibberish on your screen, because your Python IDE is interpreting those characters as iso-8859-1, not koi8-r. But at least they do print. (And, if you look carefully, it's the same gibberish that you saw when you opened the original XML document in a non-unicode-aware text editor. Python converted it from koi8-r into unicode when it parsed the XML document, and you've just converted it back.) -
                                                                                10. To sum up, unicode itself is a bit intimidating if you've never seen it before, but unicode data is really very easy to handle in Python. If your XML documents are all 7-bit ASCII (like the examples in this chapter), you will literally never think about unicode. Python will convert the ASCII data in the XML documents into unicode while parsing, and auto-coerce it back to ASCII whenever necessary, and you'll never even notice. But if you need to deal with that in other languages, Python is ready. -

                                                                                  +

                                                                                  Further reading

                                                                                  • Unicode.org is the home page of the unicode standard, including a brief technical introduction. @@ -7271,7 +4288,7 @@ in Python. If your XML documents are all 7-bit ASCII

                                                                                    Traversing XML documents by stepping through each node can be tedious. If you're looking for something in particular, buried deep within your XML document, there is a shortcut you can use to find it quickly: getElementsByTagName.

                                                                                    For this section, you'll be using the binary.xml grammar file, which looks like this: -

                                                                                    Example 9.20. binary.xml

                                                                                    <?xml version="1.0"?>
                                                                                    +

                                                                                    Example 9.20. binary.xml

                                                                                    <?xml version="1.0"?>
                                                                                     <!DOCTYPE grammar PUBLIC "-//diveintopython3.org//DTD Kant Generator Pro v1.0//EN" "kgp.dtd">
                                                                                     <grammar>
                                                                                     <ref id="bit">
                                                                                    @@ -7283,207 +4300,121 @@ in Python. If your XML documents are all 7-bit ASCII
                                                                                     <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
                                                                                     </ref>
                                                                                     </grammar>

                                                                                    It has two refs, 'bit' and 'byte'. A bit is either a '0' or '1', and a byte is 8 bits. -

                                                                                    Example 9.21. Introducing getElementsByTagName

                                                                                    ->>> from xml.dom import minidom
                                                                                    ->>> xmldoc = minidom.parse('binary.xml')
                                                                                    ->>> reflist = xmldoc.getElementsByTagName('ref') 1
                                                                                    ->>> reflist
                                                                                    +

                                                                                    Example 9.21. Introducing getElementsByTagName

                                                                                    +>>> from xml.dom import minidom
                                                                                    +>>> xmldoc = minidom.parse('binary.xml')
                                                                                    +>>> reflist = xmldoc.getElementsByTagName('ref') 
                                                                                    +>>> reflist
                                                                                     [<DOM Element: ref at 136138108>, <DOM Element: ref at 136144292>]
                                                                                    ->>> print reflist[0].toxml()
                                                                                    -<ref id="bit">
                                                                                    +>>> print reflist[0].toxml()
                                                                                    +<ref id="bit">
                                                                                       <p>0</p>
                                                                                       <p>1</p>
                                                                                     </ref>
                                                                                    ->>> print reflist[1].toxml()
                                                                                    -<ref id="byte">
                                                                                    +>>> print reflist[1].toxml()
                                                                                    +<ref id="byte">
                                                                                       <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
                                                                                     <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
                                                                                     </ref>
                                                                                    -
                                                                                    - - - - - -
                                                                                    1 -getElementsByTagName takes one argument, the name of the element you wish to find. It returns a list of Element objects, corresponding to the XML elements that have that name. In this case, you find two ref elements. -
                                                                                    -

                                                                                    Example 9.22. Every element is searchable

                                                                                    ->>> firstref = reflist[0]    1
                                                                                    ->>> print firstref.toxml()
                                                                                    -<ref id="bit">
                                                                                    +
                                                                                    +
                                                                                      +
                                                                                    1. getElementsByTagName takes one argument, the name of the element you wish to find. It returns a list of Element objects, corresponding to the XML elements that have that name. In this case, you find two ref elements. +

                                                                                      Example 9.22. Every element is searchable

                                                                                      +>>> firstref = reflist[0]    
                                                                                      +>>> print firstref.toxml()
                                                                                      +<ref id="bit">
                                                                                         <p>0</p>
                                                                                         <p>1</p>
                                                                                       </ref>
                                                                                      ->>> plist = firstref.getElementsByTagName("p") 2
                                                                                      ->>> plist
                                                                                      +>>> plist = firstref.getElementsByTagName("p") 
                                                                                      +>>> plist
                                                                                       [<DOM Element: p at 136140116>, <DOM Element: p at 136142172>]
                                                                                      ->>> print plist[0].toxml()   3
                                                                                      +>>> print plist[0].toxml()   
                                                                                       <p>0</p>
                                                                                      ->>> print plist[1].toxml()
                                                                                      -<p>1</p>
                                                                                      - - - - - - - - - - - - - -
                                                                                      1 -Continuing from the previous example, the first object in your reflist is the 'bit' ref element. -
                                                                                      2 -You can use the same getElementsByTagName method on this Element to find all the <p> elements within the 'bit' ref element. -
                                                                                      3 -Just as before, the getElementsByTagName method returns a list of all the elements it found. In this case, you have two, one for each bit. -
                                                                                      -

                                                                                      Example 9.23. Searching is actually recursive

                                                                                      ->>> plist = xmldoc.getElementsByTagName("p") 1
                                                                                      ->>> plist
                                                                                      +>>> print plist[1].toxml()
                                                                                      +<p>1</p>
                                                                                      +
                                                                                        +
                                                                                      1. Continuing from the previous example, the first object in your reflist is the 'bit' ref element. +
                                                                                      2. You can use the same getElementsByTagName method on this Element to find all the <p> elements within the 'bit' ref element. +
                                                                                      3. Just as before, the getElementsByTagName method returns a list of all the elements it found. In this case, you have two, one for each bit. +

                                                                                        Example 9.23. Searching is actually recursive

                                                                                        +>>> plist = xmldoc.getElementsByTagName("p") 
                                                                                        +>>> plist
                                                                                         [<DOM Element: p at 136140116>, <DOM Element: p at 136142172>, <DOM Element: p at 136146124>]
                                                                                        ->>> plist[0].toxml()       2
                                                                                        +>>> plist[0].toxml()       
                                                                                         '<p>0</p>'
                                                                                        ->>> plist[1].toxml()
                                                                                        +>>> plist[1].toxml()
                                                                                         '<p>1</p>'
                                                                                        ->>> plist[2].toxml()       3
                                                                                        -'<p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
                                                                                        -<xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>'
                                                                                        - - - - - - - - - - - - - -
                                                                                        1 -Note carefully the difference between this and the previous example. Previously, you were searching for p elements within firstref, but here you are searching for p elements within xmldoc, the root-level object that represents the entire XML document. This does find the p elements nested within the ref elements within the root grammar element. -
                                                                                        2 -The first two p elements are within the first ref (the 'bit' ref). -
                                                                                        3 -The last p element is the one within the second ref (the 'byte' ref). -
                                                                                        +>>> plist[2].toxml() +'<p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\ +<xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>'
                                                                                    +
                                                                                      +
                                                                                    1. Note carefully the difference between this and the previous example. Previously, you were searching for p elements within firstref, but here you are searching for p elements within xmldoc, the root-level object that represents the entire XML document. This does find the p elements nested within the ref elements within the root grammar element. +
                                                                                    2. The first two p elements are within the first ref (the 'bit' ref). +
                                                                                    3. The last p element is the one within the second ref (the 'byte' ref).

                                                                                      9.6. Accessing element attributes

                                                                                      XML elements can have one or more attributes, and it is incredibly simple to access them once you have parsed an XML document. -

                                                                                      For this section, you'll be using the binary.xml grammar file that you saw in the previous section. - - - - -
                                                                                      Note
                                                                                      This section may be a little confusing, because of some overlapping terminology. Elements in an XML document have attributes, and Python objects also have attributes. When you parse an XML document, you get a bunch of Python objects that represent all the pieces of the XML document, and some of these Python objects represent attributes of the XML elements. But the (Python) objects that represent the (XML) attributes also have (Python) attributes, which are used to access various parts of the (XML) attribute that the object represents. I told you it was confusing. I am open to suggestions on how to distinguish these +

                                                                                      For this section, you'll be using the binary.xml grammar file that you saw in the previous section. + + + - -
                                                                                      NoteThis section may be a little confusing, because of some overlapping terminology. Elements in an XML document have attributes, and Python objects also have attributes. When you parse an XML document, you get a bunch of Python objects that represent all the pieces of the XML document, and some of these Python objects represent attributes of the XML elements. But the (Python) objects that represent the (XML) attributes also have (Python) attributes, which are used to access various parts of the (XML) attribute that the object represents. I told you it was confusing. I am open to suggestions on how to distinguish these more clearly. -
                                                                                      -

                                                                                      Example 9.24. Accessing element attributes

                                                                                      ->>> xmldoc = minidom.parse('binary.xml')
                                                                                      ->>> reflist = xmldoc.getElementsByTagName('ref')
                                                                                      ->>> bitref = reflist[0]
                                                                                      ->>> print bitref.toxml()
                                                                                      -<ref id="bit">
                                                                                      +

                                                                                      Example 9.24. Accessing element attributes

                                                                                      +>>> xmldoc = minidom.parse('binary.xml')
                                                                                      +>>> reflist = xmldoc.getElementsByTagName('ref')
                                                                                      +>>> bitref = reflist[0]
                                                                                      +>>> print bitref.toxml()
                                                                                      +<ref id="bit">
                                                                                         <p>0</p>
                                                                                         <p>1</p>
                                                                                       </ref>
                                                                                      ->>> bitref.attributes          1
                                                                                      +>>> bitref.attributes          
                                                                                       <xml.dom.minidom.NamedNodeMap instance at 0x81e0c9c>
                                                                                      ->>> bitref.attributes.keys()   2 3
                                                                                      +>>> bitref.attributes.keys()    
                                                                                       [u'id']
                                                                                      ->>> bitref.attributes.values() 4
                                                                                      +>>> bitref.attributes.values() 
                                                                                       [<xml.dom.minidom.Attr instance at 0x81d5044>]
                                                                                      ->>> bitref.attributes["id"]    5
                                                                                      -<xml.dom.minidom.Attr instance at 0x81d5044>
                                                                                      - - - - - - - - - - - - - - - - - - - - - -
                                                                                      1 -Each Element object has an attribute called attributes, which is a NamedNodeMap object. This sounds scary, but it's not, because a NamedNodeMap is an object that acts like a dictionary, so you already know how to use it. -
                                                                                      2 -Treating the NamedNodeMap as a dictionary, you can get a list of the names of the attributes of this element by using attributes.keys(). This element has only one attribute, 'id'. -
                                                                                      3 -Attribute names, like all other text in an XML document, are stored in unicode. -
                                                                                      4 -Again treating the NamedNodeMap as a dictionary, you can get a list of the values of the attributes by using attributes.values(). The values are themselves objects, of type Attr. You'll see how to get useful information out of this object in the next example. -
                                                                                      5 -Still treating the NamedNodeMap as a dictionary, you can access an individual attribute by name, using normal dictionary syntax. (Readers who have been +>>> bitref.attributes["id"] +<xml.dom.minidom.Attr instance at 0x81d5044>
                                                                                      +
                                                                                        +
                                                                                      1. Each Element object has an attribute called attributes, which is a NamedNodeMap object. This sounds scary, but it's not, because a NamedNodeMap is an object that acts like a dictionary, so you already know how to use it. +
                                                                                      2. Treating the NamedNodeMap as a dictionary, you can get a list of the names of the attributes of this element by using attributes.keys(). This element has only one attribute, 'id'. +
                                                                                      3. Attribute names, like all other text in an XML document, are stored in unicode. +
                                                                                      4. Again treating the NamedNodeMap as a dictionary, you can get a list of the values of the attributes by using attributes.values(). The values are themselves objects, of type Attr. You'll see how to get useful information out of this object in the next example. +
                                                                                      5. Still treating the NamedNodeMap as a dictionary, you can access an individual attribute by name, using normal dictionary syntax. (Readers who have been paying extra-close attention will already know how the NamedNodeMap class accomplishes this neat trick: by defining a __getitem__ special method. Other readers can take comfort in the fact that they don't need to understand how it works in order to use it effectively.) -
                                                                                      -

                                                                                      Example 9.25. Accessing individual attributes

                                                                                      ->>> a = bitref.attributes["id"]
                                                                                      ->>> a
                                                                                      +

                                                                                      Example 9.25. Accessing individual attributes

                                                                                      +>>> a = bitref.attributes["id"]
                                                                                      +>>> a
                                                                                       <xml.dom.minidom.Attr instance at 0x81d5044>
                                                                                      ->>> a.name  1
                                                                                      +>>> a.name  
                                                                                       u'id'
                                                                                      ->>> a.value 2
                                                                                      -u'bit'
                                                                                      - - - - - - - - - -
                                                                                      1 -The Attr object completely represents a single XML attribute of a single XML element. The name of the attribute (the same name as you used to find this object in the bitref.attributes NamedNodeMap pseudo-dictionary) is stored in a.name. -
                                                                                      2 -The actual text value of this XML attribute is stored in a.value. -
                                                                                      -
                                                                                      - - - - -
                                                                                      Note
                                                                                      Like a dictionary, attributes of an XML element have no ordering. Attributes may happen to be listed in a certain order in the original XML document, and the Attr objects may happen to be listed in a certain order when the XML document is parsed into Python objects, but these orders are arbitrary and should carry no special meaning. You should always access individual attributes +>>> a.value +u'bit'
                                                                                      +
                                                                                        +
                                                                                      1. The Attr object completely represents a single XML attribute of a single XML element. The name of the attribute (the same name as you used to find this object in the bitref.attributes NamedNodeMap pseudo-dictionary) is stored in a.name. +
                                                                                      2. The actual text value of this XML attribute is stored in a.value. + + + - -
                                                                                        NoteLike a dictionary, attributes of an XML element have no ordering. Attributes may happen to be listed in a certain order in the original XML document, and the Attr objects may happen to be listed in a certain order when the XML document is parsed into Python objects, but these orders are arbitrary and should carry no special meaning. You should always access individual attributes by name, like the keys of a dictionary. -

                                                                                        9.7. Segue

                                                                                        OK, that's it for the hard-core XML stuff. The next chapter will continue to use these same example programs, but focus on other aspects that make the program more flexible: using streams for input processing, using getattr for method dispatching, and using command-line flags to allow users to reconfigure the program without changing the code.

                                                                                        Before moving on to the next chapter, you should be comfortable doing all of these things: -

                                                                                        +
                                                                                        -


                                                                                        -
                                                                                        +


                                                                                        +

                                                                                        [5] This, sadly, is still an oversimplification. Unicode now has been extended to handle ancient Chinese, Korean, and Japanese texts, which had so many different characters that the 2-byte unicode system could not represent them all. But Python doesn't currently support that out of the box, and I don't know if there is a project afoot to add it. You've reached the limits of my expertise, sorry. -

                                                                                        +

                                                                                        Chapter 10. Scripts and Streams

                                                                                        10.1. Abstracting input sources

                                                                                        One of Python's greatest strengths is its dynamic binding, and one powerful use of dynamic binding is the file-like object. @@ -7496,13 +4427,13 @@ off and returns the next chunk of data. disk, a web page, even a hard-coded string. As long as you pass a file-like object to the function, and the function simply calls the object's read method, the function can handle any kind of input source without specific code to handle each kind.

                                                                                        In case you were wondering how this relates to XML processing, minidom.parse is one such function which can take a file-like object. -

                                                                                        Example 10.1. Parsing XML from a file

                                                                                        ->>> from xml.dom import minidom
                                                                                        ->>> fsock = open('binary.xml')    1
                                                                                        ->>> xmldoc = minidom.parse(fsock) 2
                                                                                        ->>> fsock.close()                 3
                                                                                        ->>> print xmldoc.toxml()          4
                                                                                        -<?xml version="1.0" ?>
                                                                                        +

                                                                                        Example 10.1. Parsing XML from a file

                                                                                        +>>> from xml.dom import minidom
                                                                                        +>>> fsock = open('binary.xml')    
                                                                                        +>>> xmldoc = minidom.parse(fsock) 
                                                                                        +>>> fsock.close()                 
                                                                                        +>>> print xmldoc.toxml()          
                                                                                        +<?xml version="1.0" ?>
                                                                                         <grammar>
                                                                                         <ref id="bit">
                                                                                           <p>0</p>
                                                                                        @@ -7512,42 +4443,21 @@ calls the object's read method, the function can handle any kind of
                                                                                           <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
                                                                                         <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
                                                                                         </ref>
                                                                                        -</grammar>
                                                                                        - - - - - - - - - - - - - - - - - -
                                                                                        1 -First, you open the file on disk. This gives you a file object. -
                                                                                        2 -You pass the file object to minidom.parse, which calls the read method of fsock and reads the XML document from the file on disk. -
                                                                                        3 -Be sure to call the close method of the file object after you're done with it. minidom.parse will not do this for you. -
                                                                                        4 -Calling the toxml() method on the returned XML document prints out the entire thing. -
                                                                                        +</grammar>
                                                                                        +
                                                                                          +
                                                                                        1. First, you open the file on disk. This gives you a file object. +
                                                                                        2. You pass the file object to minidom.parse, which calls the read method of fsock and reads the XML document from the file on disk. +
                                                                                        3. Be sure to call the close method of the file object after you're done with it. minidom.parse will not do this for you. +
                                                                                        4. Calling the toxml() method on the returned XML document prints out the entire thing.

                                                                                          Well, that all seems like a colossal waste of time. After all, you've already seen that minidom.parse can simply take the filename and do all the opening and closing nonsense automatically. And it's true that if you know you're just going to be parsing a local file, you can pass the filename and minidom.parse is smart enough to Do The Right Thing™. But notice how similar -- and easy -- it is to parse an XML document straight from the Internet. -

                                                                                          Example 10.2. Parsing XML from a URL

                                                                                          ->>> import urllib
                                                                                          ->>> usock = urllib.urlopen('http://slashdot.org/slashdot.rdf') 1
                                                                                          ->>> xmldoc = minidom.parse(usock)            2
                                                                                          ->>> usock.close()          3
                                                                                          ->>> print xmldoc.toxml()   4
                                                                                          -<?xml version="1.0" ?>
                                                                                          +

                                                                                          Example 10.2. Parsing XML from a URL

                                                                                          +>>> import urllib
                                                                                          +>>> usock = urllib.urlopen('http://slashdot.org/slashdot.rdf') 
                                                                                          +>>> xmldoc = minidom.parse(usock)            
                                                                                          +>>> usock.close()          
                                                                                          +>>> print xmldoc.toxml()   
                                                                                          +<?xml version="1.0" ?>
                                                                                           <rdf:RDF xmlns="http://my.netscape.com/rdf/simple/0.9/"
                                                                                            xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
                                                                                           
                                                                                          @@ -7568,170 +4478,85 @@ just going to be parsing a local file, you can pass the filename and minid
                                                                                           <link>http://slashdot.org/article.pl?sid=01/12/28/0421241</link>
                                                                                           </item>
                                                                                           
                                                                                          -[...snip...]
                                                                                          - - - - - - - - - - - - - - - - - -
                                                                                          1 -As you saw in a previous chapter, urlopen takes a web page URL and returns a file-like object. Most importantly, this object has a read method which returns the HTML source of the web page. -
                                                                                          2 -Now you pass the file-like object to minidom.parse, which obediently calls the read method of the object and parses the XML data that the read method returns. The fact that this XML data is now coming straight from a web page is completely irrelevant. minidom.parse doesn't know about web pages, and it doesn't care about web pages; it just knows about file-like objects. -
                                                                                          3 -As soon as you're done with it, be sure to close the file-like object that urlopen gives you. -
                                                                                          4 -By the way, this URL is real, and it really is XML. It's an XML representation of the current headlines on Slashdot, a technical news and gossip site. -
                                                                                          -

                                                                                          Example 10.3. Parsing XML from a string (the easy but inflexible way)

                                                                                          ->>> contents = "<grammar><ref id='bit'><p>0</p><p>1</p></ref></grammar>"
                                                                                          ->>> xmldoc = minidom.parseString(contents) 1
                                                                                          ->>> print xmldoc.toxml()
                                                                                          -<?xml version="1.0" ?>
                                                                                          -<grammar><ref id="bit"><p>0</p><p>1</p></ref></grammar>
                                                                                          - - - - - -
                                                                                          1 -minidom has a method, parseString, which takes an entire XML document as a string and parses it. You can use this instead of minidom.parse if you know you already have your entire XML document in a string. -
                                                                                          +[...snip...]
                                                                                          +
                                                                                            +
                                                                                          1. As you saw in a previous chapter, urlopen takes a web page URL and returns a file-like object. Most importantly, this object has a read method which returns the HTML source of the web page. +
                                                                                          2. Now you pass the file-like object to minidom.parse, which obediently calls the read method of the object and parses the XML data that the read method returns. The fact that this XML data is now coming straight from a web page is completely irrelevant. minidom.parse doesn't know about web pages, and it doesn't care about web pages; it just knows about file-like objects. +
                                                                                          3. As soon as you're done with it, be sure to close the file-like object that urlopen gives you. +
                                                                                          4. By the way, this URL is real, and it really is XML. It's an XML representation of the current headlines on Slashdot, a technical news and gossip site. +

                                                                                            Example 10.3. Parsing XML from a string (the easy but inflexible way)

                                                                                            +>>> contents = "<grammar><ref id='bit'><p>0</p><p>1</p></ref></grammar>"
                                                                                            +>>> xmldoc = minidom.parseString(contents) 
                                                                                            +>>> print xmldoc.toxml()
                                                                                            +<?xml version="1.0" ?>
                                                                                            +<grammar><ref id="bit"><p>0</p><p>1</p></ref></grammar>
                                                                                            +
                                                                                              +
                                                                                            1. minidom has a method, parseString, which takes an entire XML document as a string and parses it. You can use this instead of minidom.parse if you know you already have your entire XML document in a string.

                                                                                              OK, so you can use the minidom.parse function for parsing both local files and remote URLs, but for parsing strings, you use... a different function. That means that if you want to be able to take input from a file, a URL, or a string, you'll need special logic to check whether it's a string, and call the parseString function instead. How unsatisfying.

                                                                                              If there were a way to turn a string into a file-like object, then you could simply pass this object to minidom.parse. And in fact, there is a module specifically designed for doing just that: StringIO. -

                                                                                              Example 10.4. Introducing StringIO

                                                                                              ->>> contents = "<grammar><ref id='bit'><p>0</p><p>1</p></ref></grammar>"
                                                                                              ->>> import StringIO
                                                                                              ->>> ssock = StringIO.StringIO(contents)   1
                                                                                              ->>> ssock.read()        2
                                                                                              +

                                                                                              Example 10.4. Introducing StringIO

                                                                                              +>>> contents = "<grammar><ref id='bit'><p>0</p><p>1</p></ref></grammar>"
                                                                                              +>>> import StringIO
                                                                                              +>>> ssock = StringIO.StringIO(contents)   
                                                                                              +>>> ssock.read()        
                                                                                               "<grammar><ref id='bit'><p>0</p><p>1</p></ref></grammar>"
                                                                                              ->>> ssock.read()        3
                                                                                              +>>> ssock.read()        
                                                                                               ''
                                                                                              ->>> ssock.seek(0)       4
                                                                                              ->>> ssock.read(15)      5
                                                                                              +>>> ssock.seek(0)       
                                                                                              +>>> ssock.read(15)      
                                                                                               '<grammar><ref i'
                                                                                              ->>> ssock.read(15)
                                                                                              +>>> ssock.read(15)
                                                                                               "d='bit'><p>0</p"
                                                                                              ->>> ssock.read()
                                                                                              +>>> ssock.read()
                                                                                               '><p>1</p></ref></grammar>'
                                                                                              ->>> ssock.close()       6
                                                                                              - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                              1 -The StringIO module contains a single class, also called StringIO, which allows you to turn a string into a file-like object. The StringIO class takes the string as a parameter when creating an instance. -
                                                                                              2 -Now you have a file-like object, and you can do all sorts of file-like things with it. Like read, which returns the original string. -
                                                                                              3 -Calling read again returns an empty string. This is how real file objects work too; once you read the entire file, you can't read any +>>> ssock.close()
                                                                                              +
                                                                                                +
                                                                                              1. The StringIO module contains a single class, also called StringIO, which allows you to turn a string into a file-like object. The StringIO class takes the string as a parameter when creating an instance. +
                                                                                              2. Now you have a file-like object, and you can do all sorts of file-like things with it. Like read, which returns the original string. +
                                                                                              3. Calling read again returns an empty string. This is how real file objects work too; once you read the entire file, you can't read any more without explicitly seeking to the beginning of the file. The StringIO object works the same way. -
                                                                                              4 -You can explicitly seek to the beginning of the string, just like seeking through a file, by using the seek method of the StringIO object. -
                                                                                              5 -You can also read the string in chunks, by passing a size parameter to the read method. -
                                                                                              6 -At any time, read will return the rest of the string that you haven't read yet. All of this is exactly how file objects work; hence the term +
                                                                                            2. You can explicitly seek to the beginning of the string, just like seeking through a file, by using the seek method of the StringIO object. +
                                                                                            3. You can also read the string in chunks, by passing a size parameter to the read method. +
                                                                                            4. At any time, read will return the rest of the string that you haven't read yet. All of this is exactly how file objects work; hence the term file-like object. -
                                                                                            5. -

                                                                                              Example 10.5. Parsing XML from a string (the file-like object way)

                                                                                              ->>> contents = "<grammar><ref id='bit'><p>0</p><p>1</p></ref></grammar>"
                                                                                              ->>> ssock = StringIO.StringIO(contents)
                                                                                              ->>> xmldoc = minidom.parse(ssock) 1
                                                                                              ->>> ssock.close()
                                                                                              ->>> print xmldoc.toxml()
                                                                                              -<?xml version="1.0" ?>
                                                                                              -<grammar><ref id="bit"><p>0</p><p>1</p></ref></grammar>
                                                                                              - - - - - -
                                                                                              1 -Now you can pass the file-like object (really a StringIO) to minidom.parse, which will call the object's read method and happily parse away, never knowing that its input came from a hard-coded string. -
                                                                                              +

                                                                                              Example 10.5. Parsing XML from a string (the file-like object way)

                                                                                              +>>> contents = "<grammar><ref id='bit'><p>0</p><p>1</p></ref></grammar>"
                                                                                              +>>> ssock = StringIO.StringIO(contents)
                                                                                              +>>> xmldoc = minidom.parse(ssock) 
                                                                                              +>>> ssock.close()
                                                                                              +>>> print xmldoc.toxml()
                                                                                              +<?xml version="1.0" ?>
                                                                                              +<grammar><ref id="bit"><p>0</p><p>1</p></ref></grammar>
                                                                                              +
                                                                                                +
                                                                                              1. Now you can pass the file-like object (really a StringIO) to minidom.parse, which will call the object's read method and happily parse away, never knowing that its input came from a hard-coded string.

                                                                                                So now you know how to use a single function, minidom.parse, to parse an XML document stored on a web page, in a local file, or in a hard-coded string. For a web page, you use urlopen to get a file-like object; for a local file, you use open; and for a string, you use StringIO. Now let's take it one step further and generalize these differences as well. -

                                                                                                Example 10.6. openAnything

                                                                                                -def openAnything(source):1
                                                                                                +

                                                                                                Example 10.6. openAnything

                                                                                                
                                                                                                +def openAnything(source):
                                                                                                     # try to open with urllib (if source is http, ftp, or file URL)
                                                                                                     import urllib       
                                                                                                     try:                
                                                                                                -        return urllib.urlopen(source)      2
                                                                                                +        return urllib.urlopen(source)      
                                                                                                     except (IOError, OSError):            
                                                                                                         pass            
                                                                                                 
                                                                                                     # try to open with native open function (if source is pathname)
                                                                                                     try:                
                                                                                                -        return open(source)                3
                                                                                                +        return open(source)                
                                                                                                     except (IOError, OSError):            
                                                                                                         pass            
                                                                                                 
                                                                                                     # treat source as string
                                                                                                     import StringIO     
                                                                                                -    return StringIO.StringIO(str(source))  4
                                                                                                - - - - - - - - - - - - - - - - - -
                                                                                                1 -The openAnything function takes a single parameter, source, and returns a file-like object. source is a string of some sort; it can either be a URL (like 'http://slashdot.org/slashdot.rdf'), a full or partial pathname to a local file (like 'binary.xml'), or a string that contains actual XML data to be parsed. -
                                                                                                2 -First, you see if source is a URL. You do this through brute force: you try to open it as a URL and silently ignore errors caused by trying to open something which is not a URL. This is actually elegant in the sense that, if urllib ever supports new types of URLs in the future, you will also support them without recoding. If urllib is able to open source, then the return kicks you out of the function immediately and the following try statements never execute. -
                                                                                                3 -On the other hand, if urllib yelled at you and told you that source wasn't a valid URL, you assume it's a path to a file on disk and try to open it. Again, you don't do anything fancy to check whether source is a valid filename or not (the rules for valid filenames vary wildly between different platforms anyway, so you'd probably + return StringIO.StringIO(str(source))
                                                                                                +
                                                                                                  +
                                                                                                1. The openAnything function takes a single parameter, source, and returns a file-like object. source is a string of some sort; it can either be a URL (like 'http://slashdot.org/slashdot.rdf'), a full or partial pathname to a local file (like 'binary.xml'), or a string that contains actual XML data to be parsed. +
                                                                                                2. First, you see if source is a URL. You do this through brute force: you try to open it as a URL and silently ignore errors caused by trying to open something which is not a URL. This is actually elegant in the sense that, if urllib ever supports new types of URLs in the future, you will also support them without recoding. If urllib is able to open source, then the return kicks you out of the function immediately and the following try statements never execute. +
                                                                                                3. On the other hand, if urllib yelled at you and told you that source wasn't a valid URL, you assume it's a path to a file on disk and try to open it. Again, you don't do anything fancy to check whether source is a valid filename or not (the rules for valid filenames vary wildly between different platforms anyway, so you'd probably get them wrong anyway). Instead, you just blindly open the file, and silently trap any errors. -
                                                                                                4 -By this point, you need to assume that source is a string that has hard-coded data in it (since nothing else worked), so you use StringIO to create a file-like object out of it and return that. (In fact, since you're using the str function, source doesn't even need to be a string; it could be any object, and you'll use its string representation, as defined by its __str__ special method.) -
                                                                                                +
                                                                                              2. By this point, you need to assume that source is a string that has hard-coded data in it (since nothing else worked), so you use StringIO to create a file-like object out of it and return that. (In fact, since you're using the str function, source doesn't even need to be a string; it could be any object, and you'll use its string representation, as defined by its __str__ special method.)

                                                                                                Now you can use this openAnything function in conjunction with minidom.parse to make a function that takes a source that refers to an XML document somehow (either as a URL, or a local filename, or a hard-coded XML document in a string) and parses it. -

                                                                                                Example 10.7. Using openAnything in kgp.py

                                                                                                +

                                                                                                Example 10.7. Using openAnything in kgp.py

                                                                                                
                                                                                                 class KantGenerator:
                                                                                                     def _load(self, source):
                                                                                                         sock = toolbox.openAnything(source)
                                                                                                @@ -7743,165 +4568,92 @@ class KantGenerator:
                                                                                                 

                                                                                                Standard output and standard error (commonly abbreviated stdout and stderr) are pipes that are built into every UNIX system. When you print something, it goes to the stdout pipe; when your program crashes and prints out debugging information (like a traceback in Python), it goes to the stderr pipe. Both of these pipes are ordinarily just connected to the terminal window where you are working, so when a program prints, you see the output, and when a program crashes, you see the debugging information. (If you're working on a system with a window-based Python IDE, stdout and stderr default to your “Interactive Window”.) -

                                                                                                Example 10.8. Introducing stdout and stderr

                                                                                                ->>> for i in range(3):
                                                                                                -...    print 'Dive in'             1
                                                                                                -Dive in
                                                                                                +

                                                                                                Example 10.8. Introducing stdout and stderr

                                                                                                +>>> for i in range(3):
                                                                                                +...    print 'Dive in'             
                                                                                                +Dive in
                                                                                                 Dive in
                                                                                                 Dive in
                                                                                                ->>> import sys
                                                                                                ->>> for i in range(3):
                                                                                                -...    sys.stdout.write('Dive in') 2
                                                                                                +>>> import sys
                                                                                                +>>> for i in range(3):
                                                                                                +...    sys.stdout.write('Dive in') 
                                                                                                 Dive inDive inDive in
                                                                                                ->>> for i in range(3):
                                                                                                -...    sys.stderr.write('Dive in') 3
                                                                                                -Dive inDive inDive in
                                                                                                - - - - - - - - - - - - - -
                                                                                                1 -As you saw in Example 6.9, “Simple Counters”, you can use Python's built-in range function to build simple counter loops that repeat something a set number of times. -
                                                                                                2 -stdout is a file-like object; calling its write function will print out whatever string you give it. In fact, this is what the print function really does; it adds a carriage return to the end of the string you're printing, and calls sys.stdout.write. -
                                                                                                3 -In the simplest case, stdout and stderr send their output to the same place: the Python IDE (if you're in one), or the terminal (if you're running Python from the command line). Like stdout, stderr does not add carriage returns for you; if you want them, add them yourself. -
                                                                                                +>>> for i in range(3): +... sys.stderr.write('Dive in') +Dive inDive inDive in
                                                                                                +
                                                                                                  +
                                                                                                1. As you saw in Example 6.9, “Simple Counters”, you can use Python's built-in range function to build simple counter loops that repeat something a set number of times. +
                                                                                                2. stdout is a file-like object; calling its write function will print out whatever string you give it. In fact, this is what the print function really does; it adds a carriage return to the end of the string you're printing, and calls sys.stdout.write. +
                                                                                                3. In the simplest case, stdout and stderr send their output to the same place: the Python IDE (if you're in one), or the terminal (if you're running Python from the command line). Like stdout, stderr does not add carriage returns for you; if you want them, add them yourself.

                                                                                                  stdout and stderr are both file-like objects, like the ones you discussed in Section 10.1, “Abstracting input sources”, but they are both write-only. They have no read method, only write. Still, they are file-like objects, and you can assign any other file- or file-like object to them to redirect their output. -

                                                                                                  Example 10.9. Redirecting output

                                                                                                  -[you@localhost kgp]$ python stdout.py
                                                                                                  +

                                                                                                  Example 10.9. Redirecting output

                                                                                                  +[you@localhost kgp]$ python stdout.py
                                                                                                   Dive in
                                                                                                  -[you@localhost kgp]$ cat out.log
                                                                                                  +[you@localhost kgp]$ cat out.log
                                                                                                   This message will be logged instead of displayed

                                                                                                  (On Windows, you can use type instead of cat to display the contents of a file.) -

                                                                                                  If you have not already done so, you can download this and other examples used in this book.

                                                                                                  +

                                                                                                  If you have not already done so, you can download this and other examples used in this book. +

                                                                                                  
                                                                                                   #stdout.py
                                                                                                   import sys
                                                                                                   
                                                                                                  -print 'Dive in'      1
                                                                                                  -saveout = sys.stdout 2
                                                                                                  -fsock = open('out.log', 'w')           3
                                                                                                  -sys.stdout = fsock   4
                                                                                                  -print 'This message will be logged instead of displayed' 5
                                                                                                  -sys.stdout = saveout 6
                                                                                                  -fsock.close()        7
                                                                                                  -
                                                                                                  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                  1 -This will print to the IDE “Interactive Window” (or the terminal, if running the script from the command line). -
                                                                                                  2 -Always save stdout before redirecting it, so you can set it back to normal later. -
                                                                                                  3 -Open a file for writing. If the file doesn't exist, it will be created. If the file does exist, it will be overwritten.
                                                                                                  4 -Redirect all further output to the new file you just opened.
                                                                                                  5 -This will be “printed” to the log file only; it will not be visible in the IDE window or on the screen. -
                                                                                                  6 -Set stdout back to the way it was before you mucked with it. -
                                                                                                  7 -Close the log file.
                                                                                                  +print 'Dive in' +saveout = sys.stdout +fsock = open('out.log', 'w') +sys.stdout = fsock +print 'This message will be logged instead of displayed' +sys.stdout = saveout +fsock.close() +
                                                                                                  +
                                                                                                    +
                                                                                                  1. This will print to the IDE “Interactive Window” (or the terminal, if running the script from the command line). +
                                                                                                  2. Always save stdout before redirecting it, so you can set it back to normal later. +
                                                                                                  3. Open a file for writing. If the file doesn't exist, it will be created. If the file does exist, it will be overwritten. +
                                                                                                  4. Redirect all further output to the new file you just opened. +
                                                                                                  5. This will be “printed” to the log file only; it will not be visible in the IDE window or on the screen. +
                                                                                                  6. Set stdout back to the way it was before you mucked with it. +
                                                                                                  7. Close the log file.

                                                                                                    Redirecting stderr works exactly the same way, using sys.stderr instead of sys.stdout. -

                                                                                                    Example 10.10. Redirecting error information

                                                                                                    -[you@localhost kgp]$ python stderr.py
                                                                                                    -[you@localhost kgp]$ cat error.log
                                                                                                    -Traceback (most recent line last):
                                                                                                    +

                                                                                                    Example 10.10. Redirecting error information

                                                                                                    +[you@localhost kgp]$ python stderr.py
                                                                                                    +[you@localhost kgp]$ cat error.log
                                                                                                    +Traceback (most recent line last):
                                                                                                       File "stderr.py", line 5, in ?
                                                                                                         raise Exception, 'this error will be logged'
                                                                                                    -Exception: this error will be logged

                                                                                                    If you have not already done so, you can download this and other examples used in this book.

                                                                                                    +Exception: this error will be logged

                                                                                                    If you have not already done so, you can download this and other examples used in this book. +

                                                                                                    
                                                                                                     #stderr.py
                                                                                                     import sys
                                                                                                     
                                                                                                    -fsock = open('error.log', 'w')               1
                                                                                                    -sys.stderr = fsock         2
                                                                                                    -raise Exception, 'this error will be logged' 3 4
                                                                                                    -
                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                    1 -Open the log file where you want to store debugging information.
                                                                                                    2 -Redirect standard error by assigning the file object of the newly-opened log file to stderr. -
                                                                                                    3 -Raise an exception. Note from the screen output that this does not print anything on screen. All the normal traceback information has been written to error.log. -
                                                                                                    4 -Also note that you're not explicitly closing your log file, nor are you setting stderr back to its original value. This is fine, since once the program crashes (because of the exception), Python will clean up and close the file for us, and it doesn't make any difference that stderr is never restored, since, as I mentioned, the program crashes and Python ends. Restoring the original is more important for stdout, if you expect to go do other stuff within the same script afterwards. -
                                                                                                    +fsock = open('error.log', 'w') +sys.stderr = fsock +raise Exception, 'this error will be logged' +
                                                                                                    +
                                                                                                      +
                                                                                                    1. Open the log file where you want to store debugging information. +
                                                                                                    2. Redirect standard error by assigning the file object of the newly-opened log file to stderr. +
                                                                                                    3. Raise an exception. Note from the screen output that this does not print anything on screen. All the normal traceback information has been written to error.log. +
                                                                                                    4. Also note that you're not explicitly closing your log file, nor are you setting stderr back to its original value. This is fine, since once the program crashes (because of the exception), Python will clean up and close the file for us, and it doesn't make any difference that stderr is never restored, since, as I mentioned, the program crashes and Python ends. Restoring the original is more important for stdout, if you expect to go do other stuff within the same script afterwards.

                                                                                                      Since it is so common to write error messages to standard error, there is a shorthand syntax that can be used instead of going through the hassle of redirecting it outright. -

                                                                                                      Example 10.11. Printing to stderr

                                                                                                      ->>> print 'entering function'
                                                                                                      +

                                                                                                      Example 10.11. Printing to stderr

                                                                                                      +>>> print 'entering function'
                                                                                                       entering function
                                                                                                      ->>> import sys
                                                                                                      ->>> print >> sys.stderr, 'entering function' 1
                                                                                                      +>>> import sys
                                                                                                      +>>> print >> sys.stderr, 'entering function' 
                                                                                                       entering function
                                                                                                      -
                                                                                                      - - - - - -
                                                                                                      1 -This shorthand syntax of the print statement can be used to write to any open file, or file-like object. In this case, you can redirect a single print statement to stderr without affecting subsequent print statements. -
                                                                                                      +
                                                                                                      +
                                                                                                        +
                                                                                                      1. This shorthand syntax of the print statement can be used to write to any open file, or file-like object. In this case, you can redirect a single print statement to stderr without affecting subsequent print statements.

                                                                                                        Standard input, on the other hand, is a read-only file object, and it represents the data flowing into the program from some previous program. This will likely not make much sense to classic Mac OS users, or even Windows users unless you were ever fluent on the MS-DOS command line. The way it works is that you can construct a chain of commands in a single line, so that one program's output becomes the input for the next program in the chain. The first program simply outputs to standard output (without doing any special redirecting itself, just doing normal print statements or whatever), and the next program reads from standard input, and the operating system takes care of connecting one program's output to the next program's input. -

                                                                                                        Example 10.12. Chaining commands

                                                                                                        -[you@localhost kgp]$ python kgp.py -g binary.xml         1
                                                                                                        +

                                                                                                        Example 10.12. Chaining commands

                                                                                                        +[you@localhost kgp]$ python kgp.py -g binary.xml         
                                                                                                         01100111
                                                                                                        -[you@localhost kgp]$ cat binary.xml    2
                                                                                                        -<?xml version="1.0"?>
                                                                                                        +[you@localhost kgp]$ cat binary.xml    
                                                                                                        +<?xml version="1.0"?>
                                                                                                         <!DOCTYPE grammar PUBLIC "-//diveintopython3.org//DTD Kant Generator Pro v1.0//EN" "kgp.dtd">
                                                                                                         <grammar>
                                                                                                         <ref id="bit">
                                                                                                        @@ -7913,44 +4665,23 @@ one program's output to the next program's input.
                                                                                                         <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
                                                                                                         </ref>
                                                                                                         </grammar>
                                                                                                        -[you@localhost kgp]$ cat binary.xml | python kgp.py -g - 3 4
                                                                                                        -10110001
                                                                                                        - - - - - - - - - - - - - - - - - -
                                                                                                        1 -As you saw in Section 9.1, “Diving in”, this will print a string of eight random bits, 0 or 1. -
                                                                                                        2 -This simply prints out the entire contents of binary.xml. (Windows users should use type instead of cat.) -
                                                                                                        3 -This prints the contents of binary.xml, but the “|” character, called the “pipe” character, means that the contents will not be printed to the screen. Instead, they will become the standard input of the +[you@localhost kgp]$ cat binary.xml | python kgp.py -g - +10110001
                                                                                                        +
                                                                                                          +
                                                                                                        1. As you saw in Section 9.1, “Diving in”, this will print a string of eight random bits, 0 or 1. +
                                                                                                        2. This simply prints out the entire contents of binary.xml. (Windows users should use type instead of cat.) +
                                                                                                        3. This prints the contents of binary.xml, but the “|” character, called the “pipe” character, means that the contents will not be printed to the screen. Instead, they will become the standard input of the next command, which in this case calls your Python script. -
                                                                                                        4 -Instead of specifying a module (like binary.xml), you specify “-”, which causes your script to load the grammar from standard input instead of from a specific file on disk. (More on how +
                                                                                                      2. Instead of specifying a module (like binary.xml), you specify “-”, which causes your script to load the grammar from standard input instead of from a specific file on disk. (More on how this happens in the next example.) So the effect is the same as the first syntax, where you specified the grammar filename directly, but think of the expansion possibilities here. Instead of simply doing cat binary.xml, you could run a script that dynamically generates the grammar, then you can pipe it into your script. It could come from anywhere: a database, or some grammar-generating meta-script, or whatever. The point is that you don't need to change your kgp.py script at all to incorporate any of this functionality. All you need to do is be able to take grammar files from standard input, and you can separate all the other logic into another program. -
                                                                                                      3. So how does the script “know” to read from standard input when the grammar file is “-”? It's not magic; it's just code. -

                                                                                                        Example 10.13. Reading from standard input in kgp.py

                                                                                                        +

                                                                                                        Example 10.13. Reading from standard input in kgp.py

                                                                                                        
                                                                                                         def openAnything(source):
                                                                                                        -    if source == "-":    1
                                                                                                        +    if source == "-":    
                                                                                                                 import sys
                                                                                                                 return sys.stdin
                                                                                                         
                                                                                                        @@ -7958,157 +4689,77 @@ def openAnything(source):
                                                                                                             import urllib
                                                                                                             try:
                                                                                                         
                                                                                                        -[... snip ...]
                                                                                                        - - - - - -
                                                                                                        1 -This is the openAnything function from toolbox.py, which you previously examined in Section 10.1, “Abstracting input sources”. All you've done is add three lines of code at the beginning of the function to check if the source is “-”; if so, you return sys.stdin. Really, that's it! Remember, stdin is a file-like object with a read method, so the rest of the code (in kgp.py, where you call openAnything) doesn't change a bit. -
                                                                                                        +[... snip ...]
                                                                                                        +
                                                                                                          +
                                                                                                        1. This is the openAnything function from toolbox.py, which you previously examined in Section 10.1, “Abstracting input sources”. All you've done is add three lines of code at the beginning of the function to check if the source is “-”; if so, you return sys.stdin. Really, that's it! Remember, stdin is a file-like object with a read method, so the rest of the code (in kgp.py, where you call openAnything) doesn't change a bit.

                                                                                                          10.3. Caching node lookups

                                                                                                          kgp.py employs several tricks which may or may not be useful to you in your XML processing. The first one takes advantage of the consistent structure of the input documents to build a cache of nodes.

                                                                                                          A grammar file defines a series of ref elements. Each ref contains one or more p elements, which can contain a lot of different things, including xrefs. Whenever you encounter an xref, you look for a corresponding ref element with the same id attribute, and choose one of the ref element's children and parse it. (You'll see how this random choice is made in the next section.)

                                                                                                          This is how you build up the grammar: define ref elements for the smallest pieces, then define ref elements which "include" the first ref elements by using xref, and so forth. Then you parse the "largest" reference and follow each xref, and eventually output real text. The text you output depends on the (random) decisions you make each time you fill in an xref, so the output is different each time.

                                                                                                          This is all very flexible, but there is one downside: performance. When you find an xref and need to find the corresponding ref element, you have a problem. The xref has an id attribute, and you want to find the ref element that has that same id attribute, but there is no easy way to do that. The slow way to do it would be to get the entire list of ref elements each time, then manually loop through and look at each id attribute. The fast way is to do that once and build a cache, in the form of a dictionary. -

                                                                                                          Example 10.14. loadGrammar

                                                                                                          +

                                                                                                          Example 10.14. loadGrammar

                                                                                                          
                                                                                                               def loadGrammar(self, grammar):       
                                                                                                                   self.grammar = self._load(grammar)
                                                                                                          -        self.refs = {}   1
                                                                                                          -        for ref in self.grammar.getElementsByTagName("ref"): 2
                                                                                                          -            self.refs[ref.attributes["id"].value] = ref      3 4
                                                                                                          - - - - - - - - - - - - - - - - - -
                                                                                                          1 -Start by creating an empty dictionary, self.refs. -
                                                                                                          2 -As you saw in Section 9.5, “Searching for elements”, getElementsByTagName returns a list of all the elements of a particular name. You easily can get a list of all the ref elements, then simply loop through that list. -
                                                                                                          3 -As you saw in Section 9.6, “Accessing element attributes”, you can access individual attributes of an element by name, using standard dictionary syntax. So the keys of the self.refs dictionary will be the values of the id attribute of each ref element. -
                                                                                                          4 -The values of the self.refs dictionary will be the ref elements themselves. As you saw in Section 9.3, “Parsing XML”, each element, each node, each comment, each piece of text in a parsed XML document is an object. -
                                                                                                          + self.refs = {} + for ref in self.grammar.getElementsByTagName("ref"): + self.refs[ref.attributes["id"].value] = ref
                                                                                                          +
                                                                                                            +
                                                                                                          1. Start by creating an empty dictionary, self.refs. +
                                                                                                          2. As you saw in Section 9.5, “Searching for elements”, getElementsByTagName returns a list of all the elements of a particular name. You easily can get a list of all the ref elements, then simply loop through that list. +
                                                                                                          3. As you saw in Section 9.6, “Accessing element attributes”, you can access individual attributes of an element by name, using standard dictionary syntax. So the keys of the self.refs dictionary will be the values of the id attribute of each ref element. +
                                                                                                          4. The values of the self.refs dictionary will be the ref elements themselves. As you saw in Section 9.3, “Parsing XML”, each element, each node, each comment, each piece of text in a parsed XML document is an object.

                                                                                                            Once you build this cache, whenever you come across an xref and need to find the ref element with the same id attribute, you can simply look it up in self.refs. -

                                                                                                            Example 10.15. Using the ref element cache

                                                                                                            +

                                                                                                            Example 10.15. Using the ref element cache

                                                                                                            
                                                                                                                 def do_xref(self, node):
                                                                                                                     id = node.attributes["id"].value
                                                                                                                     self.parse(self.randomChildElement(self.refs[id]))

                                                                                                            You'll explore the randomChildElement function in the next section.

                                                                                                            10.4. Finding direct children of a node

                                                                                                            Another useful techique when parsing XML documents is finding all the direct child elements of a particular element. For instance, in the grammar files, a ref element can have several p elements, each of which can contain many things, including other p elements. You want to find just the p elements that are children of the ref, not p elements that are children of other p elements.

                                                                                                            You might think you could simply use getElementsByTagName for this, but you can't. getElementsByTagName searches recursively and returns a single list for all the elements it finds. Since p elements can contain other p elements, you can't use getElementsByTagName, because it would return nested p elements that you don't want. To find only direct child elements, you'll need to do it yourself. -

                                                                                                            Example 10.16. Finding direct child elements

                                                                                                            +

                                                                                                            Example 10.16. Finding direct child elements

                                                                                                            
                                                                                                                 def randomChildElement(self, node):
                                                                                                                     choices = [e for e in node.childNodes
                                                                                                            - if e.nodeType == e.ELEMENT_NODE] 1 2 3
                                                                                                            -        chosen = random.choice(choices)             4
                                                                                                            -        return chosen            
                                                                                                            - - - - - - - - - - - - - - - - - -
                                                                                                            1 -As you saw in Example 9.9, “Getting child nodes”, the childNodes attribute returns a list of all the child nodes of an element. -
                                                                                                            2 -However, as you saw in Example 9.11, “Child nodes can be text”, the list returned by childNodes contains all different types of nodes, including text nodes. That's not what you're looking for here. You only want the + if e.nodeType == e.ELEMENT_NODE] + chosen = random.choice(choices) + return chosen
                                                                                                            +
                                                                                                              +
                                                                                                            1. As you saw in Example 9.9, “Getting child nodes”, the childNodes attribute returns a list of all the child nodes of an element. +
                                                                                                            2. However, as you saw in Example 9.11, “Child nodes can be text”, the list returned by childNodes contains all different types of nodes, including text nodes. That's not what you're looking for here. You only want the children that are elements. -
                                                                                                            3 -Each node has a nodeType attribute, which can be ELEMENT_NODE, TEXT_NODE, COMMENT_NODE, or any number of other values. The complete list of possible values is in the __init__.py file in the xml.dom package. (See Section 9.2, “Packages” for more on packages.) But you're just interested in nodes that are elements, so you can filter the list to only include +
                                                                                                          5. Each node has a nodeType attribute, which can be ELEMENT_NODE, TEXT_NODE, COMMENT_NODE, or any number of other values. The complete list of possible values is in the __init__.py file in the xml.dom package. (See Section 9.2, “Packages” for more on packages.) But you're just interested in nodes that are elements, so you can filter the list to only include those nodes whose nodeType is ELEMENT_NODE. -
                                                                                                          6. 4 -Once you have a list of actual elements, choosing a random one is easy. Python comes with a module called random which includes several useful functions. The random.choice function takes a list of any number of items and returns a random item. For example, if the ref elements contains several p elements, then choices would be a list of p elements, and chosen would end up being assigned exactly one of them, selected at random. -
                                                                                                            +
                                                                                                          7. Once you have a list of actual elements, choosing a random one is easy. Python comes with a module called random which includes several useful functions. The random.choice function takes a list of any number of items and returns a random item. For example, if the ref elements contains several p elements, then choices would be a list of p elements, and chosen would end up being assigned exactly one of them, selected at random.

                                                                                                            10.5. Creating separate handlers by node type

                                                                                                            The third useful XML processing tip involves separating your code into logical functions, based on node types and element names. Parsed XML documents are made up of various types of nodes, each represented by a Python object. The root level of the document itself is represented by a Document object. The Document then contains one or more Element objects (for actual XML tags), each of which may contain other Element objects, Text objects (for bits of text), or Comment objects (for embedded comments). Python makes it easy to write a dispatcher to separate the logic for each node type. -

                                                                                                            Example 10.17. Class names of parsed XML objects

                                                                                                            ->>> from xml.dom import minidom
                                                                                                            ->>> xmldoc = minidom.parse('kant.xml') 1
                                                                                                            ->>> xmldoc
                                                                                                            +

                                                                                                            Example 10.17. Class names of parsed XML objects

                                                                                                            +>>> from xml.dom import minidom
                                                                                                            +>>> xmldoc = minidom.parse('kant.xml') 
                                                                                                            +>>> xmldoc
                                                                                                             <xml.dom.minidom.Document instance at 0x01359DE8>
                                                                                                            ->>> xmldoc.__class__ 2
                                                                                                            +>>> xmldoc.__class__ 
                                                                                                             <class xml.dom.minidom.Document at 0x01105D40>
                                                                                                            ->>> xmldoc.__class__.__name__          3
                                                                                                            -'Document'
                                                                                                            - - - - - - - - - - - - - -
                                                                                                            1 -Assume for a moment that kant.xml is in the current directory. -
                                                                                                            2 -As you saw in Section 9.2, “Packages”, the object returned by parsing an XML document is a Document object, as defined in the minidom.py in the xml.dom package. As you saw in Section 5.4, “Instantiating Classes”, __class__ is built-in attribute of every Python object. -
                                                                                                            3 -Furthermore, __name__ is a built-in attribute of every Python class, and it is a string. This string is not mysterious; it's the same as the class name you type when you define a class +>>> xmldoc.__class__.__name__ +'Document'
                                                                                                            +
                                                                                                              +
                                                                                                            1. Assume for a moment that kant.xml is in the current directory. +
                                                                                                            2. As you saw in Section 9.2, “Packages”, the object returned by parsing an XML document is a Document object, as defined in the minidom.py in the xml.dom package. As you saw in Section 5.4, “Instantiating Classes”, __class__ is built-in attribute of every Python object. +
                                                                                                            3. Furthermore, __name__ is a built-in attribute of every Python class, and it is a string. This string is not mysterious; it's the same as the class name you type when you define a class yourself. (See Section 5.3, “Defining Classes”.) -

                                                                                                            Fine, so now you can get the class name of any particular XML node (since each XML node is represented as a Python object). How can you use this to your advantage to separate the logic of parsing each node type? The answer is getattr, which you first saw in Section 4.4, “Getting Object References With getattr”. -

                                                                                                            Example 10.18. parse, a generic XML node dispatcher

                                                                                                            +

                                                                                                            Example 10.18. parse, a generic XML node dispatcher

                                                                                                            
                                                                                                                 def parse(self, node):          
                                                                                                            -        parseMethod = getattr(self, "parse_%s" % node.__class__.__name__) 1 2
                                                                                                            -        parseMethod(node) 3
                                                                                                            - - - - - - - - - - - - - -
                                                                                                            1 -First off, notice that you're constructing a larger string based on the class name of the node you were passed (in the node argument). So if you're passed a Document node, you're constructing the string 'parse_Document', and so forth. -
                                                                                                            2 -Now you can treat that string as a function name, and get a reference to the function itself using getattr
                                                                                                            3 -Finally, you can call that function and pass the node itself as an argument. The next example shows the definitions of each + parseMethod = getattr(self, "parse_%s" % node.__class__.__name__) + parseMethod(node)
                                                                                                            +
                                                                                                              +
                                                                                                            1. First off, notice that you're constructing a larger string based on the class name of the node you were passed (in the node argument). So if you're passed a Document node, you're constructing the string 'parse_Document', and so forth. +
                                                                                                            2. Now you can treat that string as a function name, and get a reference to the function itself using getattr
                                                                                                            3. Finally, you can call that function and pass the node itself as an argument. The next example shows the definitions of each of these functions. -
                                                                                                            -

                                                                                                            Example 10.19. Functions called by the parse dispatcher

                                                                                                            -    def parse_Document(self, node): 1
                                                                                                            +

                                                                                                            Example 10.19. Functions called by the parse dispatcher

                                                                                                            
                                                                                                            +    def parse_Document(self, node): 
                                                                                                                     self.parse(node.documentElement)
                                                                                                             
                                                                                                            -    def parse_Text(self, node):    2
                                                                                                            +    def parse_Text(self, node):    
                                                                                                                     text = node.data
                                                                                                                     if self.capitalizeNextWord:
                                                                                                                         self.pieces.append(text[0].upper())
                                                                                                            @@ -8117,43 +4768,22 @@ def openAnything(source):
                                                                                                                     else:
                                                                                                                         self.pieces.append(text)
                                                                                                             
                                                                                                            -    def parse_Comment(self, node): 3
                                                                                                            +    def parse_Comment(self, node): 
                                                                                                                     pass
                                                                                                             
                                                                                                            -    def parse_Element(self, node): 4
                                                                                                            +    def parse_Element(self, node): 
                                                                                                                     handlerMethod = getattr(self, "do_%s" % node.tagName)
                                                                                                            -        handlerMethod(node)
                                                                                                            - - - - - - - - - - - - - - - - - -
                                                                                                            1 -parse_Document is only ever called once, since there is only one Document node in an XML document, and only one Document object in the parsed XML representation. It simply turns around and parses the root element of the grammar file. -
                                                                                                            2 -parse_Text is called on nodes that represent bits of text. The function itself does some special processing to handle automatic capitalization + handlerMethod(node)
                                                                                                            +
                                                                                                              +
                                                                                                            1. parse_Document is only ever called once, since there is only one Document node in an XML document, and only one Document object in the parsed XML representation. It simply turns around and parses the root element of the grammar file. +
                                                                                                            2. parse_Text is called on nodes that represent bits of text. The function itself does some special processing to handle automatic capitalization of the first word of a sentence, but otherwise simply appends the represented text to a list. -
                                                                                                            3 -parse_Comment is just a pass, since you don't care about embedded comments in the grammar files. Note, however, that you still need to define the function +
                                                                                                          8. parse_Comment is just a pass, since you don't care about embedded comments in the grammar files. Note, however, that you still need to define the function and explicitly make it do nothing. If the function did not exist, the generic parse function would fail as soon as it stumbled on a comment, because it would try to find the non-existent parse_Comment function. Defining a separate function for every node type, even ones you don't use, allows the generic parse function to stay simple and dumb. -
                                                                                                          9. 4 -The parse_Element method is actually itself a dispatcher, based on the name of the element's tag. The basic idea is the same: take what distinguishes +
                                                                                                          10. The parse_Element method is actually itself a dispatcher, based on the name of the element's tag. The basic idea is the same: take what distinguishes elements from each other (their tag names) and dispatch to a separate function for each of them. You construct a string like 'do_xref' (for an <xref> tag), find a function of that name, and call it. And so forth for each of the other tag names that might be found in the course of parsing a grammar file (<p> tags, <choice> tags). -
                                                                                                          11. In this example, the dispatch functions parse and parse_Element simply find other methods in the same class. If your processing is very complex (or you have many different tag names), you could break up your code into separate modules, and use dynamic importing to import each module and call whatever functions you needed. Dynamic importing will be discussed in Chapter 16, Functional Programming. @@ -8162,112 +4792,65 @@ you needed. Dynamic importing will be discussed in

                                                                                                            Example 10.20. Introducing sys.argv

                                                                                                            -

                                                                                                            If you have not already done so, you can download this and other examples used in this book.

                                                                                                            +

                                                                                                            Example 10.20. Introducing sys.argv

                                                                                                            +

                                                                                                            If you have not already done so, you can download this and other examples used in this book. +

                                                                                                            
                                                                                                             #argecho.py
                                                                                                             import sys
                                                                                                             
                                                                                                            -for arg in sys.argv: 1
                                                                                                            -    print arg
                                                                                                            - - - - - -
                                                                                                            1 -Each command-line argument passed to the program will be in sys.argv, which is just a list. Here you are printing each argument on a separate line. -
                                                                                                            -

                                                                                                            Example 10.21. The contents of sys.argv

                                                                                                            -[you@localhost py]$ python argecho.py             1
                                                                                                            +for arg in sys.argv: 
                                                                                                            +    print arg
                                                                                                            +
                                                                                                              +
                                                                                                            1. Each command-line argument passed to the program will be in sys.argv, which is just a list. Here you are printing each argument on a separate line. +

                                                                                                              Example 10.21. The contents of sys.argv

                                                                                                              +[you@localhost py]$ python argecho.py             
                                                                                                               argecho.py
                                                                                                              -[you@localhost py]$ python argecho.py abc def     2
                                                                                                              -argecho.py
                                                                                                              +[you@localhost py]$ python argecho.py abc def     
                                                                                                              +argecho.py
                                                                                                               abc
                                                                                                               def
                                                                                                              -[you@localhost py]$ python argecho.py --help      3
                                                                                                              -argecho.py
                                                                                                              +[you@localhost py]$ python argecho.py --help      
                                                                                                              +argecho.py
                                                                                                               --help
                                                                                                              -[you@localhost py]$ python argecho.py -m kant.xml 4
                                                                                                              -argecho.py
                                                                                                              +[you@localhost py]$ python argecho.py -m kant.xml 
                                                                                                              +argecho.py
                                                                                                               -m
                                                                                                              -kant.xml
                                                                                                              - - - - - - - - - - - - - - - - - -
                                                                                                              1 -The first thing to know about sys.argv is that it contains the name of the script you're calling. You will actually use this knowledge to your advantage later, +kant.xml
                                                                                                              +
                                                                                                                +
                                                                                                              1. The first thing to know about sys.argv is that it contains the name of the script you're calling. You will actually use this knowledge to your advantage later, in Chapter 16, Functional Programming. Don't worry about it for now. -
                                                                                                              2 -Command-line arguments are separated by spaces, and each shows up as a separate element in the sys.argv list. -
                                                                                                              3 -Command-line flags, like --help, also show up as their own element in the sys.argv list. -
                                                                                                              4 -To make things even more interesting, some command-line flags themselves take arguments. For instance, here you have a flag +
                                                                                                            2. Command-line arguments are separated by spaces, and each shows up as a separate element in the sys.argv list. +
                                                                                                            3. Command-line flags, like --help, also show up as their own element in the sys.argv list. +
                                                                                                            4. To make things even more interesting, some command-line flags themselves take arguments. For instance, here you have a flag (-m) which takes an argument (kant.xml). Both the flag itself and the flag's argument are simply sequential elements in the sys.argv list. No attempt is made to associate one with the other; all you get is a list. -
                                                                                                            5. So as you can see, you certainly have all the information passed on the command line, but then again, it doesn't look like it's going to be all that easy to actually use it. For simple programs that only take a single argument and have no flags, you can simply use sys.argv[1] to access the argument. There's no shame in this; I do it all the time. For more complex programs, you need the getopt module. -

                                                                                                              Example 10.22. Introducing getopt

                                                                                                              +

                                                                                                              Example 10.22. Introducing getopt

                                                                                                              
                                                                                                               def main(argv):       
                                                                                                              -    grammar = "kant.xml"                 1
                                                                                                              +    grammar = "kant.xml"                 
                                                                                                                   try:              
                                                                                                              -        opts, args = getopt.getopt(argv, "hg:d", ["help", "grammar="]) 2
                                                                                                              -    except getopt.GetoptError:           3
                                                                                                              -        usage()        4
                                                                                                              +        opts, args = getopt.getopt(argv, "hg:d", ["help", "grammar="]) 
                                                                                                              +    except getopt.GetoptError:           
                                                                                                              +        usage()        
                                                                                                                       sys.exit(2)   
                                                                                                               
                                                                                                               ...
                                                                                                               
                                                                                                               if __name__ == "__main__":
                                                                                                              -    main(sys.argv[1:])
                                                                                                              - - - - - - - - - - - - - - - - - -
                                                                                                              1 -First off, look at the bottom of the example and notice that you're calling the main function with sys.argv[1:]. Remember, sys.argv[0] is the name of the script that you're running; you don't care about that for command-line processing, so you chop it off + main(sys.argv[1:])
                                                                                                              +
                                                                                                                +
                                                                                                              1. First off, look at the bottom of the example and notice that you're calling the main function with sys.argv[1:]. Remember, sys.argv[0] is the name of the script that you're running; you don't care about that for command-line processing, so you chop it off and pass the rest of the list. -
                                                                                                              2 -This is where all the interesting processing happens. The getopt function of the getopt module takes three parameters: the argument list (which you got from sys.argv[1:]), a string containing all the possible single-character command-line flags that this program accepts, and a list of longer +
                                                                                                            6. This is where all the interesting processing happens. The getopt function of the getopt module takes three parameters: the argument list (which you got from sys.argv[1:]), a string containing all the possible single-character command-line flags that this program accepts, and a list of longer command-line flags that are equivalent to the single-character versions. This is quite confusing at first glance, and is explained in more detail below. -
                                                                                                            7. 3 -If anything goes wrong trying to parse these command-line flags, getopt will raise an exception, which you catch. You told getopt all the flags you understand, so this probably means that the end user passed some command-line flag that you don't understand. -
                                                                                                              4 -As is standard practice in the UNIX world, when the script is passed flags it doesn't understand, you print out a summary of proper usage and exit gracefully. +
                                                                                                            8. If anything goes wrong trying to parse these command-line flags, getopt will raise an exception, which you catch. You told getopt all the flags you understand, so this probably means that the end user passed some command-line flag that you don't understand. +
                                                                                                            9. As is standard practice in the UNIX world, when the script is passed flags it doesn't understand, you print out a summary of proper usage and exit gracefully. Note that I haven't shown the usage function here. You would still need to code that somewhere and have it print out the appropriate summary; it's not automatic. -
                                                                                                            10. So what are all those parameters you pass to the getopt function? Well, the first one is simply the raw list of command-line flags and arguments (not including the first element, the script name, which you already chopped off before calling the main function). The second is the list of short command-line flags that the script accepts. -

                                                                                                              +

                                                                                                              "hg:d"

                                                                                                              -h
                                                                                                              @@ -8281,7 +4864,7 @@ the script name, which you already chopped off before calling the main-g) must be followed by an argument, which is the name of the grammar file to read from. In fact it can be a filename or a web address, and you don't know which yet (you'll figure it out later), but you know it has to be something. So you tell getopt this by putting a colon after the g in that second parameter to the getopt function.

                                                                                                              To further complicate things, the script accepts either short flags (like -h) or long flags (like --help), and you want them to do the same thing. This is what the third parameter to getopt is for, to specify a list of the long flags that correspond to the short flags you specified in the second parameter. -

                                                                                                              +

                                                                                                              ["help", "grammar="]

                                                                                                              --help
                                                                                                              @@ -8290,7 +4873,7 @@ and you don't know which yet (you'll figure it out later), but you know it has t
                                                                                                              use specified grammar file or URL

                                                                                                              Three things of note here: -

                                                                                                              +
                                                                                                              1. All long flags are preceded by two dashes on the command line, but you don't include those dashes when calling getopt. They are understood. @@ -8301,76 +4884,45 @@ and you don't know which yet (you'll figure it out later), but you know it has t

                                                                                                              Confused yet? Let's look at the actual code and see if it makes sense in context. -

                                                                                                              Example 10.23. Handling command-line arguments in kgp.py

                                                                                                              -def main(argv):        1
                                                                                                              +

                                                                                                              Example 10.23. Handling command-line arguments in kgp.py

                                                                                                              
                                                                                                              +def main(argv):        
                                                                                                                   grammar = "kant.xml"                
                                                                                                                   try:              
                                                                                                                       opts, args = getopt.getopt(argv, "hg:d", ["help", "grammar="])
                                                                                                                   except getopt.GetoptError:          
                                                                                                                       usage()       
                                                                                                                       sys.exit(2)   
                                                                                                              -    for opt, arg in opts:                2
                                                                                                              -        if opt in ("-h", "--help"):      3
                                                                                                              +    for opt, arg in opts:                
                                                                                                              +        if opt in ("-h", "--help"):      
                                                                                                                           usage()   
                                                                                                                           sys.exit()
                                                                                                              -        elif opt == '-d':                4
                                                                                                              +        elif opt == '-d':                
                                                                                                                           global _debug               
                                                                                                                           _debug = 1
                                                                                                              -        elif opt in ("-g", "--grammar"): 5
                                                                                                              +        elif opt in ("-g", "--grammar"): 
                                                                                                                           grammar = arg               
                                                                                                               
                                                                                                              -    source = "".join(args)               6
                                                                                                              +    source = "".join(args)               
                                                                                                               
                                                                                                                   k = KantGenerator(grammar, source)
                                                                                                              -    print k.output()
                                                                                                              - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                              1 -The grammar variable will keep track of the grammar file you're using. You initialize it here in case it's not specified on the command + print k.output()
                                                                                                              +
                                                                                                                +
                                                                                                              1. The grammar variable will keep track of the grammar file you're using. You initialize it here in case it's not specified on the command line (using either the -g or the --grammar flag). -
                                                                                                              2 -The opts variable that you get back from getopt contains a list of tuples: flag and argument. If the flag doesn't take an argument, then arg will simply be None. This makes it easier to loop through the flags. -
                                                                                                              3 -getopt validates that the command-line flags are acceptable, but it doesn't do any sort of conversion between short and long flags. +
                                                                                                            11. The opts variable that you get back from getopt contains a list of tuples: flag and argument. If the flag doesn't take an argument, then arg will simply be None. This makes it easier to loop through the flags. +
                                                                                                            12. getopt validates that the command-line flags are acceptable, but it doesn't do any sort of conversion between short and long flags. If you specify the -h flag, opt will contain "-h"; if you specify the --help flag, opt will contain "--help". So you need to check for both. -
                                                                                                            13. 4 -Remember, the -d flag didn't have a corresponding long flag, so you only need to check for the short form. If you find it, you set a global +
                                                                                                            14. Remember, the -d flag didn't have a corresponding long flag, so you only need to check for the short form. If you find it, you set a global variable that you'll refer to later to print out debugging information. (I used this during the development of the script. What, you thought all these examples worked on the first try?) -
                                                                                                            15. 5 -If you find a grammar file, either with a -g flag or a --grammar flag, you save the argument that followed it (stored in arg) into the grammar variable, overwriting the default that you initialized at the top of the main function. -
                                                                                                              6 -That's it. You've looped through and dealt with all the command-line flags. That means that anything left must be command-line +
                                                                                                            16. If you find a grammar file, either with a -g flag or a --grammar flag, you save the argument that followed it (stored in arg) into the grammar variable, overwriting the default that you initialized at the top of the main function. +
                                                                                                            17. That's it. You've looped through and dealt with all the command-line flags. That means that anything left must be command-line arguments. These come back from the getopt function in the args variable. In this case, you're treating them as source material for the parser. If there are no command-line arguments specified, args will be an empty list, and source will end up as the empty string. -
                                                                                                            18. 10.7. Putting it all together

                                                                                                              You've covered a lot of ground. Let's step back and see how all the pieces fit together.

                                                                                                              To start with, this is a script that takes its arguments on the command line, using the getopt module. -

                                                                                                              +
                                                                                                              
                                                                                                               def main(argv):       
                                                                                                               ...
                                                                                                                   try:              
                                                                                                              @@ -8379,19 +4931,19 @@ def main(argv):
                                                                                                               ...
                                                                                                                   for opt, arg in opts:               
                                                                                                               ...

                                                                                                              You create a new instance of the KantGenerator class, and pass it the grammar file and source that may or may not have been specified on the command line. -

                                                                                                              +
                                                                                                              
                                                                                                                   k = KantGenerator(grammar, source)

                                                                                                              The KantGenerator instance automatically loads the grammar, which is an XML file. You use your custom openAnything function to open the file (which could be stored in a local file or a remote web server), then use the built-in minidom parsing functions to parse the XML into a tree of Python objects. -

                                                                                                              +
                                                                                                              
                                                                                                                   def _load(self, source):
                                                                                                                       sock = toolbox.openAnything(source)
                                                                                                                       xmldoc = minidom.parse(sock).documentElement
                                                                                                                       sock.close()

                                                                                                              Oh, and along the way, you take advantage of your knowledge of the structure of the XML document to set up a little cache of references, which are just elements in the XML document. -

                                                                                                              +
                                                                                                              
                                                                                                                   def loadGrammar(self, grammar):       
                                                                                                                       for ref in self.grammar.getElementsByTagName("ref"):
                                                                                                                           self.refs[ref.attributes["id"].value] = ref     

                                                                                                              If you specified some source material on the command line, you use that; otherwise you rip through the grammar looking for the "top-level" reference (that isn't referenced by anything else) and use that as a starting point. -

                                                                                                              +
                                                                                                              
                                                                                                                   def getDefaultSource(self):
                                                                                                                       xrefs = {}
                                                                                                                       for xref in self.grammar.getElementsByTagName("xref"):
                                                                                                              @@ -8399,28 +4951,28 @@ the "top-level" reference (that isn't referenced by anything else) and use that
                                                                                                                       xrefs = xrefs.keys()
                                                                                                                       standaloneXrefs = [e for e in self.refs.keys() if e not in xrefs]
                                                                                                                       return '<xref id="%s"/>' % random.choice(standaloneXrefs)

                                                                                                              Now you rip through the source material. The source material is also XML, and you parse it one node at a time. To keep the code separated and more maintainable, you use separate handlers for each node type. -

                                                                                                              +
                                                                                                              
                                                                                                                   def parse_Element(self, node): 
                                                                                                                       handlerMethod = getattr(self, "do_%s" % node.tagName)
                                                                                                                       handlerMethod(node)

                                                                                                              You bounce through the grammar, parsing all the children of each p element, -

                                                                                                              +
                                                                                                              
                                                                                                                   def do_p(self, node):
                                                                                                               ...
                                                                                                                       if doit:
                                                                                                                           for child in node.childNodes: self.parse(child)

                                                                                                              replacing choice elements with a random child, -

                                                                                                              +
                                                                                                              
                                                                                                                   def do_choice(self, node):
                                                                                                                       self.parse(self.randomChildElement(node))

                                                                                                              and replacing xref elements with a random child of the corresponding ref element, which you previously cached. -

                                                                                                              +
                                                                                                              
                                                                                                                   def do_xref(self, node):
                                                                                                                       id = node.attributes["id"].value
                                                                                                                       self.parse(self.randomChildElement(self.refs[id]))

                                                                                                              Eventually, you parse your way down to plain text, -

                                                                                                              +
                                                                                                              
                                                                                                                   def parse_Text(self, node):    
                                                                                                                       text = node.data
                                                                                                               ...
                                                                                                                           self.pieces.append(text)

                                                                                                              which you print out. -

                                                                                                              +
                                                                                                              
                                                                                                               def main(argv):       
                                                                                                               ...
                                                                                                                   k = KantGenerator(grammar, source)
                                                                                                              @@ -8428,7 +4980,7 @@ def main(argv):
                                                                                                               

                                                                                                              Python comes with powerful libraries for parsing and manipulating XML documents. The minidom takes an XML file and parses it into Python objects, providing for random access to arbitrary elements. Furthermore, this chapter shows how Python can be used to create a "real" standalone command-line script, complete with command-line flags, command-line arguments, error handling, even the ability to take input from the piped result of a previous program.

                                                                                                              Before moving on to the next chapter, you should be comfortable doing all of these things: -

                                                                                                              +
                                                                                                              -
                                                                                                              +

                                                                                                              Chapter 11. HTTP Web Services

                                                                                                              11.1. Diving in

                                                                                                              You've learned about HTML processing and XML processing, and along the way you saw how to download a web page and how to parse XML from a URL, but let's dive into the more general topic of HTTP web services. @@ -8451,7 +5003,7 @@ languages include an HTTP library for downloading it. Debugging is also easier, any web browser and see the raw data. Modern browsers will even nicely format and pretty-print XML data for you, to allow you to quickly navigate through it.

                                                                                                              Examples of pure XML-over-HTTP web services: -

                                                                                                              +
                                                                                                              • Amazon API allows you to retrieve product information from the Amazon.com online store. @@ -8467,8 +5019,9 @@ semantics to the underlying HTTP semantics. (They tunnel everything over HTTP PO using HTTP GET to get data from a remote server, and you'll explore several HTTP features you can use to get the maximum benefit out of pure HTTP web services.

                                                                                                                Here is a more advanced version of the openanything module that you saw in the previous chapter: -

                                                                                                                Example 11.1. openanything.py

                                                                                                                -

                                                                                                                If you have not already done so, you can download this and other examples used in this book.

                                                                                                                +

                                                                                                                Example 11.1. openanything.py

                                                                                                                +

                                                                                                                If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                
                                                                                                                 import urllib2, urlparse, gzip
                                                                                                                 from StringIO import StringIO
                                                                                                                 
                                                                                                                @@ -8562,7 +5115,7 @@ def fetch(source, etag=None, last_modified=None, agent=USER_AGENT):
                                                                                                                         result['status'] = f.status                
                                                                                                                     f.close()  
                                                                                                                     return result                
                                                                                                                -
                                                                                                                +

                                                                                                                Further reading

                                                                                                                • Paul Prescod believes that pure HTTP web services are the future of the Internet. @@ -8572,11 +5125,11 @@ def fetch(source, etag=None, last_modified=None, agent=USER_AGENT):

                                                                                                                  Let's say you want to download a resource over HTTP, such as a syndicated Atom feed. But you don't just want to download it once; you want to download it over and over again, every hour, to get the latest news from the site that's offering the news feed. Let's do it the quick-and-dirty way first, and then see how you can do better. -

                                                                                                                  Example 11.2. Downloading a feed the quick-and-dirty way

                                                                                                                  ->>> import urllib
                                                                                                                  ->>> data = urllib.urlopen('http://diveintomark.org/xml/atom.xml').read()    1
                                                                                                                  ->>> print data
                                                                                                                  -<?xml version="1.0" encoding="iso-8859-1"?>
                                                                                                                  +

                                                                                                                  Example 11.2. Downloading a feed the quick-and-dirty way

                                                                                                                  +>>> import urllib
                                                                                                                  +>>> data = urllib.urlopen('http://diveintomark.org/xml/atom.xml').read()    
                                                                                                                  +>>> print data
                                                                                                                  +<?xml version="1.0" encoding="iso-8859-1"?>
                                                                                                                   <feed version="0.3"
                                                                                                                     xmlns="http://purl.org/atom/ns#"
                                                                                                                     xmlns:dc="http://purl.org/dc/elements/1.1/"
                                                                                                                  @@ -8584,15 +5137,9 @@ def fetch(source, etag=None, last_modified=None, agent=USER_AGENT):
                                                                                                                     <title mode="escaped">dive into mark</title>
                                                                                                                     <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
                                                                                                                     <-- rest of feed omitted for brevity -->
                                                                                                                  -
                                                                                                                  - - - - - -
                                                                                                                  1 -Downloading anything over HTTP is incredibly easy in Python; in fact, it's a one-liner. The urllib module has a handy urlopen function that takes the address of the page you want, and returns a file-like object that you can just read() from to get the full contents of the page. It just can't get much easier. -
                                                                                                                  +
                                                                                                                  +
                                                                                                                    +
                                                                                                                  1. Downloading anything over HTTP is incredibly easy in Python; in fact, it's a one-liner. The urllib module has a handy urlopen function that takes the address of the page you want, and returns a file-like object that you can just read() from to get the full contents of the page. It just can't get much easier.

                                                                                                                    So what's wrong with this? Well, for a quick one-off during testing or development, there's nothing wrong with it. I do it all the time. I wanted the contents of the feed, and I got the contents of the feed. The same technique works for any web page. But once you start thinking in terms of a web service that you want to access on a regular basis -- and remember, @@ -8652,94 +5199,53 @@ Python comes with a separate gzip module, which has functions you c

                                                                                                                    11.4. Debugging HTTP web services

                                                                                                                    First, let's turn on the debugging features of Python's HTTP library and see what's being sent over the wire. This will be useful throughout the chapter, as you add more and more features. -

                                                                                                                    Example 11.3. Debugging HTTP

                                                                                                                    ->>> import httplib
                                                                                                                    ->>> httplib.HTTPConnection.debuglevel = 1             1
                                                                                                                    ->>> import urllib
                                                                                                                    ->>> feeddata = urllib.urlopen('http://diveintomark.org/xml/atom.xml').read()
                                                                                                                    -connect: (diveintomark.org, 80)     2
                                                                                                                    +

                                                                                                                    Example 11.3. Debugging HTTP

                                                                                                                    +>>> import httplib
                                                                                                                    +>>> httplib.HTTPConnection.debuglevel = 1             
                                                                                                                    +>>> import urllib
                                                                                                                    +>>> feeddata = urllib.urlopen('http://diveintomark.org/xml/atom.xml').read()
                                                                                                                    +connect: (diveintomark.org, 80)     
                                                                                                                     send: '
                                                                                                                    -GET /xml/atom.xml HTTP/1.0          3
                                                                                                                    -Host: diveintomark.org              4
                                                                                                                    -User-agent: Python-urllib/1.15      5
                                                                                                                    +GET /xml/atom.xml HTTP/1.0          
                                                                                                                    +Host: diveintomark.org              
                                                                                                                    +User-agent: Python-urllib/1.15      
                                                                                                                     '
                                                                                                                    -reply: 'HTTP/1.1 200 OK\r\n'        6
                                                                                                                    +reply: 'HTTP/1.1 200 OK\r\n'        
                                                                                                                     header: Date: Wed, 14 Apr 2004 22:27:30 GMT
                                                                                                                     header: Server: Apache/2.0.49 (Debian GNU/Linux)
                                                                                                                     header: Content-Type: application/atom+xml
                                                                                                                    -header: Last-Modified: Wed, 14 Apr 2004 22:14:38 GMT  7
                                                                                                                    -header: ETag: "e8284-68e0-4de30f80" 8
                                                                                                                    +header: Last-Modified: Wed, 14 Apr 2004 22:14:38 GMT  
                                                                                                                    +header: ETag: "e8284-68e0-4de30f80" 
                                                                                                                     header: Accept-Ranges: bytes
                                                                                                                     header: Content-Length: 26848
                                                                                                                     header: Connection: close
                                                                                                                    -
                                                                                                                    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                    1 -urllib relies on another standard Python library, httplib. Normally you don't need to import httplib directly (urllib does that automatically), but you will here so you can set the debugging flag on the HTTPConnection class that urllib uses internally to connect to the HTTP server. This is an incredibly useful technique. Some other Python libraries have similar debug flags, but there's no particular standard for naming them or turning them on; you need to read +
                                                                                                                    +
                                                                                                                      +
                                                                                                                    1. urllib relies on another standard Python library, httplib. Normally you don't need to import httplib directly (urllib does that automatically), but you will here so you can set the debugging flag on the HTTPConnection class that urllib uses internally to connect to the HTTP server. This is an incredibly useful technique. Some other Python libraries have similar debug flags, but there's no particular standard for naming them or turning them on; you need to read the documentation of each library to see if such a feature is available. -
                                                                                                                    2 -Now that the debugging flag is set, information on the the HTTP request and response is printed out in real time. The first +
                                                                                                                  2. Now that the debugging flag is set, information on the the HTTP request and response is printed out in real time. The first thing it tells you is that you're connecting to the server diveintomark.org on port 80, which is the standard port for HTTP. -
                                                                                                                  3. 3 -When you request the Atom feed, urllib sends three lines to the server. The first line specifies the HTTP verb you're using, and the path of the resource (minus +
                                                                                                                  4. When you request the Atom feed, urllib sends three lines to the server. The first line specifies the HTTP verb you're using, and the path of the resource (minus the domain name). All the requests in this chapter will use GET, but in the next chapter on SOAP, you'll see that it uses POST for everything. The basic syntax is the same, regardless of the verb. -
                                                                                                                  5. 4 -The second line is the Host header, which specifies the domain name of the service you're accessing. This is important, because a single HTTP server +
                                                                                                                  6. The second line is the Host header, which specifies the domain name of the service you're accessing. This is important, because a single HTTP server can host multiple separate domains. My server currently hosts 12 domains; other servers can host hundreds or even thousands. -
                                                                                                                  7. 5 -The third line is the User-Agent header. What you see here is the generic User-Agent that the urllib library adds by default. In the next section, you'll see how to customize this to be more specific. -
                                                                                                                    6 -The server replies with a status code and a bunch of headers (and possibly some data, which got stored in the feeddata variable). The status code here is 200, meaning “everything's normal, here's the data you requested”. The server also tells you the date it responded to your request, some information about the server itself, and the content +
                                                                                                                  8. The third line is the User-Agent header. What you see here is the generic User-Agent that the urllib library adds by default. In the next section, you'll see how to customize this to be more specific. +
                                                                                                                  9. The server replies with a status code and a bunch of headers (and possibly some data, which got stored in the feeddata variable). The status code here is 200, meaning “everything's normal, here's the data you requested”. The server also tells you the date it responded to your request, some information about the server itself, and the content type of the data it's giving you. Depending on your application, this might be useful, or not. It's certainly reassuring that you thought you were asking for an Atom feed, and lo and behold, you're getting an Atom feed (application/atom+xml, which is the registered content type for Atom feeds). -
                                                                                                                  10. 7 -The server tells you when this Atom feed was last modified (in this case, about 13 minutes ago). You can send this date back +
                                                                                                                  11. The server tells you when this Atom feed was last modified (in this case, about 13 minutes ago). You can send this date back to the server the next time you request the same feed, and the server can do last-modified checking. -
                                                                                                                  12. 8 -The server also tells you that this Atom feed has an ETag hash of "e8284-68e0-4de30f80". The hash doesn't mean anything by itself; there's nothing you can do with it, except send it back to the server the next +
                                                                                                                  13. The server also tells you that this Atom feed has an ETag hash of "e8284-68e0-4de30f80". The hash doesn't mean anything by itself; there's nothing you can do with it, except send it back to the server the next time you request this same feed. Then the server can use it to tell you if the data has changed or not. -
                                                                                                                  14. 11.5. Setting the User-Agent

                                                                                                                    The first step to improving your HTTP web services client is to identify yourself properly with a User-Agent. To do that, you need to move beyond the basic urllib and dive into urllib2. -

                                                                                                                    Example 11.4. Introducing urllib2

                                                                                                                    ->>> import httplib
                                                                                                                    ->>> httplib.HTTPConnection.debuglevel = 1           1
                                                                                                                    ->>> import urllib2
                                                                                                                    ->>> request = urllib2.Request('http://diveintomark.org/xml/atom.xml') 2
                                                                                                                    ->>> opener = urllib2.build_opener()                 3
                                                                                                                    ->>> feeddata = opener.open(request).read()          4
                                                                                                                    +

                                                                                                                    Example 11.4. Introducing urllib2

                                                                                                                    +>>> import httplib
                                                                                                                    +>>> httplib.HTTPConnection.debuglevel = 1           
                                                                                                                    +>>> import urllib2
                                                                                                                    +>>> request = urllib2.Request('http://diveintomark.org/xml/atom.xml') 
                                                                                                                    +>>> opener = urllib2.build_opener()                 
                                                                                                                    +>>> feeddata = opener.open(request).read()          
                                                                                                                     connect: (diveintomark.org, 80)
                                                                                                                     send: '
                                                                                                                     GET /xml/atom.xml HTTP/1.0
                                                                                                                    @@ -8755,50 +5261,29 @@ header: ETag: "e8284-68e0-4de30f80"
                                                                                                                     header: Accept-Ranges: bytes
                                                                                                                     header: Content-Length: 26848
                                                                                                                     header: Connection: close
                                                                                                                    -
                                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                                    1 -If you still have your Python IDE open from the previous section's example, you can skip this, but this turns on HTTP debugging so you can see what you're actually sending over the wire, and what gets sent back. -
                                                                                                                    2 -Fetching an HTTP resource with urllib2 is a three-step process, for good reasons that will become clear shortly. The first step is to create a Request object, which takes the URL of the resource you'll eventually get around to retrieving. Note that this step doesn't actually +
                                                                                                                    +
                                                                                                                      +
                                                                                                                    1. If you still have your Python IDE open from the previous section's example, you can skip this, but this turns on HTTP debugging so you can see what you're actually sending over the wire, and what gets sent back. +
                                                                                                                    2. Fetching an HTTP resource with urllib2 is a three-step process, for good reasons that will become clear shortly. The first step is to create a Request object, which takes the URL of the resource you'll eventually get around to retrieving. Note that this step doesn't actually retrieve anything yet. -
                                                                                                                    3 -The second step is to build a URL opener. This can take any number of handlers, which control how responses are handled. +
                                                                                                                  15. The second step is to build a URL opener. This can take any number of handlers, which control how responses are handled. But you can also build an opener without any custom handlers, which is what you're doing here. You'll see how to define and use custom handlers later in this chapter when you explore redirects. -
                                                                                                                  16. 4 -The final step is to tell the opener to open the URL, using the Request object you created. As you can see from all the debugging information that gets printed, this step actually retrieves the +
                                                                                                                  17. The final step is to tell the opener to open the URL, using the Request object you created. As you can see from all the debugging information that gets printed, this step actually retrieves the resource and stores the returned data in feeddata. -
                                                                                                                  18. -

                                                                                                                    Example 11.5. Adding headers with the Request

                                                                                                                    ->>> request            1
                                                                                                                    +

                                                                                                                    Example 11.5. Adding headers with the Request

                                                                                                                    +>>> request            
                                                                                                                     <urllib2.Request instance at 0x00250AA8>
                                                                                                                    ->>> request.get_full_url()
                                                                                                                    +>>> request.get_full_url()
                                                                                                                     http://diveintomark.org/xml/atom.xml
                                                                                                                    ->>> request.add_header('User-Agent',
                                                                                                                    -...    'OpenAnything/1.0 +http://diveintopython3.org/')    2
                                                                                                                    ->>> feeddata = opener.open(request).read()                 3
                                                                                                                    +>>> request.add_header('User-Agent',
                                                                                                                    +...    'OpenAnything/1.0 +http://diveintopython3.org/')    
                                                                                                                    +>>> feeddata = opener.open(request).read()                 
                                                                                                                     connect: (diveintomark.org, 80)
                                                                                                                     send: '
                                                                                                                     GET /xml/atom.xml HTTP/1.0
                                                                                                                     Host: diveintomark.org
                                                                                                                    -User-agent: OpenAnything/1.0 +http://diveintopython3.org/   4
                                                                                                                    +User-agent: OpenAnything/1.0 +http://diveintopython3.org/   
                                                                                                                     '
                                                                                                                     reply: 'HTTP/1.1 200 OK\r\n'
                                                                                                                     header: Date: Wed, 14 Apr 2004 23:45:17 GMT
                                                                                                                    @@ -8809,48 +5294,27 @@ header: ETag: "e8284-68e0-4de30f80"
                                                                                                                     header: Accept-Ranges: bytes
                                                                                                                     header: Content-Length: 26848
                                                                                                                     header: Connection: close
                                                                                                                    -
                                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                                    1 -You're continuing from the previous example; you've already created a Request object with the URL you want to access. -
                                                                                                                    2 -Using the add_header method on the Request object, you can add arbitrary HTTP headers to the request. The first argument is the header, the second is the value you're +
                                                                                                                    +
                                                                                                                      +
                                                                                                                    1. You're continuing from the previous example; you've already created a Request object with the URL you want to access. +
                                                                                                                    2. Using the add_header method on the Request object, you can add arbitrary HTTP headers to the request. The first argument is the header, the second is the value you're providing for that header. Convention dictates that a User-Agent should be in this specific format: an application name, followed by a slash, followed by a version number. The rest is free-form, and you'll see a lot of variations in the wild, but somewhere it should include a URL of your application. The User-Agent is usually logged by the server along with other details of your request, and including a URL of your application allows server administrators looking through their access logs to contact you if something is wrong. -
                                                                                                                    3 -The opener object you created before can be reused too, and it will retrieve the same feed again, but with your custom User-Agent header. -
                                                                                                                    4 -And here's you sending your custom User-Agent, in place of the generic one that Python sends by default. If you look closely, you'll notice that you defined a User-Agent header, but you actually sent a User-agent header. See the difference? urllib2 changed the case so that only the first letter was capitalized. It doesn't really matter; HTTP specifies that header field +
                                                                                                                  19. The opener object you created before can be reused too, and it will retrieve the same feed again, but with your custom User-Agent header. +
                                                                                                                  20. And here's you sending your custom User-Agent, in place of the generic one that Python sends by default. If you look closely, you'll notice that you defined a User-Agent header, but you actually sent a User-agent header. See the difference? urllib2 changed the case so that only the first letter was capitalized. It doesn't really matter; HTTP specifies that header field names are completely case-insensitive. -
                                                                                                                  21. 11.6. Handling Last-Modified and ETag

                                                                                                                    Now that you know how to add custom HTTP headers to your web service requests, let's look at adding support for Last-Modified and ETag headers.

                                                                                                                    These examples show the output with debugging turned off. If you still have it turned on from the previous section, you can turn it off by setting httplib.HTTPConnection.debuglevel = 0. Or you can just leave debugging on, if that helps you. -

                                                                                                                    Example 11.6. Testing Last-Modified

                                                                                                                    ->>> import urllib2
                                                                                                                    ->>> request = urllib2.Request('http://diveintomark.org/xml/atom.xml')
                                                                                                                    ->>> opener = urllib2.build_opener()
                                                                                                                    ->>> firstdatastream = opener.open(request)
                                                                                                                    ->>> firstdatastream.headers.dict     1
                                                                                                                    -{'date': 'Thu, 15 Apr 2004 20:42:41 GMT', 
                                                                                                                    +

                                                                                                                    Example 11.6. Testing Last-Modified

                                                                                                                    +>>> import urllib2
                                                                                                                    +>>> request = urllib2.Request('http://diveintomark.org/xml/atom.xml')
                                                                                                                    +>>> opener = urllib2.build_opener()
                                                                                                                    +>>> firstdatastream = opener.open(request)
                                                                                                                    +>>> firstdatastream.headers.dict     
                                                                                                                    +{'date': 'Thu, 15 Apr 2004 20:42:41 GMT', 
                                                                                                                      'server': 'Apache/2.0.49 (Debian GNU/Linux)', 
                                                                                                                      'content-type': 'application/atom+xml',
                                                                                                                      'last-modified': 'Thu, 15 Apr 2004 19:45:21 GMT', 
                                                                                                                    @@ -8858,10 +5322,10 @@ turn it off by setting httplib.HTTPConnection.debuglevel = 0. Or yo
                                                                                                                      'content-length': '15955', 
                                                                                                                      'accept-ranges': 'bytes', 
                                                                                                                      'connection': 'close'}
                                                                                                                    ->>> request.add_header('If-Modified-Since',
                                                                                                                    -...    firstdatastream.headers.get('Last-Modified'))  2
                                                                                                                    ->>> seconddatastream = opener.open(request)            3
                                                                                                                    -Traceback (most recent call last):
                                                                                                                    +>>> request.add_header('If-Modified-Since',
                                                                                                                    +...    firstdatastream.headers.get('Last-Modified'))  
                                                                                                                    +>>> seconddatastream = opener.open(request)            
                                                                                                                    +Traceback (most recent call last):
                                                                                                                       File "<stdin>", line 1, in ?
                                                                                                                       File "c:\python23\lib\urllib2.py", line 326, in open
                                                                                                                         '_open', req)
                                                                                                                    @@ -8878,115 +5342,63 @@ turn it off by setting httplib.HTTPConnection.debuglevel = 0. Or yo
                                                                                                                       File "c:\python23\lib\urllib2.py", line 412, in http_error_default
                                                                                                                         raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
                                                                                                                     urllib2.HTTPError: HTTP Error 304: Not Modified
                                                                                                                    -
                                                                                                                    - - - - - - - - - - - - - -
                                                                                                                    1 -Remember all those HTTP headers you saw printed out when you turned on debugging? This is how you can get access to them +
                                                                                                                    +
                                                                                                                      +
                                                                                                                    1. Remember all those HTTP headers you saw printed out when you turned on debugging? This is how you can get access to them programmatically: firstdatastream.headers is an object that acts like a dictionary and allows you to get any of the individual headers returned from the HTTP server. -
                                                                                                                    2 -On the second request, you add the If-Modified-Since header with the last-modified date from the first request. If the data hasn't changed, the server should return a 304 status code. -
                                                                                                                    3 -Sure enough, the data hasn't changed. You can see from the traceback that urllib2 throws a special exception, HTTPError, in response to the 304 status code. This is a little unusual, and not entirely helpful. After all, it's not an error; you specifically asked the +
                                                                                                                  22. On the second request, you add the If-Modified-Since header with the last-modified date from the first request. If the data hasn't changed, the server should return a 304 status code. +
                                                                                                                  23. Sure enough, the data hasn't changed. You can see from the traceback that urllib2 throws a special exception, HTTPError, in response to the 304 status code. This is a little unusual, and not entirely helpful. After all, it's not an error; you specifically asked the server not to send you any data if it hadn't changed, and the data didn't change, so the server told you it wasn't sending you any data. That's not an error; that's exactly what you were hoping for. -
                                                                                                                  24. urllib2 also raises an HTTPError exception for conditions that you would think of as errors, such as 404 (page not found). In fact, it will raise HTTPError for any status code other than 200 (OK), 301 (permanent redirect), or 302 (temporary redirect). It would be more helpful for your purposes to capture the status code and simply return it, without throwing an exception. To do that, you'll need to define a custom URL handler. -

                                                                                                                    Example 11.7. Defining URL handlers

                                                                                                                    -

                                                                                                                    This custom URL handler is part of openanything.py.

                                                                                                                    -class DefaultErrorHandler(urllib2.HTTPDefaultErrorHandler):    1
                                                                                                                    -    def http_error_default(self, req, fp, code, msg, headers): 2
                                                                                                                    +

                                                                                                                    Example 11.7. Defining URL handlers

                                                                                                                    +

                                                                                                                    This custom URL handler is part of openanything.py. +

                                                                                                                    
                                                                                                                    +class DefaultErrorHandler(urllib2.HTTPDefaultErrorHandler):    
                                                                                                                    +    def http_error_default(self, req, fp, code, msg, headers): 
                                                                                                                             result = urllib2.HTTPError(         
                                                                                                                                 req.get_full_url(), code, msg, headers, fp)       
                                                                                                                    -        result.status = code                 3
                                                                                                                    +        result.status = code                 
                                                                                                                             return result     
                                                                                                                    -
                                                                                                                    - - - - - - - - - - - - - -
                                                                                                                    1 -urllib2 is designed around URL handlers. Each handler is just a class that can define any number of methods. When something happens +
                                                                                                                    +
                                                                                                                      +
                                                                                                                    1. urllib2 is designed around URL handlers. Each handler is just a class that can define any number of methods. When something happens -- like an HTTP error, or even a 304 code -- urllib2 introspects into the list of defined handlers for a method that can handle it. You used a similar introspection in Chapter 9, XML Processing to define handlers for different node types, but urllib2 is more flexible, and introspects over as many handlers as are defined for the current request. -
                                                                                                                    2 -urllib2 searches through the defined handlers and calls the http_error_default method when it encounters a 304 status code from the server. By defining a custom error handler, you can prevent urllib2 from raising an exception. Instead, you create the HTTPError object, but return it instead of raising it. -
                                                                                                                    3 -This is the key part: before returning, you save the status code returned by the HTTP server. This will allow you easy access +
                                                                                                                  25. urllib2 searches through the defined handlers and calls the http_error_default method when it encounters a 304 status code from the server. By defining a custom error handler, you can prevent urllib2 from raising an exception. Instead, you create the HTTPError object, but return it instead of raising it. +
                                                                                                                  26. This is the key part: before returning, you save the status code returned by the HTTP server. This will allow you easy access to it from the calling program. -
                                                                                                                  27. -

                                                                                                                    Example 11.8. Using custom URL handlers

                                                                                                                    ->>> request.headers         1
                                                                                                                    +

                                                                                                                    Example 11.8. Using custom URL handlers

                                                                                                                    +>>> request.headers         
                                                                                                                     {'If-modified-since': 'Thu, 15 Apr 2004 19:45:21 GMT'}
                                                                                                                    ->>> import openanything
                                                                                                                    ->>> opener = urllib2.build_opener(
                                                                                                                    -...    openanything.DefaultErrorHandler())   2
                                                                                                                    ->>> seconddatastream = opener.open(request)
                                                                                                                    ->>> seconddatastream.status 3
                                                                                                                    +>>> import openanything
                                                                                                                    +>>> opener = urllib2.build_opener(
                                                                                                                    +...    openanything.DefaultErrorHandler())   
                                                                                                                    +>>> seconddatastream = opener.open(request)
                                                                                                                    +>>> seconddatastream.status 
                                                                                                                     304
                                                                                                                    ->>> seconddatastream.read() 4
                                                                                                                    +>>> seconddatastream.read() 
                                                                                                                     ''
                                                                                                                    -
                                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                                    1 -You're continuing the previous example, so the Request object is already set up, and you've already added the If-Modified-Since header. -
                                                                                                                    2 -This is the key: now that you've defined your custom URL handler, you need to tell urllib2 to use it. Remember how I said that urllib2 broke up the process of accessing an HTTP resource into three steps, and for good reason? This is why building the URL opener +
                                                                                                                    +
                                                                                                                      +
                                                                                                                    1. You're continuing the previous example, so the Request object is already set up, and you've already added the If-Modified-Since header. +
                                                                                                                    2. This is the key: now that you've defined your custom URL handler, you need to tell urllib2 to use it. Remember how I said that urllib2 broke up the process of accessing an HTTP resource into three steps, and for good reason? This is why building the URL opener is its own step, because you can build it with your own custom URL handlers that override urllib2's default behavior. -
                                                                                                                    3 -Now you can quietly open the resource, and what you get back is an object that, along with the usual headers (use seconddatastream.headers.dict to acess them), also contains the HTTP status code. In this case, as you expected, the status is 304, meaning this data hasn't changed since the last time you asked for it. -
                                                                                                                    4 -Note that when the server sends back a 304 status code, it doesn't re-send the data. That's the whole point: to save bandwidth by not re-downloading data that hasn't +
                                                                                                                  28. Now you can quietly open the resource, and what you get back is an object that, along with the usual headers (use seconddatastream.headers.dict to acess them), also contains the HTTP status code. In this case, as you expected, the status is 304, meaning this data hasn't changed since the last time you asked for it. +
                                                                                                                  29. Note that when the server sends back a 304 status code, it doesn't re-send the data. That's the whole point: to save bandwidth by not re-downloading data that hasn't changed. So if you actually want that data, you'll need to cache it locally the first time you get it. -
                                                                                                                  30. Handling ETag works much the same way, but instead of checking for Last-Modified and sending If-Modified-Since, you check for ETag and send If-None-Match. Let's start with a fresh IDE session. -

                                                                                                                    Example 11.9. Supporting ETag/If-None-Match

                                                                                                                    ->>> import urllib2, openanything
                                                                                                                    ->>> request = urllib2.Request('http://diveintomark.org/xml/atom.xml')
                                                                                                                    ->>> opener = urllib2.build_opener(
                                                                                                                    -...    openanything.DefaultErrorHandler())
                                                                                                                    ->>> firstdatastream = opener.open(request)
                                                                                                                    ->>> firstdatastream.headers.get('ETag')        1
                                                                                                                    +

                                                                                                                    Example 11.9. Supporting ETag/If-None-Match

                                                                                                                    +>>> import urllib2, openanything
                                                                                                                    +>>> request = urllib2.Request('http://diveintomark.org/xml/atom.xml')
                                                                                                                    +>>> opener = urllib2.build_opener(
                                                                                                                    +...    openanything.DefaultErrorHandler())
                                                                                                                    +>>> firstdatastream = opener.open(request)
                                                                                                                    +>>> firstdatastream.headers.get('ETag')        
                                                                                                                     '"e842a-3e53-55d97640"'
                                                                                                                    ->>> firstdata = firstdatastream.read()
                                                                                                                    ->>> print firstdata          2
                                                                                                                    -<?xml version="1.0" encoding="iso-8859-1"?>
                                                                                                                    +>>> firstdata = firstdatastream.read()
                                                                                                                    +>>> print firstdata          
                                                                                                                    +<?xml version="1.0" encoding="iso-8859-1"?>
                                                                                                                     <feed version="0.3"
                                                                                                                       xmlns="http://purl.org/atom/ns#"
                                                                                                                       xmlns:dc="http://purl.org/dc/elements/1.1/"
                                                                                                                    @@ -8994,82 +5406,51 @@ class DefaultErrorHandler(urllib2.HTTPDefaultErrorHandler):    dive into mark</title>
                                                                                                                       <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
                                                                                                                       <-- rest of feed omitted for brevity -->
                                                                                                                    ->>> request.add_header('If-None-Match',
                                                                                                                    -...    firstdatastream.headers.get('ETag'))   3
                                                                                                                    ->>> seconddatastream = opener.open(request)
                                                                                                                    ->>> seconddatastream.status  4
                                                                                                                    +>>> request.add_header('If-None-Match',
                                                                                                                    +...    firstdatastream.headers.get('ETag'))   
                                                                                                                    +>>> seconddatastream = opener.open(request)
                                                                                                                    +>>> seconddatastream.status  
                                                                                                                     304
                                                                                                                    ->>> seconddatastream.read()  5
                                                                                                                    +>>> seconddatastream.read()  
                                                                                                                     ''
                                                                                                                    -
                                                                                                                    - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                    1 -Using the firstdatastream.headers pseudo-dictionary, you can get the ETag returned from the server. (What happens if the server didn't send back an ETag? Then this line would return None.) -
                                                                                                                    2 -OK, you got the data.
                                                                                                                    3 -Now set up the second call by setting the If-None-Match header to the ETag you got from the first call. -
                                                                                                                    4 -The second call succeeds quietly (without throwing an exception), and once again you see that the server has sent back a 304 status code. Based on the ETag you sent the second time, it knows that the data hasn't changed. -
                                                                                                                    5 -Regardless of whether the 304 is triggered by Last-Modified date checking or ETag hash matching, you'll never get the data along with the 304. That's the whole point. -
                                                                                                                    -
                                                                                                                    - - - - -
                                                                                                                    Note
                                                                                                                    In these examples, the HTTP server has supported both Last-Modified and ETag headers, but not all servers do. As a web services client, you should be prepared to support both, but you must code defensively +
                                                                                                                    +
                                                                                                                      +
                                                                                                                    1. Using the firstdatastream.headers pseudo-dictionary, you can get the ETag returned from the server. (What happens if the server didn't send back an ETag? Then this line would return None.) +
                                                                                                                    2. OK, you got the data. +
                                                                                                                    3. Now set up the second call by setting the If-None-Match header to the ETag you got from the first call. +
                                                                                                                    4. The second call succeeds quietly (without throwing an exception), and once again you see that the server has sent back a 304 status code. Based on the ETag you sent the second time, it knows that the data hasn't changed. +
                                                                                                                    5. Regardless of whether the 304 is triggered by Last-Modified date checking or ETag hash matching, you'll never get the data along with the 304. That's the whole point. + + + - -
                                                                                                                      NoteIn these examples, the HTTP server has supported both Last-Modified and ETag headers, but not all servers do. As a web services client, you should be prepared to support both, but you must code defensively in case a server only supports one or the other, or neither. -

                                                                                                                      11.7. Handling redirects

                                                                                                                      You can support permanent and temporary redirects using a different kind of custom URL handler.

                                                                                                                      First, let's see why a redirect handler is necessary in the first place. -

                                                                                                                      Example 11.10. Accessing web services without a redirect handler

                                                                                                                      ->>> import urllib2, httplib
                                                                                                                      ->>> httplib.HTTPConnection.debuglevel = 1           1
                                                                                                                      ->>> request = urllib2.Request(
                                                                                                                      -...    'http://diveintomark.org/redir/example301.xml') 2
                                                                                                                      ->>> opener = urllib2.build_opener()
                                                                                                                      ->>> f = opener.open(request)
                                                                                                                      -connect: (diveintomark.org, 80)
                                                                                                                      +

                                                                                                                      Example 11.10. Accessing web services without a redirect handler

                                                                                                                      +>>> import urllib2, httplib
                                                                                                                      +>>> httplib.HTTPConnection.debuglevel = 1           
                                                                                                                      +>>> request = urllib2.Request(
                                                                                                                      +...    'http://diveintomark.org/redir/example301.xml') 
                                                                                                                      +>>> opener = urllib2.build_opener()
                                                                                                                      +>>> f = opener.open(request)
                                                                                                                      +connect: (diveintomark.org, 80)
                                                                                                                       send: '
                                                                                                                       GET /redir/example301.xml HTTP/1.0
                                                                                                                       Host: diveintomark.org
                                                                                                                       User-agent: Python-urllib/2.1
                                                                                                                       '
                                                                                                                      -reply: 'HTTP/1.1 301 Moved Permanently\r\n'             3
                                                                                                                      -header: Date: Thu, 15 Apr 2004 22:06:25 GMT
                                                                                                                      +reply: 'HTTP/1.1 301 Moved Permanently\r\n'             
                                                                                                                      +header: Date: Thu, 15 Apr 2004 22:06:25 GMT
                                                                                                                       header: Server: Apache/2.0.49 (Debian GNU/Linux)
                                                                                                                      -header: Location: http://diveintomark.org/xml/atom.xml  4
                                                                                                                      -header: Content-Length: 338
                                                                                                                      +header: Location: http://diveintomark.org/xml/atom.xml  
                                                                                                                      +header: Content-Length: 338
                                                                                                                       header: Connection: close
                                                                                                                       header: Content-Type: text/html; charset=iso-8859-1
                                                                                                                       connect: (diveintomark.org, 80)
                                                                                                                       send: '
                                                                                                                      -GET /xml/atom.xml HTTP/1.0            5
                                                                                                                      -Host: diveintomark.org
                                                                                                                      +GET /xml/atom.xml HTTP/1.0            
                                                                                                                      +Host: diveintomark.org
                                                                                                                       User-agent: Python-urllib/2.1
                                                                                                                       '
                                                                                                                       reply: 'HTTP/1.1 200 OK\r\n'
                                                                                                                      @@ -9081,10 +5462,10 @@ header: Accept-Ranges: bytes
                                                                                                                       header: Content-Length: 15955
                                                                                                                       header: Connection: close
                                                                                                                       header: Content-Type: application/atom+xml
                                                                                                                      ->>> f.url           6
                                                                                                                      +>>> f.url           
                                                                                                                       'http://diveintomark.org/xml/atom.xml'
                                                                                                                      ->>> f.headers.dict
                                                                                                                      -{'content-length': '15955', 
                                                                                                                      +>>> f.headers.dict
                                                                                                                      +{'content-length': '15955', 
                                                                                                                       'accept-ranges': 'bytes', 
                                                                                                                       'server': 'Apache/2.0.49 (Debian GNU/Linux)', 
                                                                                                                       'last-modified': 'Thu, 15 Apr 2004 19:45:21 GMT', 
                                                                                                                      @@ -9092,110 +5473,60 @@ header: Content-Type: application/atom+xml
                                                                                                                       'etag': '"e842a-3e53-55d97640"', 
                                                                                                                       'date': 'Thu, 15 Apr 2004 22:06:25 GMT', 
                                                                                                                       'content-type': 'application/atom+xml'}
                                                                                                                      ->>> f.status
                                                                                                                      -Traceback (most recent call last):
                                                                                                                      +>>> f.status
                                                                                                                      +Traceback (most recent call last):
                                                                                                                         File "<stdin>", line 1, in ?
                                                                                                                       AttributeError: addinfourl instance has no attribute 'status'
                                                                                                                      -
                                                                                                                      - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                      1 -You'll be better able to see what's happening if you turn on debugging.
                                                                                                                      2 -This is a URL which I have set up to permanently redirect to my Atom feed at http://diveintomark.org/xml/atom.xml. -
                                                                                                                      3 -Sure enough, when you try to download the data at that address, the server sends back a 301 status code, telling you that the resource has moved permanently. -
                                                                                                                      4 -The server also sends back a Location: header that gives the new address of this data. -
                                                                                                                      5 -urllib2 notices the redirect status code and automatically tries to retrieve the data at the new location specified in the Location: header. -
                                                                                                                      6 -The object you get back from the opener contains the new permanent address and all the headers returned from the second request (retrieved from the new permanent +
                                                                                                                      +
                                                                                                                        +
                                                                                                                      1. You'll be better able to see what's happening if you turn on debugging. +
                                                                                                                      2. This is a URL which I have set up to permanently redirect to my Atom feed at http://diveintomark.org/xml/atom.xml. +
                                                                                                                      3. Sure enough, when you try to download the data at that address, the server sends back a 301 status code, telling you that the resource has moved permanently. +
                                                                                                                      4. The server also sends back a Location: header that gives the new address of this data. +
                                                                                                                      5. urllib2 notices the redirect status code and automatically tries to retrieve the data at the new location specified in the Location: header. +
                                                                                                                      6. The object you get back from the opener contains the new permanent address and all the headers returned from the second request (retrieved from the new permanent address). But the status code is missing, so you have no way of knowing programmatically whether this redirect was temporary or permanent. And that matters very much: if it was a temporary redirect, then you should continue to ask for the data at the old location. But if it was a permanent redirect (as this was), you should ask for the data at the new location from now on. -

                                                                                                                      This is suboptimal, but easy to fix. urllib2 doesn't behave exactly as you want it to when it encounters a 301 or 302, so let's override its behavior. How? With a custom URL handler, just like you did to handle 304 codes. -

                                                                                                                      Example 11.11. Defining the redirect handler

                                                                                                                      -

                                                                                                                      This class is defined in openanything.py.

                                                                                                                      -class SmartRedirectHandler(urllib2.HTTPRedirectHandler):     1
                                                                                                                      +

                                                                                                                      Example 11.11. Defining the redirect handler

                                                                                                                      +

                                                                                                                      This class is defined in openanything.py. +

                                                                                                                      
                                                                                                                      +class SmartRedirectHandler(urllib2.HTTPRedirectHandler):     
                                                                                                                           def http_error_301(self, req, fp, code, msg, headers):  
                                                                                                                      -        result = urllib2.HTTPRedirectHandler.http_error_301( 2
                                                                                                                      +        result = urllib2.HTTPRedirectHandler.http_error_301( 
                                                                                                                                   self, req, fp, code, msg, headers)              
                                                                                                                      -        result.status = code               3
                                                                                                                      +        result.status = code               
                                                                                                                               return result   
                                                                                                                       
                                                                                                                      -    def http_error_302(self, req, fp, code, msg, headers):   4
                                                                                                                      +    def http_error_302(self, req, fp, code, msg, headers):   
                                                                                                                               result = urllib2.HTTPRedirectHandler.http_error_302(
                                                                                                                                   self, req, fp, code, msg, headers)              
                                                                                                                               result.status = code              
                                                                                                                               return result   
                                                                                                                      -
                                                                                                                      - - - - - - - - - - - - - - - - - -
                                                                                                                      1 -Redirect behavior is defined in urllib2 in a class called HTTPRedirectHandler. You don't want to completely override the behavior, you just want to extend it a little, so you'll subclass HTTPRedirectHandler so you can call the ancestor class to do all the hard work. -
                                                                                                                      2 -When it encounters a 301 status code from the server, urllib2 will search through its handlers and call the http_error_301 method. The first thing ours does is just call the http_error_301 method in the ancestor, which handles the grunt work of looking for the Location: header and following the redirect to the new address. -
                                                                                                                      3 -Here's the key: before you return, you store the status code (301), so that the calling program can access it later. -
                                                                                                                      4 -Temporary redirects (status code 302) work the same way: override the http_error_302 method, call the ancestor, and save the status code before returning. -
                                                                                                                      +
                                                                                                                      +
                                                                                                                        +
                                                                                                                      1. Redirect behavior is defined in urllib2 in a class called HTTPRedirectHandler. You don't want to completely override the behavior, you just want to extend it a little, so you'll subclass HTTPRedirectHandler so you can call the ancestor class to do all the hard work. +
                                                                                                                      2. When it encounters a 301 status code from the server, urllib2 will search through its handlers and call the http_error_301 method. The first thing ours does is just call the http_error_301 method in the ancestor, which handles the grunt work of looking for the Location: header and following the redirect to the new address. +
                                                                                                                      3. Here's the key: before you return, you store the status code (301), so that the calling program can access it later. +
                                                                                                                      4. Temporary redirects (status code 302) work the same way: override the http_error_302 method, call the ancestor, and save the status code before returning.

                                                                                                                        So what has this bought us? You can now build a URL opener with the custom redirect handler, and it will still automatically follow redirects, but now it will also expose the redirect status code. -

                                                                                                                        Example 11.12. Using the redirect handler to detect permanent redirects

                                                                                                                        ->>> request = urllib2.Request('http://diveintomark.org/redir/example301.xml')
                                                                                                                        ->>> import openanything, httplib
                                                                                                                        ->>> httplib.HTTPConnection.debuglevel = 1
                                                                                                                        ->>> opener = urllib2.build_opener(
                                                                                                                        -...    openanything.SmartRedirectHandler())           1
                                                                                                                        ->>> f = opener.open(request)
                                                                                                                        -connect: (diveintomark.org, 80)
                                                                                                                        +

                                                                                                                        Example 11.12. Using the redirect handler to detect permanent redirects

                                                                                                                        +>>> request = urllib2.Request('http://diveintomark.org/redir/example301.xml')
                                                                                                                        +>>> import openanything, httplib
                                                                                                                        +>>> httplib.HTTPConnection.debuglevel = 1
                                                                                                                        +>>> opener = urllib2.build_opener(
                                                                                                                        +...    openanything.SmartRedirectHandler())           
                                                                                                                        +>>> f = opener.open(request)
                                                                                                                        +connect: (diveintomark.org, 80)
                                                                                                                         send: 'GET /redir/example301.xml HTTP/1.0
                                                                                                                         Host: diveintomark.org
                                                                                                                         User-agent: Python-urllib/2.1
                                                                                                                         '
                                                                                                                        -reply: 'HTTP/1.1 301 Moved Permanently\r\n'            2
                                                                                                                        -header: Date: Thu, 15 Apr 2004 22:13:21 GMT
                                                                                                                        +reply: 'HTTP/1.1 301 Moved Permanently\r\n'            
                                                                                                                        +header: Date: Thu, 15 Apr 2004 22:13:21 GMT
                                                                                                                         header: Server: Apache/2.0.49 (Debian GNU/Linux)
                                                                                                                         header: Location: http://diveintomark.org/xml/atom.xml
                                                                                                                         header: Content-Length: 338
                                                                                                                        @@ -9217,46 +5548,31 @@ header: Content-Length: 15955
                                                                                                                         header: Connection: close
                                                                                                                         header: Content-Type: application/atom+xml
                                                                                                                         
                                                                                                                        ->>> f.status       3
                                                                                                                        +>>> f.status       
                                                                                                                         301
                                                                                                                        ->>> f.url
                                                                                                                        +>>> f.url
                                                                                                                         'http://diveintomark.org/xml/atom.xml'
                                                                                                                        -
                                                                                                                        - - - - - - - - - - - - - -
                                                                                                                        1 -First, build a URL opener with the redirect handler you just defined.
                                                                                                                        2 -You sent off a request, and you got a 301 status code in response. At this point, the http_error_301 method gets called. You call the ancestor method, which follows the redirect and sends a request at the new location (http://diveintomark.org/xml/atom.xml). -
                                                                                                                        3 -This is the payoff: now, not only do you have access to the new URL, but you have access to the redirect status code, so you +
                                                                                                                        +
                                                                                                                          +
                                                                                                                        1. First, build a URL opener with the redirect handler you just defined. +
                                                                                                                        2. You sent off a request, and you got a 301 status code in response. At this point, the http_error_301 method gets called. You call the ancestor method, which follows the redirect and sends a request at the new location (http://diveintomark.org/xml/atom.xml). +
                                                                                                                        3. This is the payoff: now, not only do you have access to the new URL, but you have access to the redirect status code, so you can tell that this was a permanent redirect. The next time you request this data, you should request it from the new location (http://diveintomark.org/xml/atom.xml, as specified in f.url). If you had stored the location in a configuration file or a database, you need to update that so you don't keep pounding the server with requests at the old address. It's time to update your address book. -

                                                                                                                        The same redirect handler can also tell you that you shouldn't update your address book. -

                                                                                                                        Example 11.13. Using the redirect handler to detect temporary redirects

                                                                                                                        ->>> request = urllib2.Request(
                                                                                                                        -...    'http://diveintomark.org/redir/example302.xml')   1
                                                                                                                        ->>> f = opener.open(request)
                                                                                                                        -connect: (diveintomark.org, 80)
                                                                                                                        +

                                                                                                                        Example 11.13. Using the redirect handler to detect temporary redirects

                                                                                                                        +>>> request = urllib2.Request(
                                                                                                                        +...    'http://diveintomark.org/redir/example302.xml')   
                                                                                                                        +>>> f = opener.open(request)
                                                                                                                        +connect: (diveintomark.org, 80)
                                                                                                                         send: '
                                                                                                                         GET /redir/example302.xml HTTP/1.0
                                                                                                                         Host: diveintomark.org
                                                                                                                         User-agent: Python-urllib/2.1
                                                                                                                         '
                                                                                                                        -reply: 'HTTP/1.1 302 Found\r\n'         2
                                                                                                                        -header: Date: Thu, 15 Apr 2004 22:18:21 GMT
                                                                                                                        +reply: 'HTTP/1.1 302 Found\r\n'         
                                                                                                                        +header: Date: Thu, 15 Apr 2004 22:18:21 GMT
                                                                                                                         header: Server: Apache/2.0.49 (Debian GNU/Linux)
                                                                                                                         header: Location: http://diveintomark.org/xml/atom.xml
                                                                                                                         header: Content-Length: 314
                                                                                                                        @@ -9264,8 +5580,8 @@ header: Connection: close
                                                                                                                         header: Content-Type: text/html; charset=iso-8859-1
                                                                                                                         connect: (diveintomark.org, 80)
                                                                                                                         send: '
                                                                                                                        -GET /xml/atom.xml HTTP/1.0              3
                                                                                                                        -Host: diveintomark.org
                                                                                                                        +GET /xml/atom.xml HTTP/1.0              
                                                                                                                        +Host: diveintomark.org
                                                                                                                         User-agent: Python-urllib/2.1
                                                                                                                         '
                                                                                                                         reply: 'HTTP/1.1 200 OK\r\n'
                                                                                                                        @@ -9277,59 +5593,38 @@ header: Accept-Ranges: bytes
                                                                                                                         header: Content-Length: 15955
                                                                                                                         header: Connection: close
                                                                                                                         header: Content-Type: application/atom+xml
                                                                                                                        ->>> f.status          4
                                                                                                                        +>>> f.status          
                                                                                                                         302
                                                                                                                        ->>> f.url
                                                                                                                        +>>> f.url
                                                                                                                         http://diveintomark.org/xml/atom.xml
                                                                                                                        -
                                                                                                                        - - - - - - - - - - - - - - - - - -
                                                                                                                        1 -This is a sample URL I've set up that is configured to tell clients to temporarily redirect to http://diveintomark.org/xml/atom.xml. -
                                                                                                                        2 -The server sends back a 302 status code, indicating a temporary redirect. The temporary new location of the data is given in the Location: header. -
                                                                                                                        3 -urllib2 calls your http_error_302 method, which calls the ancestor method of the same name in urllib2.HTTPRedirectHandler, which follows the redirect to the new location. Then your http_error_302 method stores the status code (302) so the calling application can get it later. -
                                                                                                                        4 -And here you are, having successfully followed the redirect to http://diveintomark.org/xml/atom.xml. f.status tells you that this was a temporary redirect, which means that you should continue to request data from the original address +
                                                                                                                        +
                                                                                                                          +
                                                                                                                        1. This is a sample URL I've set up that is configured to tell clients to temporarily redirect to http://diveintomark.org/xml/atom.xml. +
                                                                                                                        2. The server sends back a 302 status code, indicating a temporary redirect. The temporary new location of the data is given in the Location: header. +
                                                                                                                        3. urllib2 calls your http_error_302 method, which calls the ancestor method of the same name in urllib2.HTTPRedirectHandler, which follows the redirect to the new location. Then your http_error_302 method stores the status code (302) so the calling application can get it later. +
                                                                                                                        4. And here you are, having successfully followed the redirect to http://diveintomark.org/xml/atom.xml. f.status tells you that this was a temporary redirect, which means that you should continue to request data from the original address (http://diveintomark.org/redir/example302.xml). Maybe it will redirect next time too, but maybe not. Maybe it will redirect to a different address. It's not for you to say. The server said this redirect was only temporary, so you should respect that. And now you're exposing enough information that the calling application can respect that. -

                                                                                                                        11.8. Handling compressed data

                                                                                                                        The last important HTTP feature you want to support is compression. Many web services have the ability to send data compressed, which can cut down the amount of data sent over the wire by 60% or more. This is especially true of XML web services, since XML data compresses very well.

                                                                                                                        Servers won't give you compressed data unless you tell them you can handle it. -

                                                                                                                        Example 11.14. Telling the server you would like compressed data

                                                                                                                        ->>> import urllib2, httplib
                                                                                                                        ->>> httplib.HTTPConnection.debuglevel = 1
                                                                                                                        ->>> request = urllib2.Request('http://diveintomark.org/xml/atom.xml')
                                                                                                                        ->>> request.add_header('Accept-encoding', 'gzip')        1
                                                                                                                        ->>> opener = urllib2.build_opener()
                                                                                                                        ->>> f = opener.open(request)
                                                                                                                        -connect: (diveintomark.org, 80)
                                                                                                                        +

                                                                                                                        Example 11.14. Telling the server you would like compressed data

                                                                                                                        +>>> import urllib2, httplib
                                                                                                                        +>>> httplib.HTTPConnection.debuglevel = 1
                                                                                                                        +>>> request = urllib2.Request('http://diveintomark.org/xml/atom.xml')
                                                                                                                        +>>> request.add_header('Accept-encoding', 'gzip')        
                                                                                                                        +>>> opener = urllib2.build_opener()
                                                                                                                        +>>> f = opener.open(request)
                                                                                                                        +connect: (diveintomark.org, 80)
                                                                                                                         send: '
                                                                                                                         GET /xml/atom.xml HTTP/1.0
                                                                                                                         Host: diveintomark.org
                                                                                                                         User-agent: Python-urllib/2.1
                                                                                                                        -Accept-encoding: gzip2
                                                                                                                        -'
                                                                                                                        +Accept-encoding: gzip
                                                                                                                        +'
                                                                                                                         reply: 'HTTP/1.1 200 OK\r\n'
                                                                                                                         header: Date: Thu, 15 Apr 2004 22:24:39 GMT
                                                                                                                         header: Server: Apache/2.0.49 (Debian GNU/Linux)
                                                                                                                        @@ -9337,48 +5632,28 @@ header: Last-Modified: Thu, 15 Apr 2004 19:45:21 GMT
                                                                                                                         header: ETag: "e842a-3e53-55d97640"
                                                                                                                         header: Accept-Ranges: bytes
                                                                                                                         header: Vary: Accept-Encoding
                                                                                                                        -header: Content-Encoding: gzip         3
                                                                                                                        -header: Content-Length: 6289           4
                                                                                                                        -header: Connection: close
                                                                                                                        +header: Content-Encoding: gzip         
                                                                                                                        +header: Content-Length: 6289           
                                                                                                                        +header: Connection: close
                                                                                                                         header: Content-Type: application/atom+xml
                                                                                                                        -
                                                                                                                        - - - - - - - - - - - - - - - - - -
                                                                                                                        1 -This is the key: once you've created your Request object, add an Accept-encoding header to tell the server you can accept gzip-encoded data. gzip is the name of the compression algorithm you're using. In theory there could be other compression algorithms, but gzip is the compression algorithm used by 99% of web servers. -
                                                                                                                        2 -There's your header going across the wire.
                                                                                                                        3 -And here's what the server sends back: the Content-Encoding: gzip header means that the data you're about to receive has been gzip-compressed. -
                                                                                                                        4 -The Content-Length header is the length of the compressed data, not the uncompressed data. As you'll see in a minute, the actual length of +
                                                                                                                        +
                                                                                                                          +
                                                                                                                        1. This is the key: once you've created your Request object, add an Accept-encoding header to tell the server you can accept gzip-encoded data. gzip is the name of the compression algorithm you're using. In theory there could be other compression algorithms, but gzip is the compression algorithm used by 99% of web servers. +
                                                                                                                        2. There's your header going across the wire. +
                                                                                                                        3. And here's what the server sends back: the Content-Encoding: gzip header means that the data you're about to receive has been gzip-compressed. +
                                                                                                                        4. The Content-Length header is the length of the compressed data, not the uncompressed data. As you'll see in a minute, the actual length of the uncompressed data was 15955, so gzip compression cut your bandwidth by over 60%! -
                                                                                                                        -

                                                                                                                        Example 11.15. Decompressing the data

                                                                                                                        ->>> compresseddata = f.read()            1
                                                                                                                        ->>> len(compresseddata)
                                                                                                                        +

                                                                                                                        Example 11.15. Decompressing the data

                                                                                                                        +>>> compresseddata = f.read()            
                                                                                                                        +>>> len(compresseddata)
                                                                                                                         6289
                                                                                                                        ->>> import StringIO
                                                                                                                        ->>> compressedstream = StringIO.StringIO(compresseddata)   2
                                                                                                                        ->>> import gzip
                                                                                                                        ->>> gzipper = gzip.GzipFile(fileobj=compressedstream)      3
                                                                                                                        ->>> data = gzipper.read()                4
                                                                                                                        ->>> print data         5
                                                                                                                        -<?xml version="1.0" encoding="iso-8859-1"?>
                                                                                                                        +>>> import StringIO
                                                                                                                        +>>> compressedstream = StringIO.StringIO(compresseddata)   
                                                                                                                        +>>> import gzip
                                                                                                                        +>>> gzipper = gzip.GzipFile(fileobj=compressedstream)      
                                                                                                                        +>>> data = gzipper.read()                
                                                                                                                        +>>> print data         
                                                                                                                        +<?xml version="1.0" encoding="iso-8859-1"?>
                                                                                                                         <feed version="0.3"
                                                                                                                           xmlns="http://purl.org/atom/ns#"
                                                                                                                           xmlns:dc="http://purl.org/dc/elements/1.1/"
                                                                                                                        @@ -9386,268 +5661,135 @@ header: Content-Type: application/atom+xml
                                                                                                                           <title mode="escaped">dive into mark</title>
                                                                                                                           <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
                                                                                                                           <-- rest of feed omitted for brevity -->
                                                                                                                        ->>> len(data)
                                                                                                                        +>>> len(data)
                                                                                                                         15955
                                                                                                                        -
                                                                                                                        - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                        1 -Continuing from the previous example, f is the file-like object returned from the URL opener. Using its read() method would ordinarily get you the uncompressed data, but since this data has been gzip-compressed, this is just the first +
                                                                                                                        +
                                                                                                                          +
                                                                                                                        1. Continuing from the previous example, f is the file-like object returned from the URL opener. Using its read() method would ordinarily get you the uncompressed data, but since this data has been gzip-compressed, this is just the first step towards getting the data you really want. -
                                                                                                                        2 -OK, this step is a little bit of messy workaround. Python has a gzip module, which reads (and actually writes) gzip-compressed files on disk. But you don't have a file on disk, you have a gzip-compressed +
                                                                                                                      5. OK, this step is a little bit of messy workaround. Python has a gzip module, which reads (and actually writes) gzip-compressed files on disk. But you don't have a file on disk, you have a gzip-compressed buffer in memory, and you don't want to write out a temporary file just so you can uncompress it. So what you're going to do is create a file-like object out of the in-memory data (compresseddata), using the StringIO module. You first saw the StringIO module in the previous chapter, but now you've found another use for it. -
                                                                                                                      6. 3 -Now you can create an instance of GzipFile, and tell it that its “file” is the file-like object compressedstream. -
                                                                                                                        4 -This is the line that does all the actual work: “reading” from GzipFile will decompress the data. Strange? Yes, but it makes sense in a twisted kind of way. gzipper is a file-like object which represents a gzip-compressed file. That “file” is not a real file on disk, though; gzipper is really just “reading” from the file-like object you created with StringIO to wrap the compressed data, which is only in memory in the variable compresseddata. And where did that compressed data come from? You originally downloaded it from a remote HTTP server by “reading” from the file-like object you built with urllib2.build_opener. And amazingly, this all just works. Every step in the chain has no idea that the previous step is faking it. -
                                                                                                                        5 -Look ma, real data. (15955 bytes of it, in fact.)
                                                                                                                        -

                                                                                                                        “But wait!” I hear you cry. “This could be even easier!” I know what you're thinking. You're thinking that opener.open returns a file-like object, so why not cut out the StringIO middleman and just pass f directly to GzipFile? OK, maybe you weren't thinking that, but don't worry about it, because it doesn't work. -

                                                                                                                        Example 11.16. Decompressing the data directly from the server

                                                                                                                        ->>> f = opener.open(request)1
                                                                                                                        ->>> f.headers.get('Content-Encoding')         2
                                                                                                                        +
                                                                                                                      7. Now you can create an instance of GzipFile, and tell it that its “file” is the file-like object compressedstream. +
                                                                                                                      8. This is the line that does all the actual work: “reading” from GzipFile will decompress the data. Strange? Yes, but it makes sense in a twisted kind of way. gzipper is a file-like object which represents a gzip-compressed file. That “file” is not a real file on disk, though; gzipper is really just “reading” from the file-like object you created with StringIO to wrap the compressed data, which is only in memory in the variable compresseddata. And where did that compressed data come from? You originally downloaded it from a remote HTTP server by “reading” from the file-like object you built with urllib2.build_opener. And amazingly, this all just works. Every step in the chain has no idea that the previous step is faking it. +
                                                                                                                      9. Look ma, real data. (15955 bytes of it, in fact.)

                                                                                                                        “But wait!” I hear you cry. “This could be even easier!” I know what you're thinking. You're thinking that opener.open returns a file-like object, so why not cut out the StringIO middleman and just pass f directly to GzipFile? OK, maybe you weren't thinking that, but don't worry about it, because it doesn't work. +

                                                                                                                        Example 11.16. Decompressing the data directly from the server

                                                                                                                        +>>> f = opener.open(request)
                                                                                                                        +>>> f.headers.get('Content-Encoding')         
                                                                                                                         'gzip'
                                                                                                                        ->>> data = gzip.GzipFile(fileobj=f).read()    3
                                                                                                                        -Traceback (most recent call last):
                                                                                                                        +>>> data = gzip.GzipFile(fileobj=f).read()    
                                                                                                                        +Traceback (most recent call last):
                                                                                                                           File "<stdin>", line 1, in ?
                                                                                                                           File "c:\python23\lib\gzip.py", line 217, in read
                                                                                                                             self._read(readsize)
                                                                                                                           File "c:\python23\lib\gzip.py", line 252, in _read
                                                                                                                             pos = self.fileobj.tell()   # Save current position
                                                                                                                         AttributeError: addinfourl instance has no attribute 'tell'
                                                                                                                        -
                                                                                                                        - - - - - - - - - - - - - -
                                                                                                                        1 -Continuing from the previous example, you already have a Request object set up with an Accept-encoding: gzip header. -
                                                                                                                        2 -Simply opening the request will get you the headers (though not download any data yet). As you can see from the returned +
                                                                                                                        +
                                                                                                                          +
                                                                                                                        1. Continuing from the previous example, you already have a Request object set up with an Accept-encoding: gzip header. +
                                                                                                                        2. Simply opening the request will get you the headers (though not download any data yet). As you can see from the returned Content-Encoding header, this data has been sent gzip-compressed. -
                                                                                                                        3 -Since opener.open returns a file-like object, and you know from the headers that when you read it, you're going to get gzip-compressed data, +
                                                                                                                      10. Since opener.open returns a file-like object, and you know from the headers that when you read it, you're going to get gzip-compressed data, why not simply pass that file-like object directly to GzipFile? As you “read” from the GzipFile instance, it will “read” compressed data from the remote HTTP server and decompress it on the fly. It's a good idea, but unfortunately it doesn't work. Because of the way gzip compression works, GzipFile needs to save its position and move forwards and backwards through the compressed file. This doesn't work when the “file” is a stream of bytes coming from a remote server; all you can do with it is retrieve bytes one at a time, not move back and forth through the data stream. So the inelegant hack of using StringIO is the best solution: download the compressed data, create a file-like object out of it with StringIO, and then decompress the data from that. -
                                                                                                                      11. 11.9. Putting it all together

                                                                                                                        You've seen all the pieces for building an intelligent HTTP web services client. Now let's see how they all fit together. -

                                                                                                                        Example 11.17. The openanything function

                                                                                                                        -

                                                                                                                        This function is defined in openanything.py.

                                                                                                                        +

                                                                                                                        Example 11.17. The openanything function

                                                                                                                        +

                                                                                                                        This function is defined in openanything.py. +

                                                                                                                        
                                                                                                                         def openAnything(source, etag=None, lastmodified=None, agent=USER_AGENT):
                                                                                                                             # non-HTTP code omitted for brevity
                                                                                                                        -    if urlparse.urlparse(source)[0] == 'http':   1
                                                                                                                        +    if urlparse.urlparse(source)[0] == 'http':   
                                                                                                                                 # open URL with urllib2                 
                                                                                                                                 request = urllib2.Request(source)       
                                                                                                                        -        request.add_header('User-Agent', agent)  2
                                                                                                                        +        request.add_header('User-Agent', agent)  
                                                                                                                                 if etag:              
                                                                                                                        -            request.add_header('If-None-Match', etag)              3
                                                                                                                        +            request.add_header('If-None-Match', etag)              
                                                                                                                                 if lastmodified:      
                                                                                                                        -            request.add_header('If-Modified-Since', lastmodified)  4
                                                                                                                        -        request.add_header('Accept-encoding', 'gzip')              5
                                                                                                                        -        opener = urllib2.build_opener(SmartRedirectHandler(), DefaultErrorHandler()) 6
                                                                                                                        -        return opener.open(request)              7
                                                                                                                        -
                                                                                                                        - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                        1 -urlparse is a handy utility module for, you guessed it, parsing URLs. It's primary function, also called urlparse, takes a URL and splits it into a tuple of (scheme, domain, path, params, query string parameters, and fragment identifier). + request.add_header('If-Modified-Since', lastmodified) + request.add_header('Accept-encoding', 'gzip') + opener = urllib2.build_opener(SmartRedirectHandler(), DefaultErrorHandler()) + return opener.open(request) +
                                                                                                                        +
                                                                                                                          +
                                                                                                                        1. urlparse is a handy utility module for, you guessed it, parsing URLs. It's primary function, also called urlparse, takes a URL and splits it into a tuple of (scheme, domain, path, params, query string parameters, and fragment identifier). Of these, the only thing you care about is the scheme, to make sure that you're dealing with an HTTP URL (which urllib2 can handle). -
                                                                                                                        2 -You identify yourself to the HTTP server with the User-Agent passed in by the calling function. If no User-Agent was specified, you use a default one defined earlier in the openanything.py module. You never use the default one defined by urllib2. -
                                                                                                                        3 -If an ETag hash was given, send it in the If-None-Match header. -
                                                                                                                        4 -If a last-modified date was given, send it in the If-Modified-Since header. -
                                                                                                                        5 -Tell the server you would like compressed data if possible.
                                                                                                                        6 -Build a URL opener that uses both of the custom URL handlers: SmartRedirectHandler for handling 301 and 302 redirects, and DefaultErrorHandler for handling 304, 404, and other error conditions gracefully. -
                                                                                                                        7 -That's it! Open the URL and return a file-like object to the caller.
                                                                                                                        -

                                                                                                                        Example 11.18. The fetch function

                                                                                                                        -

                                                                                                                        This function is defined in openanything.py.

                                                                                                                        +
                                                                                                                      12. You identify yourself to the HTTP server with the User-Agent passed in by the calling function. If no User-Agent was specified, you use a default one defined earlier in the openanything.py module. You never use the default one defined by urllib2. +
                                                                                                                      13. If an ETag hash was given, send it in the If-None-Match header. +
                                                                                                                      14. If a last-modified date was given, send it in the If-Modified-Since header. +
                                                                                                                      15. Tell the server you would like compressed data if possible. +
                                                                                                                      16. Build a URL opener that uses both of the custom URL handlers: SmartRedirectHandler for handling 301 and 302 redirects, and DefaultErrorHandler for handling 304, 404, and other error conditions gracefully. +
                                                                                                                      17. That's it! Open the URL and return a file-like object to the caller. +

                                                                                                                        Example 11.18. The fetch function

                                                                                                                        +

                                                                                                                        This function is defined in openanything.py. +

                                                                                                                        
                                                                                                                         def fetch(source, etag=None, last_modified=None, agent=USER_AGENT):  
                                                                                                                             '''Fetch data and metadata from a URL, file, stream, or string'''
                                                                                                                             result = {}
                                                                                                                        -    f = openAnything(source, etag, last_modified, agent)              1
                                                                                                                        -    result['data'] = f.read()     2
                                                                                                                        +    f = openAnything(source, etag, last_modified, agent)              
                                                                                                                        +    result['data'] = f.read()     
                                                                                                                             if hasattr(f, 'headers'):    
                                                                                                                                 # save ETag, if the server sent one        
                                                                                                                        -        result['etag'] = f.headers.get('ETag')      3
                                                                                                                        +        result['etag'] = f.headers.get('ETag')      
                                                                                                                                 # save Last-Modified header, if the server sent one          
                                                                                                                        -        result['lastmodified'] = f.headers.get('Last-Modified')       4
                                                                                                                        -        if f.headers.get('content-encoding', '') == 'gzip':           5
                                                                                                                        +        result['lastmodified'] = f.headers.get('Last-Modified')       
                                                                                                                        +        if f.headers.get('content-encoding', '') == 'gzip':           
                                                                                                                                     # data came back gzip-compressed, decompress it          
                                                                                                                                     result['data'] = gzip.GzipFile(fileobj=StringIO(result['data']])).read()
                                                                                                                        -    if hasattr(f, 'url'):         6
                                                                                                                        +    if hasattr(f, 'url'):         
                                                                                                                                 result['url'] = f.url    
                                                                                                                                 result['status'] = 200   
                                                                                                                        -    if hasattr(f, 'status'):      7
                                                                                                                        +    if hasattr(f, 'status'):      
                                                                                                                                 result['status'] = f.status                
                                                                                                                             f.close()  
                                                                                                                             return result                
                                                                                                                        -
                                                                                                                        - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                        1 -First, you call the openAnything function with a URL, ETag hash, Last-Modified date, and User-Agent. -
                                                                                                                        2 -Read the actual data returned from the server. This may be compressed; if so, you'll decompress it later.
                                                                                                                        3 -Save the ETag hash returned from the server, so the calling application can pass it back to you next time, and you can pass it on to openAnything, which can stick it in the If-None-Match header and send it to the remote server. -
                                                                                                                        4 -Save the Last-Modified date too. -
                                                                                                                        5 -If the server says that it sent compressed data, decompress it.
                                                                                                                        6 -If you got a URL back from the server, save it, and assume that the status code is 200 until you find out otherwise. -
                                                                                                                        7 -If one of the custom URL handlers captured a status code, then save that too.
                                                                                                                        -

                                                                                                                        Example 11.19. Using openanything.py

                                                                                                                        ->>> import openanything
                                                                                                                        ->>> useragent = 'MyHTTPWebServicesApp/1.0'
                                                                                                                        ->>> url = 'http://diveintopython3.org/redir/example301.xml'
                                                                                                                        ->>> params = openanything.fetch(url, agent=useragent)              1
                                                                                                                        ->>> params   2
                                                                                                                        -{'url': 'http://diveintomark.org/xml/atom.xml', 
                                                                                                                        +
                                                                                                                        +
                                                                                                                          +
                                                                                                                        1. First, you call the openAnything function with a URL, ETag hash, Last-Modified date, and User-Agent. +
                                                                                                                        2. Read the actual data returned from the server. This may be compressed; if so, you'll decompress it later. +
                                                                                                                        3. Save the ETag hash returned from the server, so the calling application can pass it back to you next time, and you can pass it on to openAnything, which can stick it in the If-None-Match header and send it to the remote server. +
                                                                                                                        4. Save the Last-Modified date too. +
                                                                                                                        5. If the server says that it sent compressed data, decompress it. +
                                                                                                                        6. If you got a URL back from the server, save it, and assume that the status code is 200 until you find out otherwise. +
                                                                                                                        7. If one of the custom URL handlers captured a status code, then save that too. +

                                                                                                                          Example 11.19. Using openanything.py

                                                                                                                          +>>> import openanything
                                                                                                                          +>>> useragent = 'MyHTTPWebServicesApp/1.0'
                                                                                                                          +>>> url = 'http://diveintopython3.org/redir/example301.xml'
                                                                                                                          +>>> params = openanything.fetch(url, agent=useragent)              
                                                                                                                          +>>> params   
                                                                                                                          +{'url': 'http://diveintomark.org/xml/atom.xml', 
                                                                                                                           'lastmodified': 'Thu, 15 Apr 2004 19:45:21 GMT', 
                                                                                                                           'etag': '"e842a-3e53-55d97640"', 
                                                                                                                           'status': 301,
                                                                                                                           'data': '<?xml version="1.0" encoding="iso-8859-1"?>
                                                                                                                           <feed version="0.3"
                                                                                                                           <-- rest of data omitted for brevity -->'}
                                                                                                                          ->>> if params['status'] == 301:3
                                                                                                                          -...    url = params['url']
                                                                                                                          ->>> newparams = openanything.fetch(
                                                                                                                          -...    url, params['etag'], params['lastmodified'], useragent)    4
                                                                                                                          ->>> newparams
                                                                                                                          -{'url': 'http://diveintomark.org/xml/atom.xml', 
                                                                                                                          +>>> if params['status'] == 301:
                                                                                                                          +...    url = params['url']
                                                                                                                          +>>> newparams = openanything.fetch(
                                                                                                                          +...    url, params['etag'], params['lastmodified'], useragent)    
                                                                                                                          +>>> newparams
                                                                                                                          +{'url': 'http://diveintomark.org/xml/atom.xml', 
                                                                                                                           'lastmodified': None, 
                                                                                                                           'etag': '"e842a-3e53-55d97640"', 
                                                                                                                           'status': 304,
                                                                                                                          -'data': ''}  5
                                                                                                                          -
                                                                                                                          - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                          1 -The very first time you fetch a resource, you don't have an ETag hash or Last-Modified date, so you'll leave those out. (They're optional parameters.) -
                                                                                                                          2 -What you get back is a dictionary of several useful headers, the HTTP status code, and the actual data returned from the server. +'data': ''} +
                                                                                                                          +
                                                                                                                            +
                                                                                                                          1. The very first time you fetch a resource, you don't have an ETag hash or Last-Modified date, so you'll leave those out. (They're optional parameters.) +
                                                                                                                          2. What you get back is a dictionary of several useful headers, the HTTP status code, and the actual data returned from the server. openanything handles the gzip compression internally; you don't care about that at this level. -
                                                                                                                          3 -If you ever get a 301 status code, that's a permanent redirect, and you need to update your URL to the new address. -
                                                                                                                          4 -The second time you fetch the same resource, you have all sorts of information to pass back: a (possibly updated) URL, the +
                                                                                                                        8. If you ever get a 301 status code, that's a permanent redirect, and you need to update your URL to the new address. +
                                                                                                                        9. The second time you fetch the same resource, you have all sorts of information to pass back: a (possibly updated) URL, the ETag from the last time, the Last-Modified date from the last time, and of course your User-Agent. -
                                                                                                                        10. 5 -What you get back is again a dictionary, but the data hasn't changed, so all you got was a 304 status code and no data. -
                                                                                                                          +
                                                                                                                        11. What you get back is again a dictionary, but the data hasn't changed, so all you got was a 304 status code and no data.

                                                                                                                          11.10. Summary

                                                                                                                          The openanything.py and its functions should now make perfect sense.

                                                                                                                          There are 5 important features of HTTP web services that every client should support: -

                                                                                                                          +
                                                                                                                          -
                                                                                                                          +

                                                                                                                          Chapter 12. SOAP Web Services

                                                                                                                          Chapter 11 focused on document-oriented web services over HTTP. The “input parameter” was the URL, and the “return value” was an actual XML document which it was your responsibility to parse.

                                                                                                                          This chapter will focus on SOAP web services, which take a more structured approach. Rather than dealing with HTTP requests and XML documents directly, @@ -9671,7 +5813,7 @@ you can “call” a function through a SOAP library,

                                                                                                                          12.1. Diving In

                                                                                                                          You use Google, right? It's a popular search engine. Have you ever wished you could programmatically access Google search results? Now you can. Here is a program to search Google from Python. -

                                                                                                                          Example 12.1. search.py

                                                                                                                          from SOAPpy import WSDL
                                                                                                                          +

                                                                                                                          Example 12.1. search.py

                                                                                                                          from SOAPpy import WSDL
                                                                                                                           
                                                                                                                           # you'll need to configure these two values;
                                                                                                                           # see http://www.google.com/apis/
                                                                                                                          @@ -9698,9 +5840,9 @@ if __name__ == '__main__':
                                                                                                                           command line, you give the search query as a command-line argument, and it prints out the URL, title, and description of the
                                                                                                                           top five Google search results.
                                                                                                                           

                                                                                                                          Here is the sample output for a search for the word “python”. -

                                                                                                                          Example 12.2. Sample Usage of search.py

                                                                                                                          -C:\diveintopython3\common\py> python search.py "python"
                                                                                                                          -<b>Python</b> Programming Language
                                                                                                                          +

                                                                                                                          Example 12.2. Sample Usage of search.py

                                                                                                                          +C:\diveintopython3\common\py> python search.py "python"
                                                                                                                          +<b>Python</b> Programming Language
                                                                                                                           http://www.python.org/
                                                                                                                           Home page for <b>Python</b>, an interpreted, interactive, object-oriented,
                                                                                                                           extensible<br> programming language. <b>...</b> <b>Python</b>
                                                                                                                          @@ -9727,7 +5869,7 @@ http://diveintopython3.org/
                                                                                                                           Dive Into <b>Python</b>. <b>Python</b> from novice to pro. Find:
                                                                                                                           <b>...</b> It is also available in multiple<br> languages. Read
                                                                                                                           Dive Into <b>Python</b>. This book is still being written. <b>...</b>
                                                                                                                          -
                                                                                                                          +

                                                                                                                          Further Reading on SOAP

                                                                                                                          • http://www.xmethods.net/ is a repository of public access SOAP web services. @@ -9740,7 +5882,7 @@ Dive Into <b>Python</b>. This book is still being written. <b>...</b

                                                                                                                            Before you can dive into SOAP web services, you'll need to install three libraries: PyXML, fpconst, and SOAPpy.

                                                                                                                            12.2.1. Installing PyXML

                                                                                                                            The first library you need is PyXML, an advanced set of XML libraries that provide more functionality than the built-in XML libraries we studied in Chapter 9. -

                                                                                                                            +

                                                                                                                            Procedure 12.1.

                                                                                                                            Here is the procedure for installing PyXML:

                                                                                                                              @@ -9762,15 +5904,15 @@ Dive Into <b>Python</b>. This book is still being written. <b>...</b

                                                                                                                            To verify that you installed PyXML correctly, run your Python IDE and check the version of the XML libraries you have installed, as shown here. -

                                                                                                                            Example 12.3. Verifying PyXML Installation

                                                                                                                            ->>> import xml
                                                                                                                            ->>> xml.__version__
                                                                                                                            +

                                                                                                                            Example 12.3. Verifying PyXML Installation

                                                                                                                            +>>> import xml
                                                                                                                            +>>> xml.__version__
                                                                                                                             '0.8.3'
                                                                                                                             

                                                                                                                            This version number should match the version number of the PyXML installer program you downloaded and ran.

                                                                                                                            12.2.2. Installing fpconst

                                                                                                                            The second library you need is fpconst, a set of constants and functions for working with IEEE754 double-precision special values. This provides support for the special values Not-a-Number (NaN), Positive Infinity (Inf), and Negative Infinity (-Inf), which are part of the SOAP datatype specification. -

                                                                                                                            +

                                                                                                                            Procedure 12.2.

                                                                                                                            Here is the procedure for installing fpconst:

                                                                                                                              @@ -9792,14 +5934,14 @@ Dive Into <b>Python</b>. This book is still being written. <b>...</b

                                                                                                                            To verify that you installed fpconst correctly, run your Python IDE and check the version number. -

                                                                                                                            Example 12.4. Verifying fpconst Installation

                                                                                                                            ->>> import fpconst
                                                                                                                            ->>> fpconst.__version__
                                                                                                                            +

                                                                                                                            Example 12.4. Verifying fpconst Installation

                                                                                                                            +>>> import fpconst
                                                                                                                            +>>> fpconst.__version__
                                                                                                                             '0.6.0'
                                                                                                                             

                                                                                                                            This version number should match the version number of the fpconst archive you downloaded and installed.

                                                                                                                            12.2.3. Installing SOAPpy

                                                                                                                            The third and final requirement is the SOAP library itself: SOAPpy. -

                                                                                                                            +

                                                                                                                            Procedure 12.3.

                                                                                                                            Here is the procedure for installing SOAPpy:

                                                                                                                              @@ -9820,66 +5962,45 @@ Dive Into <b>Python</b>. This book is still being written. <b>...</b

                                                                                                                            To verify that you installed SOAPpy correctly, run your Python IDE and check the version number. -

                                                                                                                            Example 12.5. Verifying SOAPpy Installation

                                                                                                                            ->>> import SOAPpy
                                                                                                                            ->>> SOAPpy.__version__
                                                                                                                            +

                                                                                                                            Example 12.5. Verifying SOAPpy Installation

                                                                                                                            +>>> import SOAPpy
                                                                                                                            +>>> SOAPpy.__version__
                                                                                                                             '0.11.4'
                                                                                                                             

                                                                                                                            This version number should match the version number of the SOAPpy archive you downloaded and installed.

                                                                                                                            12.3. First Steps with SOAP

                                                                                                                            The heart of SOAP is the ability to call remote functions. There are a number of public access SOAP servers that provide simple functions for demonstration purposes.

                                                                                                                            The most popular public access SOAP server is http://www.xmethods.net/. This example uses a demonstration function that takes a United States zip code and returns the current temperature in that region. -

                                                                                                                            Example 12.6. Getting the Current Temperature

                                                                                                                            ->>> from SOAPpy import SOAPProxy            1
                                                                                                                            ->>> url = 'http://services.xmethods.net:80/soap/servlet/rpcrouter'
                                                                                                                            ->>> namespace = 'urn:xmethods-Temperature'  2
                                                                                                                            ->>> server = SOAPProxy(url, namespace)      3
                                                                                                                            ->>> server.getTemp('27502')                 4
                                                                                                                            +

                                                                                                                            Example 12.6. Getting the Current Temperature

                                                                                                                            +>>> from SOAPpy import SOAPProxy            
                                                                                                                            +>>> url = 'http://services.xmethods.net:80/soap/servlet/rpcrouter'
                                                                                                                            +>>> namespace = 'urn:xmethods-Temperature'  
                                                                                                                            +>>> server = SOAPProxy(url, namespace)      
                                                                                                                            +>>> server.getTemp('27502')                 
                                                                                                                             80.0
                                                                                                                            -
                                                                                                                            - - - - - - - - - - - - - - - - - -
                                                                                                                            1 -You access the remote SOAP server through a proxy class, SOAPProxy. The proxy handles all the internals of SOAP for you, including creating the XML request document out of the function name and argument list, sending the request over +
                                                                                                                            +
                                                                                                                              +
                                                                                                                            1. You access the remote SOAP server through a proxy class, SOAPProxy. The proxy handles all the internals of SOAP for you, including creating the XML request document out of the function name and argument list, sending the request over HTTP to the remote SOAP server, parsing the XML response document, and creating native Python values to return. You'll see what these XML documents look like in the next section. -
                                                                                                                            2 -Every SOAP service has a URL which handles all the requests. The same URL is used for all function calls. This particular service only has a single function, but later in this chapter you'll see +
                                                                                                                          • Every SOAP service has a URL which handles all the requests. The same URL is used for all function calls. This particular service only has a single function, but later in this chapter you'll see examples of the Google API, which has several functions. The service URL is shared by all functions.Each SOAP service also has a namespace, which is defined by the server and is completely arbitrary. It's simply part of the configuration required to call SOAP methods. It allows the server to share a single service URL and route requests between several unrelated services. It's like dividing Python modules into packages. -
                                                                                                                          • 3 -You're creating the SOAPProxy with the service URL and the service namespace. This doesn't make any connection to the SOAP server; it simply creates a local Python object. -
                                                                                                                            4 -Now with everything configured properly, you can actually call remote SOAP methods as if they were local functions. You pass arguments just like a normal function, and you get a return value just +
                                                                                                                          • You're creating the SOAPProxy with the service URL and the service namespace. This doesn't make any connection to the SOAP server; it simply creates a local Python object. +
                                                                                                                          • Now with everything configured properly, you can actually call remote SOAP methods as if they were local functions. You pass arguments just like a normal function, and you get a return value just like a normal function. But under the covers, there's a heck of a lot going on. -
                                                                                                                          • Let's peek under those covers.

                                                                                                                            12.4. Debugging SOAP Web Services

                                                                                                                            The SOAP libraries provide an easy way to see what's going on behind the scenes.

                                                                                                                            Turning on debugging is a simple matter of setting two flags in the SOAPProxy's configuration. -

                                                                                                                            Example 12.7. Debugging SOAP Web Services

                                                                                                                            ->>> from SOAPpy import SOAPProxy
                                                                                                                            ->>> url = 'http://services.xmethods.net:80/soap/servlet/rpcrouter'
                                                                                                                            ->>> n = 'urn:xmethods-Temperature'
                                                                                                                            ->>> server = SOAPProxy(url, namespace=n)     1
                                                                                                                            ->>> server.config.dumpSOAPOut = 1            2
                                                                                                                            ->>> server.config.dumpSOAPIn = 1
                                                                                                                            ->>> temperature = server.getTemp('27502')    3
                                                                                                                            -*** Outgoing SOAP ******************************************************
                                                                                                                            +

                                                                                                                            Example 12.7. Debugging SOAP Web Services

                                                                                                                            +>>> from SOAPpy import SOAPProxy
                                                                                                                            +>>> url = 'http://services.xmethods.net:80/soap/servlet/rpcrouter'
                                                                                                                            +>>> n = 'urn:xmethods-Temperature'
                                                                                                                            +>>> server = SOAPProxy(url, namespace=n)     
                                                                                                                            +>>> server.config.dumpSOAPOut = 1            
                                                                                                                            +>>> server.config.dumpSOAPIn = 1
                                                                                                                            +>>> temperature = server.getTemp('27502')    
                                                                                                                            +*** Outgoing SOAP ******************************************************
                                                                                                                             <?xml version="1.0" encoding="UTF-8"?>
                                                                                                                             <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                                                                                                                               xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                                                                                                                            @@ -9908,90 +6029,42 @@ region.
                                                                                                                             </SOAP-ENV:Envelope>
                                                                                                                             ************************************************************************
                                                                                                                             
                                                                                                                            ->>> temperature
                                                                                                                            +>>> temperature
                                                                                                                             80.0
                                                                                                                            -
                                                                                                                            - - - - - - - - - - - - - -
                                                                                                                            1 -First, create the SOAPProxy like normal, with the service URL and the namespace. -
                                                                                                                            2 -Second, turn on debugging by setting server.config.dumpSOAPIn and server.config.dumpSOAPOut. -
                                                                                                                            3 -Third, call the remote SOAP method as usual. The SOAP library will print out both the outgoing XML request document, and the incoming XML response document. This is all the hard +
                                                                                                                            +
                                                                                                                              +
                                                                                                                            1. First, create the SOAPProxy like normal, with the service URL and the namespace. +
                                                                                                                            2. Second, turn on debugging by setting server.config.dumpSOAPIn and server.config.dumpSOAPOut. +
                                                                                                                            3. Third, call the remote SOAP method as usual. The SOAP library will print out both the outgoing XML request document, and the incoming XML response document. This is all the hard work that SOAPProxy is doing for you. Intimidating, isn't it? Let's break it down. -

                                                                                                                            Most of the XML request document that gets sent to the server is just boilerplate. Ignore all the namespace declarations; they're going to be the same (or similar) for all SOAP calls. The heart of the “function call” is this fragment within the <Body> element: -

                                                                                                                            -<ns1:getTemp               1
                                                                                                                            -  xmlns:ns1="urn:xmethods-Temperature"       2
                                                                                                                            +
                                                                                                                            
                                                                                                                            +<ns1:getTemp               
                                                                                                                            +  xmlns:ns1="urn:xmethods-Temperature"       
                                                                                                                               SOAP-ENC:root="1">
                                                                                                                            -<v1 xsi:type="xsd:string">27502</v1>         3
                                                                                                                            +<v1 xsi:type="xsd:string">27502</v1>         
                                                                                                                             </ns1:getTemp>
                                                                                                                            -
                                                                                                                            - - - - - - - - - - - - - -
                                                                                                                            1 -The element name is the function name, getTemp. SOAPProxy uses getattr as a dispatcher. Instead of calling separate local methods based on the method name, it actually uses the method name to construct the XML +
                                                                                                                            +
                                                                                                                              +
                                                                                                                            1. The element name is the function name, getTemp. SOAPProxy uses getattr as a dispatcher. Instead of calling separate local methods based on the method name, it actually uses the method name to construct the XML request document. -
                                                                                                                            2 -The function's XML element is contained in a specific namespace, which is the namespace you specified when you created the +
                                                                                                                          • The function's XML element is contained in a specific namespace, which is the namespace you specified when you created the SOAPProxy object. Don't worry about the SOAP-ENC:root; that's boilerplate too. -
                                                                                                                          • 3 -The arguments of the function also got translated into XML. SOAPProxy introspects each argument to determine its datatype (in this case it's a string). The argument datatype goes into the xsi:type attribute, followed by the actual string value. -
                                                                                                                            +
                                                                                                                          • The arguments of the function also got translated into XML. SOAPProxy introspects each argument to determine its datatype (in this case it's a string). The argument datatype goes into the xsi:type attribute, followed by the actual string value.

                                                                                                                            The XML return document is equally easy to understand, once you know what to ignore. Focus on this fragment within the <Body>: -

                                                                                                                            -<ns1:getTempResponse           1
                                                                                                                            -  xmlns:ns1="urn:xmethods-Temperature"           2
                                                                                                                            +
                                                                                                                            
                                                                                                                            +<ns1:getTempResponse           
                                                                                                                            +  xmlns:ns1="urn:xmethods-Temperature"           
                                                                                                                               SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
                                                                                                                            -<return xsi:type="xsd:float">80.0</return>       3
                                                                                                                            +<return xsi:type="xsd:float">80.0</return>       
                                                                                                                             </ns1:getTempResponse>
                                                                                                                            -
                                                                                                                            - - - - - - - - - - - - - -
                                                                                                                            1 -The server wraps the function return value within a <getTempResponse> element. By convention, this wrapper element is the name of the function, plus Response. But it could really be almost anything; the important thing that SOAPProxy notices is not the element name, but the namespace. -
                                                                                                                            2 -The server returns the response in the same namespace we used in the request, the same namespace we specified when we first +
                                                                                                                            +
                                                                                                                              +
                                                                                                                            1. The server wraps the function return value within a <getTempResponse> element. By convention, this wrapper element is the name of the function, plus Response. But it could really be almost anything; the important thing that SOAPProxy notices is not the element name, but the namespace. +
                                                                                                                            2. The server returns the response in the same namespace we used in the request, the same namespace we specified when we first create the SOAPProxy. Later in this chapter we'll see what happens if you forget to specify the namespace when creating the SOAPProxy. -
                                                                                                                            3 -The return value is specified, along with its datatype (it's a float). SOAPProxy uses this explicit datatype to create a Python object of the correct native datatype and return it. -
                                                                                                                            +
                                                                                                                          • The return value is specified, along with its datatype (it's a float). SOAPProxy uses this explicit datatype to create a Python object of the correct native datatype and return it.

                                                                                                                            12.5. Introducing WSDL

                                                                                                                            The SOAPProxy class proxies local method calls and transparently turns then into invocations of remote SOAP methods. As you've seen, this is a lot of work, and SOAPProxy does it quickly and transparently. What it doesn't do is provide any means of method introspection.

                                                                                                                            Consider this: the previous two sections showed an example of calling a simple remote SOAP method with one argument and one return value, both of simple data types. This required knowing, and keeping track of, the @@ -10006,7 +6079,7 @@ a module, and with a little work, drill down to individual function declarations

                                                                                                                            A WSDL file is just that: a file. More specifically, it's an XML file. It usually lives on the same server you use to access the SOAP web services it describes, although there's nothing special about it. Later in this chapter, we'll download the WSDL file for the Google API and use it locally. That doesn't mean we're calling Google locally; the WSDL file still describes the remote functions sitting on Google's server.

                                                                                                                            A WSDL file contains a description of everything involved in calling a SOAP web service: -

                                                                                                                            +
                                                                                                                            • The service URL and namespace @@ -10022,105 +6095,57 @@ a module, and with a little work, drill down to individual function declarations

                                                                                                                              Like many things in the web services arena, WSDL has a long and checkered history, full of political strife and intrigue. I will skip over this history entirely, since it bores me to tears. There were other standards that tried to do similar things, but WSDL won, so let's learn how to use it.

                                                                                                                              The most fundamental thing that WSDL allows you to do is discover the available methods offered by a SOAP server. -

                                                                                                                              Example 12.8. Discovering The Available Methods

                                                                                                                              ->>> from SOAPpy import WSDL          1
                                                                                                                              ->>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl')
                                                                                                                              ->>> server = WSDL.Proxy(wsdlFile)    2
                                                                                                                              ->>> server.methods.keys()            3
                                                                                                                              +

                                                                                                                              Example 12.8. Discovering The Available Methods

                                                                                                                              +>>> from SOAPpy import WSDL          
                                                                                                                              +>>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl')
                                                                                                                              +>>> server = WSDL.Proxy(wsdlFile)    
                                                                                                                              +>>> server.methods.keys()            
                                                                                                                               [u'getTemp']
                                                                                                                              -
                                                                                                                              - - - - - - - - - - - - - -
                                                                                                                              1 -SOAPpy includes a WSDL parser. At the time of this writing, it was labeled as being in the early stages of development, but I had no problem parsing +
                                                                                                                              +
                                                                                                                                +
                                                                                                                              1. SOAPpy includes a WSDL parser. At the time of this writing, it was labeled as being in the early stages of development, but I had no problem parsing any of the WSDL files I tried. -
                                                                                                                              2 -To use a WSDL file, you again use a proxy class, WSDL.Proxy, which takes a single argument: the WSDL file. Note that in this case you are passing in the URL of a WSDL file stored on the remote server, but the proxy class works just as well with a local copy of the WSDL file. The act of creating the WSDL proxy will download the WSDL file and parse it, so it there are any errors in the WSDL file (or it can't be fetched due to networking problems), you'll know about it immediately. -
                                                                                                                              3 -The WSDL proxy class exposes the available functions as a Python dictionary, server.methods. So getting the list of available methods is as simple as calling the dictionary method keys(). -
                                                                                                                              +
                                                                                                                            • To use a WSDL file, you again use a proxy class, WSDL.Proxy, which takes a single argument: the WSDL file. Note that in this case you are passing in the URL of a WSDL file stored on the remote server, but the proxy class works just as well with a local copy of the WSDL file. The act of creating the WSDL proxy will download the WSDL file and parse it, so it there are any errors in the WSDL file (or it can't be fetched due to networking problems), you'll know about it immediately. +
                                                                                                                            • The WSDL proxy class exposes the available functions as a Python dictionary, server.methods. So getting the list of available methods is as simple as calling the dictionary method keys().

                                                                                                                              Okay, so you know that this SOAP server offers a single method: getTemp. But how do you call it? The WSDL proxy object can tell you that too. -

                                                                                                                              Example 12.9. Discovering A Method's Arguments

                                                                                                                              ->>> callInfo = server.methods['getTemp']  1
                                                                                                                              ->>> callInfo.inparams   2
                                                                                                                              +

                                                                                                                              Example 12.9. Discovering A Method's Arguments

                                                                                                                              +>>> callInfo = server.methods['getTemp']  
                                                                                                                              +>>> callInfo.inparams   
                                                                                                                               [<SOAPpy.wstools.WSDLTools.ParameterInfo instance at 0x00CF3AD0>]
                                                                                                                              ->>> callInfo.inparams[0].name             3
                                                                                                                              +>>> callInfo.inparams[0].name             
                                                                                                                               u'zipcode'
                                                                                                                              ->>> callInfo.inparams[0].type             4
                                                                                                                              +>>> callInfo.inparams[0].type             
                                                                                                                               (u'http://www.w3.org/2001/XMLSchema', u'string')
                                                                                                                              -
                                                                                                                              - - - - - - - - - - - - - - - - - -
                                                                                                                              1 -The server.methods dictionary is filled with a SOAPpy-specific structure called CallInfo. A CallInfo object contains information about one specific function, including the function arguments. -
                                                                                                                              2 -The function arguments are stored in callInfo.inparams, which is a Python list of ParameterInfo objects that hold information about each parameter. -
                                                                                                                              3 -Each ParameterInfo object contains a name attribute, which is the argument name. You are not required to know the argument name to call the function through SOAP, but SOAP does support calling functions with named arguments (just like Python), and WSDL.Proxy will correctly handle mapping named arguments to the remote function if you choose to use them. -
                                                                                                                              4 -Each parameter is also explicitly typed, using datatypes defined in XML Schema. You saw this in the wire trace in the previous +
                                                                                                                              +
                                                                                                                                +
                                                                                                                              1. The server.methods dictionary is filled with a SOAPpy-specific structure called CallInfo. A CallInfo object contains information about one specific function, including the function arguments. +
                                                                                                                              2. The function arguments are stored in callInfo.inparams, which is a Python list of ParameterInfo objects that hold information about each parameter. +
                                                                                                                              3. Each ParameterInfo object contains a name attribute, which is the argument name. You are not required to know the argument name to call the function through SOAP, but SOAP does support calling functions with named arguments (just like Python), and WSDL.Proxy will correctly handle mapping named arguments to the remote function if you choose to use them. +
                                                                                                                              4. Each parameter is also explicitly typed, using datatypes defined in XML Schema. You saw this in the wire trace in the previous section; the XML Schema namespace was part of the “boilerplate” I told you to ignore. For our purposes here, you may continue to ignore it. The zipcode parameter is a string, and if you pass in a Python string to the WSDL.Proxy object, it will map it correctly and send it to the server. -

                                                                                                                              WSDL also lets you introspect into a function's return values. -

                                                                                                                              Example 12.10. Discovering A Method's Return Values

                                                                                                                              ->>> callInfo.outparams            1
                                                                                                                              +

                                                                                                                              Example 12.10. Discovering A Method's Return Values

                                                                                                                              +>>> callInfo.outparams            
                                                                                                                               [<SOAPpy.wstools.WSDLTools.ParameterInfo instance at 0x00CF3AF8>]
                                                                                                                              ->>> callInfo.outparams[0].name    2
                                                                                                                              +>>> callInfo.outparams[0].name    
                                                                                                                               u'return'
                                                                                                                              ->>> callInfo.outparams[0].type
                                                                                                                              +>>> callInfo.outparams[0].type
                                                                                                                               (u'http://www.w3.org/2001/XMLSchema', u'float')
                                                                                                                              -
                                                                                                                              - - - - - - - - - -
                                                                                                                              1 -The adjunct to callInfo.inparams for function arguments is callInfo.outparams for return value. It is also a list, because functions called through SOAP can return multiple values, just like Python functions. -
                                                                                                                              2 -Each ParameterInfo object contains name and type. This function returns a single value, named return, which is a float. -
                                                                                                                              +
                                                                                                                              +
                                                                                                                                +
                                                                                                                              1. The adjunct to callInfo.inparams for function arguments is callInfo.outparams for return value. It is also a list, because functions called through SOAP can return multiple values, just like Python functions. +
                                                                                                                              2. Each ParameterInfo object contains name and type. This function returns a single value, named return, which is a float.

                                                                                                                                Let's put it all together, and call a SOAP web service through a WSDL proxy. -

                                                                                                                                Example 12.11. Calling A Web Service Through A WSDL Proxy

                                                                                                                                ->>> from SOAPpy import WSDL
                                                                                                                                ->>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl')
                                                                                                                                ->>> server = WSDL.Proxy(wsdlFile)               1
                                                                                                                                ->>> server.getTemp('90210')   2
                                                                                                                                +

                                                                                                                                Example 12.11. Calling A Web Service Through A WSDL Proxy

                                                                                                                                +>>> from SOAPpy import WSDL
                                                                                                                                +>>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl')
                                                                                                                                +>>> server = WSDL.Proxy(wsdlFile)               
                                                                                                                                +>>> server.getTemp('90210')   
                                                                                                                                 66.0
                                                                                                                                ->>> server.soapproxy.config.dumpSOAPOut = 1     3
                                                                                                                                ->>> server.soapproxy.config.dumpSOAPIn = 1
                                                                                                                                ->>> temperature = server.getTemp('90210')
                                                                                                                                -*** Outgoing SOAP ******************************************************
                                                                                                                                +>>> server.soapproxy.config.dumpSOAPOut = 1     
                                                                                                                                +>>> server.soapproxy.config.dumpSOAPIn = 1
                                                                                                                                +>>> temperature = server.getTemp('90210')
                                                                                                                                +*** Outgoing SOAP ******************************************************
                                                                                                                                 <?xml version="1.0" encoding="UTF-8"?>
                                                                                                                                 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                                                                                                                                   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                                                                                                                                @@ -10149,34 +6174,18 @@ u'return'
                                                                                                                                 </SOAP-ENV:Envelope>
                                                                                                                                 ************************************************************************
                                                                                                                                 
                                                                                                                                ->>> temperature
                                                                                                                                +>>> temperature
                                                                                                                                 66.0
                                                                                                                                -
                                                                                                                                - - - - - - - - - - - - - -
                                                                                                                                1 -The configuration is simpler than calling the SOAP service directly, since the WSDL file contains the both service URL and namespace you need to call the service. Creating the WSDL.Proxy object downloads the WSDL file, parses it, and configures a SOAPProxy object that it uses to call the actual SOAP web service. -
                                                                                                                                2 -Once the WSDL.Proxy object is created, you can call a function as easily as you did with the SOAPProxy object. This is not surprising; the WSDL.Proxy is just a wrapper around the SOAPProxy with some introspection methods added, so the syntax for calling functions is the same. -
                                                                                                                                3 -You can access the WSDL.Proxy's SOAPProxy with server.soapproxy. This is useful to turning on debugging, so that when you can call functions through the WSDL proxy, its SOAPProxy will dump the outgoing and incoming XML documents that are going over the wire. -
                                                                                                                                +
                                                                                                                                +
                                                                                                                                  +
                                                                                                                                1. The configuration is simpler than calling the SOAP service directly, since the WSDL file contains the both service URL and namespace you need to call the service. Creating the WSDL.Proxy object downloads the WSDL file, parses it, and configures a SOAPProxy object that it uses to call the actual SOAP web service. +
                                                                                                                                2. Once the WSDL.Proxy object is created, you can call a function as easily as you did with the SOAPProxy object. This is not surprising; the WSDL.Proxy is just a wrapper around the SOAPProxy with some introspection methods added, so the syntax for calling functions is the same. +
                                                                                                                                3. You can access the WSDL.Proxy's SOAPProxy with server.soapproxy. This is useful to turning on debugging, so that when you can call functions through the WSDL proxy, its SOAPProxy will dump the outgoing and incoming XML documents that are going over the wire.

                                                                                                                                  12.7. Searching Google

                                                                                                                                  Let's finally turn to the sample code that you saw that the beginning of this chapter, which does something more useful and exciting than get the current temperature.

                                                                                                                                  Google provides a SOAP API for programmatically accessing Google search results. To use it, you will need to sign up for Google Web Services. -

                                                                                                                                  +

                                                                                                                                  Procedure 12.4. Signing Up for Google Web Services

                                                                                                                                  1. @@ -10191,15 +6200,15 @@ u'return'

                                                                                                                                  Once you have your developer key and your Google WSDL file in a known place, you can start poking around with Google Web Services. -

                                                                                                                                  Example 12.12. Introspecting Google Web Services

                                                                                                                                  ->>> from SOAPpy import WSDL
                                                                                                                                  ->>> server = WSDL.Proxy('/path/to/your/GoogleSearch.wsdl') 1
                                                                                                                                  ->>> server.methods.keys()                2
                                                                                                                                  +

                                                                                                                                  Example 12.12. Introspecting Google Web Services

                                                                                                                                  +>>> from SOAPpy import WSDL
                                                                                                                                  +>>> server = WSDL.Proxy('/path/to/your/GoogleSearch.wsdl') 
                                                                                                                                  +>>> server.methods.keys()                
                                                                                                                                   [u'doGoogleSearch', u'doGetCachedPage', u'doSpellingSuggestion']
                                                                                                                                  ->>> callInfo = server.methods['doGoogleSearch']
                                                                                                                                  ->>> for arg in callInfo.inparams:        3
                                                                                                                                  -...    print arg.name.ljust(15), arg.type
                                                                                                                                  -key             (u'http://www.w3.org/2001/XMLSchema', u'string')
                                                                                                                                  +>>> callInfo = server.methods['doGoogleSearch']
                                                                                                                                  +>>> for arg in callInfo.inparams:        
                                                                                                                                  +...    print arg.name.ljust(15), arg.type
                                                                                                                                  +key             (u'http://www.w3.org/2001/XMLSchema', u'string')
                                                                                                                                   q               (u'http://www.w3.org/2001/XMLSchema', u'string')
                                                                                                                                   start           (u'http://www.w3.org/2001/XMLSchema', u'int')
                                                                                                                                   maxResults      (u'http://www.w3.org/2001/XMLSchema', u'int')
                                                                                                                                  @@ -10209,32 +6218,16 @@ safeSearch      (u'http://www.w3.org/2001/XMLSchema', u'boolean')
                                                                                                                                   lr              (u'http://www.w3.org/2001/XMLSchema', u'string')
                                                                                                                                   ie              (u'http://www.w3.org/2001/XMLSchema', u'string')
                                                                                                                                   oe              (u'http://www.w3.org/2001/XMLSchema', u'string')
                                                                                                                                  -
                                                                                                                                  - - - - - - - - - - - - - -
                                                                                                                                  1 -Getting started with Google web services is easy: just create a WSDL.Proxy object and point it at your local copy of Google's WSDL file. -
                                                                                                                                  2 -According to the WSDL file, Google offers three functions: doGoogleSearch, doGetCachedPage, and doSpellingSuggestion. These do exactly what they sound like: perform a Google search and return the results programmatically, get access to the +
                                                                                                                                  +
                                                                                                                                    +
                                                                                                                                  1. Getting started with Google web services is easy: just create a WSDL.Proxy object and point it at your local copy of Google's WSDL file. +
                                                                                                                                  2. According to the WSDL file, Google offers three functions: doGoogleSearch, doGetCachedPage, and doSpellingSuggestion. These do exactly what they sound like: perform a Google search and return the results programmatically, get access to the cached version of a page from the last time Google saw it, and offer spelling suggestions for commonly misspelled search words. -
                                                                                                                                  3 -The doGoogleSearch function takes a number of parameters of various types. Note that while the WSDL file can tell you what the arguments are called and what datatype they are, it can't tell you what they mean or how to use +
                                                                                                                                4. The doGoogleSearch function takes a number of parameters of various types. Note that while the WSDL file can tell you what the arguments are called and what datatype they are, it can't tell you what they mean or how to use them. It could theoretically tell you the acceptable range of values for each parameter, if only specific values were allowed, but Google's WSDL file is not that detailed. WSDL.Proxy can't work magic; it can only give you the information provided in the WSDL file. -
                                                                                                                                5. Here is a brief synopsis of all the parameters to the doGoogleSearch function: -

                                                                                                                                  +
                                                                                                                                  • key - Your Google API key, which you received when you signed up for Google web services. @@ -10258,87 +6251,55 @@ oe (u'http://www.w3.org/2001/XMLSchema', u'string')
                                                                                                                                  • ie and oe (“input encoding” and “output encoding”) - Deprecated, both must be utf-8.
                                                                                                                                  -

                                                                                                                                  Example 12.13. Searching Google

                                                                                                                                  ->>> from SOAPpy import WSDL
                                                                                                                                  ->>> server = WSDL.Proxy('/path/to/your/GoogleSearch.wsdl')
                                                                                                                                  ->>> key = 'YOUR_GOOGLE_API_KEY'
                                                                                                                                  ->>> results = server.doGoogleSearch(key, 'mark', 0, 10, False, "",
                                                                                                                                  -...    False, "", "utf-8", "utf-8")             1
                                                                                                                                  ->>> len(results.resultElements)2
                                                                                                                                  +

                                                                                                                                  Example 12.13. Searching Google

                                                                                                                                  +>>> from SOAPpy import WSDL
                                                                                                                                  +>>> server = WSDL.Proxy('/path/to/your/GoogleSearch.wsdl')
                                                                                                                                  +>>> key = 'YOUR_GOOGLE_API_KEY'
                                                                                                                                  +>>> results = server.doGoogleSearch(key, 'mark', 0, 10, False, "",
                                                                                                                                  +...    False, "", "utf-8", "utf-8")             
                                                                                                                                  +>>> len(results.resultElements)
                                                                                                                                   10
                                                                                                                                  ->>> results.resultElements[0].URL                3
                                                                                                                                  +>>> results.resultElements[0].URL                
                                                                                                                                   'http://diveintomark.org/'
                                                                                                                                  ->>> results.resultElements[0].title
                                                                                                                                  +>>> results.resultElements[0].title
                                                                                                                                   'dive into <b>mark</b>'
                                                                                                                                  -
                                                                                                                                  - - - - - - - - - - - - - -
                                                                                                                                  1 -After setting up the WSDL.Proxy object, you can call server.doGoogleSearch with all ten parameters. Remember to use your own Google API key that you received when you signed up for Google web services. -
                                                                                                                                  2 -There's a lot of information returned, but let's look at the actual search results first. They're stored in results.resultElements, and you can access them just like a normal Python list. -
                                                                                                                                  3 -Each element in the resultElements is an object that has a URL, title, snippet, and other useful attributes. At this point you can use normal Python introspection techniques like dir(results.resultElements[0]) to see the available attributes. Or you can introspect through the WSDL proxy object and look through the function's outparams. Each technique will give you the same information. -
                                                                                                                                  +
                                                                                                                                  +
                                                                                                                                    +
                                                                                                                                  1. After setting up the WSDL.Proxy object, you can call server.doGoogleSearch with all ten parameters. Remember to use your own Google API key that you received when you signed up for Google web services. +
                                                                                                                                  2. There's a lot of information returned, but let's look at the actual search results first. They're stored in results.resultElements, and you can access them just like a normal Python list. +
                                                                                                                                  3. Each element in the resultElements is an object that has a URL, title, snippet, and other useful attributes. At this point you can use normal Python introspection techniques like dir(results.resultElements[0]) to see the available attributes. Or you can introspect through the WSDL proxy object and look through the function's outparams. Each technique will give you the same information.

                                                                                                                                    The results object contains more than the actual search results. It also contains information about the search itself, such as how long it took and how many results were found (even though only 10 were returned). The Google web interface shows this information, and you can access it programmatically too. -

                                                                                                                                    Example 12.14. Accessing Secondary Information From Google

                                                                                                                                    ->>> results.searchTime   1
                                                                                                                                    +

                                                                                                                                    Example 12.14. Accessing Secondary Information From Google

                                                                                                                                    +>>> results.searchTime   
                                                                                                                                     0.224919
                                                                                                                                    ->>> results.estimatedTotalResultsCount     2
                                                                                                                                    +>>> results.estimatedTotalResultsCount     
                                                                                                                                     29800000
                                                                                                                                    ->>> results.directoryCategories            3
                                                                                                                                    -[<SOAPpy.Types.structType item at 14367400>:
                                                                                                                                    +>>> results.directoryCategories            
                                                                                                                                    +[<SOAPpy.Types.structType item at 14367400>:
                                                                                                                                      {'fullViewableName':
                                                                                                                                       'Top/Arts/Literature/World_Literature/American/19th_Century/Twain,_Mark',
                                                                                                                                       'specialEncoding': ''}]
                                                                                                                                    ->>> results.directoryCategories[0].fullViewableName
                                                                                                                                    +>>> results.directoryCategories[0].fullViewableName
                                                                                                                                     'Top/Arts/Literature/World_Literature/American/19th_Century/Twain,_Mark'
                                                                                                                                    -
                                                                                                                                    - - - - - - - - - - - - - -
                                                                                                                                    1 -This search took 0.224919 seconds. That does not include the time spent sending and receiving the actual SOAP XML documents. It's just the time that Google spent processing your request once it received it. -
                                                                                                                                    2 -In total, there were approximately 30 million results. You can access them 10 at a time by changing the start parameter and calling server.doGoogleSearch again. -
                                                                                                                                    3 -For some queries, Google also returns a list of related categories in the Google Directory. You can append these URLs to http://directory.google.com/ to construct the link to the directory category page. -
                                                                                                                                    +
                                                                                                                                    +
                                                                                                                                      +
                                                                                                                                    1. This search took 0.224919 seconds. That does not include the time spent sending and receiving the actual SOAP XML documents. It's just the time that Google spent processing your request once it received it. +
                                                                                                                                    2. In total, there were approximately 30 million results. You can access them 10 at a time by changing the start parameter and calling server.doGoogleSearch again. +
                                                                                                                                    3. For some queries, Google also returns a list of related categories in the Google Directory. You can append these URLs to http://directory.google.com/ to construct the link to the directory category page.

                                                                                                                                      12.8. Troubleshooting SOAP Web Services

                                                                                                                                      Of course, the world of SOAP web services is not all happiness and light. Sometimes things go wrong.

                                                                                                                                      As you've seen throughout this chapter, SOAP involves several layers. There's the HTTP layer, since SOAP is sending XML documents to, and receiving XML documents from, an HTTP server. So all the debugging techniques you learned in Chapter 11, HTTP Web Services come into play here. You can import httplib and then set httplib.HTTPConnection.debuglevel = 1 to see the underlying HTTP traffic.

                                                                                                                                      Beyond the underlying HTTP layer, there are a number of things that can go wrong. SOAPpy does an admirable job hiding the SOAP syntax from you, but that also means it can be difficult to determine where the problem is when things don't work.

                                                                                                                                      Here are a few examples of common mistakes that I've made in using SOAP web services, and the errors they generated. -

                                                                                                                                      Example 12.15. Calling a Method With an Incorrectly Configured Proxy

                                                                                                                                      ->>> from SOAPpy import SOAPProxy
                                                                                                                                      ->>> url = 'http://services.xmethods.net:80/soap/servlet/rpcrouter'
                                                                                                                                      ->>> server = SOAPProxy(url)    1
                                                                                                                                      ->>> server.getTemp('27502')    2
                                                                                                                                      -<Fault SOAP-ENV:Server.BadTargetObjectURI:
                                                                                                                                      +

                                                                                                                                      Example 12.15. Calling a Method With an Incorrectly Configured Proxy

                                                                                                                                      +>>> from SOAPpy import SOAPProxy
                                                                                                                                      +>>> url = 'http://services.xmethods.net:80/soap/servlet/rpcrouter'
                                                                                                                                      +>>> server = SOAPProxy(url)    
                                                                                                                                      +>>> server.getTemp('27502')    
                                                                                                                                      +<Fault SOAP-ENV:Server.BadTargetObjectURI:
                                                                                                                                       Unable to determine object id from call: is the method element namespaced?>
                                                                                                                                       Traceback (most recent call last):
                                                                                                                                         File "<stdin>", line 1, in ?
                                                                                                                                      @@ -10350,29 +6311,18 @@ Traceback (most recent call last):
                                                                                                                                           raise p
                                                                                                                                       SOAPpy.Types.faultType: <Fault SOAP-ENV:Server.BadTargetObjectURI:
                                                                                                                                       Unable to determine object id from call: is the method element namespaced?>
                                                                                                                                      -
                                                                                                                                      - - - - - - - - - -
                                                                                                                                      1 -Did you spot the mistake? You're creating a SOAPProxy manually, and you've correctly specified the service URL, but you haven't specified the namespace. Since multiple services may be routed through the same service URL, the namespace is essential to determine which service you're trying to talk to, and therefore which method you're really +
                                                                                                                                      +
                                                                                                                                        +
                                                                                                                                      1. Did you spot the mistake? You're creating a SOAPProxy manually, and you've correctly specified the service URL, but you haven't specified the namespace. Since multiple services may be routed through the same service URL, the namespace is essential to determine which service you're trying to talk to, and therefore which method you're really calling. -
                                                                                                                                      2 -The server responds by sending a SOAP Fault, which SOAPpy turns into a Python exception of type SOAPpy.Types.faultType. All errors returned from any SOAP server will always be SOAP Faults, so you can easily catch this exception. In this case, the human-readable part of the SOAP Fault gives a clue to the problem: the method element is not namespaced, because the original SOAPProxy object was not configured with a service namespace. -
                                                                                                                                      +
                                                                                                                                    4. The server responds by sending a SOAP Fault, which SOAPpy turns into a Python exception of type SOAPpy.Types.faultType. All errors returned from any SOAP server will always be SOAP Faults, so you can easily catch this exception. In this case, the human-readable part of the SOAP Fault gives a clue to the problem: the method element is not namespaced, because the original SOAPProxy object was not configured with a service namespace.

                                                                                                                                      Misconfiguring the basic elements of the SOAP service is one of the problems that WSDL aims to solve. The WSDL file contains the service URL and namespace, so you can't get it wrong. Of course, there are still other things you can get wrong. -

                                                                                                                                      Example 12.16. Calling a Method With the Wrong Arguments

                                                                                                                                      ->>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl'
                                                                                                                                      ->>> server = WSDL.Proxy(wsdlFile)
                                                                                                                                      ->>> temperature = server.getTemp(27502)              1
                                                                                                                                      -<Fault SOAP-ENV:Server: Exception while handling service request:
                                                                                                                                      -services.temperature.TempService.getTemp(int) -- no signature match>   2
                                                                                                                                      +

                                                                                                                                      Example 12.16. Calling a Method With the Wrong Arguments

                                                                                                                                      +>>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl'
                                                                                                                                      +>>> server = WSDL.Proxy(wsdlFile)
                                                                                                                                      +>>> temperature = server.getTemp(27502)              
                                                                                                                                      +<Fault SOAP-ENV:Server: Exception while handling service request:
                                                                                                                                      +services.temperature.TempService.getTemp(int) -- no signature match>   
                                                                                                                                       Traceback (most recent call last):
                                                                                                                                         File "<stdin>", line 1, in ?
                                                                                                                                         File "c:\python23\Lib\site-packages\SOAPpy\Client.py", line 453, in __call__
                                                                                                                                      @@ -10383,49 +6333,32 @@ Traceback (most recent call last):
                                                                                                                                           raise p
                                                                                                                                       SOAPpy.Types.faultType: <Fault SOAP-ENV:Server: Exception while handling service request:
                                                                                                                                       services.temperature.TempService.getTemp(int) -- no signature match>
                                                                                                                                      -
                                                                                                                                      - - - - - - - - - -
                                                                                                                                      1 -Did you spot the mistake? It's a subtle one: you're calling server.getTemp with an integer instead of a string. As you saw from introspecting the WSDL file, the getTemp() SOAP function takes a single argument, zipcode, which must be a string. WSDL.Proxy will not coerce datatypes for you; you need to pass the exact datatypes that the server expects. -
                                                                                                                                      2 -Again, the server returns a SOAP Fault, and the human-readable part of the error gives a clue as to the problem: you're calling a getTemp function with an integer value, but there is no function defined with that name that takes an integer. In theory, SOAP allows you to overload functions, so you could have two functions in the same SOAP service with the same name and the same number of arguments, but the arguments were of different datatypes. This is why +
                                                                                                                                      +
                                                                                                                                        +
                                                                                                                                      1. Did you spot the mistake? It's a subtle one: you're calling server.getTemp with an integer instead of a string. As you saw from introspecting the WSDL file, the getTemp() SOAP function takes a single argument, zipcode, which must be a string. WSDL.Proxy will not coerce datatypes for you; you need to pass the exact datatypes that the server expects. +
                                                                                                                                      2. Again, the server returns a SOAP Fault, and the human-readable part of the error gives a clue as to the problem: you're calling a getTemp function with an integer value, but there is no function defined with that name that takes an integer. In theory, SOAP allows you to overload functions, so you could have two functions in the same SOAP service with the same name and the same number of arguments, but the arguments were of different datatypes. This is why it's important to match the datatypes exactly, and why WSDL.Proxy doesn't coerce datatypes for you. If it did, you could end up calling a completely different function! Good luck debugging that one. It's much easier to be picky about datatypes and fail as quickly as possible if you get them wrong. -

                                                                                                                                      It's also possible to write Python code that expects a different number of return values than the remote function actually returns. -

                                                                                                                                      Example 12.17. Calling a Method and Expecting the Wrong Number of Return Values

                                                                                                                                      ->>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl'
                                                                                                                                      ->>> server = WSDL.Proxy(wsdlFile)
                                                                                                                                      ->>> (city, temperature) = server.getTemp(27502)  1
                                                                                                                                      -Traceback (most recent call last):
                                                                                                                                      +

                                                                                                                                      Example 12.17. Calling a Method and Expecting the Wrong Number of Return Values

                                                                                                                                      +>>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl'
                                                                                                                                      +>>> server = WSDL.Proxy(wsdlFile)
                                                                                                                                      +>>> (city, temperature) = server.getTemp(27502)  
                                                                                                                                      +Traceback (most recent call last):
                                                                                                                                         File "<stdin>", line 1, in ?
                                                                                                                                       TypeError: unpack non-sequence
                                                                                                                                      -
                                                                                                                                      - - - - - -
                                                                                                                                      1 -Did you spot the mistake? server.getTemp only returns one value, a float, but you've written code that assumes you're getting two values and trying to assign them +
                                                                                                                                      +
                                                                                                                                        +
                                                                                                                                      1. Did you spot the mistake? server.getTemp only returns one value, a float, but you've written code that assumes you're getting two values and trying to assign them to two different variables. Note that this does not fail with a SOAP fault. As far as the remote server is concerned, nothing went wrong at all. The error only occurred after the SOAP transaction was complete, WSDL.Proxy returned a float, and your local Python interpreter tried to accomodate your request to split it into two different variables. Since the function only returned one value, you get a Python exception trying to split it, not a SOAP Fault. -

                                                                                                                                      What about Google's web service? The most common problem I've had with it is that I forget to set the application key properly. -

                                                                                                                                      Example 12.18. Calling a Method With An Application-Specific Error

                                                                                                                                      ->>> from SOAPpy import WSDL
                                                                                                                                      ->>> server = WSDL.Proxy(r'/path/to/local/GoogleSearch.wsdl')
                                                                                                                                      ->>> results = server.doGoogleSearch('foo', 'mark', 0, 10, False, "", 1
                                                                                                                                      -...    False, "", "utf-8", "utf-8")
                                                                                                                                      -<Fault SOAP-ENV:Server:          2
                                                                                                                                      +

                                                                                                                                      Example 12.18. Calling a Method With An Application-Specific Error

                                                                                                                                      +>>> from SOAPpy import WSDL
                                                                                                                                      +>>> server = WSDL.Proxy(r'/path/to/local/GoogleSearch.wsdl')
                                                                                                                                      +>>> results = server.doGoogleSearch('foo', 'mark', 0, 10, False, "", 
                                                                                                                                      +...    False, "", "utf-8", "utf-8")
                                                                                                                                      +<Fault SOAP-ENV:Server:          
                                                                                                                                        Exception from service object: Invalid authorization key: foo:
                                                                                                                                        <SOAPpy.Types.structType detail at 14164616>:
                                                                                                                                        {'stackTrace':
                                                                                                                                      @@ -10495,24 +6428,13 @@ Caused by: com.google.soap.search.UserKeyInvalidException: Key was of wrong size
                                                                                                                                            QueryLimits.java:217)
                                                                                                                                          ... 14 more
                                                                                                                                       '}>
                                                                                                                                      -
                                                                                                                                      - - - - - - - - - -
                                                                                                                                      1 -Can you spot the mistake? There's nothing wrong with the calling syntax, or the number of arguments, or the datatypes. The +
                                                                                                                                      +
                                                                                                                                        +
                                                                                                                                      1. Can you spot the mistake? There's nothing wrong with the calling syntax, or the number of arguments, or the datatypes. The problem is application-specific: the first argument is supposed to be my application key, but foo is not a valid Google key. -
                                                                                                                                      2 -The Google server responds with a SOAP Fault and an incredibly long error message, which includes a complete Java stack trace. Remember that all SOAP errors are signified by SOAP Faults: errors in configuration, errors in function arguments, and application-specific errors like this. Buried in there +
                                                                                                                                    5. The Google server responds with a SOAP Fault and an incredibly long error message, which includes a complete Java stack trace. Remember that all SOAP errors are signified by SOAP Faults: errors in configuration, errors in function arguments, and application-specific errors like this. Buried in there somewhere is the crucial piece of information: Invalid authorization key: foo. -
                                                                                                                                    6. -
                                                                                                                                      +

                                                                                                                                      Further Reading on Troubleshooting SOAP

                                                                                                                                      • New developments for SOAPpy steps through trying to connect to another SOAP service that doesn't quite work as advertised. @@ -10521,9 +6443,9 @@ Caused by: com.google.soap.search.UserKeyInvalidException: Key was of wrong size

                                                                                                                                        12.9. Summary

                                                                                                                                        SOAP web services are very complicated. The specification is very ambitious and tries to cover many different use cases for web services. This chapter has touched on some of the simpler use cases. -

                                                                                                                                        +

                                                                                                                                        Before diving into the next chapter, make sure you're comfortable doing all of these things: -

                                                                                                                                        +
                                                                                                                                        • Connecting to a SOAP server and calling remote methods @@ -10534,17 +6456,17 @@ Caused by: com.google.soap.search.UserKeyInvalidException: Key was of wrong size
                                                                                                                                        • Troubleshooting common SOAP-related errors
                                                                                                                                        -
                                                                                                                                        +

                                                                                                                                        Chapter 13. Unit Testing

                                                                                                                                        13.1. Introduction to Roman numerals

                                                                                                                                        In previous chapters, you “dived in” by immediately looking at code and trying to understand it as quickly as possible. Now that you have some Python under your belt, you're going to step back and look at the steps that happen before the code gets written.

                                                                                                                                        In the next few chapters, you're going to write, debug, and optimize a set of utility functions to convert to and from Roman numerals. You saw the mechanics of constructing and validating Roman numerals in Section 7.3, “Case Study: Roman Numerals”, but now let's step back and consider what it would take to expand that into a two-way utility.

                                                                                                                                        The rules for Roman numerals lead to a number of interesting observations: -

                                                                                                                                        +
                                                                                                                                        1. There is only one correct way to represent a particular number as Roman numerals. -
                                                                                                                                        2. The converse is also true: if a string of characters is a valid Roman numeral, it represents only one number (i.e. it can only be read one way). +
                                                                                                                                        3. The converse is also true: if a string of characters is a valid Roman numeral, it represents only one number (i.e. it can only be read one way).
                                                                                                                                        4. There is a limited range of numbers that can be expressed as Roman numerals, specifically 1 through 3999. (The Romans did have several ways of expressing larger numbers, for instance by having a bar over a numeral to represent that its normal value should be multiplied by 1000, but you're not going to deal with that. For the purposes of this chapter, let's stipulate that Roman numerals go from 1 to 3999.) @@ -10555,7 +6477,7 @@ numerals. You saw the mechanics of constructing and validating Roman numerals in
                                                                                                                                        5. There is no way to represent fractions or non-integer numbers in Roman numerals.

                                                                                                                                        Given all of this, what would you expect out of a set of functions to convert to and from Roman numerals? -

                                                                                                                                        roman.py requirements

                                                                                                                                        +

                                                                                                                                        roman.py requirements

                                                                                                                                        1. toRoman should return the Roman numeral representation for all integers 1 to 3999. @@ -10572,10 +6494,10 @@ numerals. You saw the mechanics of constructing and validating Roman numerals in
                                                                                                                                        2. toRoman should always return a Roman numeral using uppercase letters. -
                                                                                                                                        3. fromRoman should only accept uppercase Roman numerals (i.e. it should fail when given lowercase input). +
                                                                                                                                        4. fromRoman should only accept uppercase Roman numerals (i.e. it should fail when given lowercase input).
                                                                                                                                        -
                                                                                                                                        +

                                                                                                                                        Further reading

                                                                                                                                        • This site has more on Roman numerals, including a fascinating history of how Romans and other civilizations really used them (short answer: haphazardly and inconsistently). @@ -10587,20 +6509,15 @@ numerals. You saw the mechanics of constructing and validating Roman numerals in behave the way you want them to. You read that right: you're going to write code that tests code that you haven't written yet.

                                                                                                                                          This is called unit testing, since the set of two conversion functions can be written and tested as a unit, separate from -any larger program they may become part of later. Python has a framework for unit testing, the appropriately-named unittest module. - - - - - - -
                                                                                                                                          Note
                                                                                                                                          unittest is included with Python 2.1 and later. Python 2.0 users can download it from pyunit.sourceforge.net. -
                                                                                                                                          +any larger program they may become part of later. Python has a framework for unit testing, the appropriately-named unittest module. + + +
                                                                                                                                          Noteunittest is included with Python 2.1 and later. Python 2.0 users can download it from pyunit.sourceforge.net.

                                                                                                                                          Unit testing is an important part of an overall testing-centric development strategy. If you write unit tests, it is important to write them early (preferably before writing the code that they test), and to keep them updated as code and requirements change. Unit testing is not a replacement for higher-level functional or system testing, but it is important in all phases of development: -

                                                                                                                                          +
                                                                                                                                          • Before writing code, it forces you to detail your requirements in a useful fashion.
                                                                                                                                          • While writing code, it keeps you from over-coding. When all the test cases pass, the function is complete. @@ -10618,8 +6535,9 @@ of development:

                                                                                                                                            This is the complete test suite for your Roman numeral conversion functions, which are yet to be written but will eventually be in roman.py. It is not immediately obvious how it all fits together; none of these classes or methods reference any of the others. There are good reasons for this, as you'll see shortly. -

                                                                                                                                            Example 13.1. romantest.py

                                                                                                                                            -

                                                                                                                                            If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                            +

                                                                                                                                            Example 13.1. romantest.py

                                                                                                                                            +

                                                                                                                                            If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                            
                                                                                                                                             """Unit test for roman.py"""
                                                                                                                                             
                                                                                                                                             import roman
                                                                                                                                            @@ -10753,14 +6671,14 @@ class CaseCheck(unittest.TestCase):
                                                                                                                                                         roman.fromRoman, numeral.lower())
                                                                                                                                             
                                                                                                                                             if __name__ == "__main__":
                                                                                                                                            -    unittest.main()   
                                                                                                                                            + unittest.main()

                                                                                                                                            Further reading

                                                                                                                                            • The PyUnit home page has an in-depth discussion of using the unittest framework, including advanced features not covered in this chapter.
                                                                                                                                            • The PyUnit FAQ explains why test cases are stored separately from the code they test. -
                                                                                                                                            • Python Library Reference summarizes the unittest module. +
                                                                                                                                            • Python Library Reference summarizes the unittest module.
                                                                                                                                            • ExtremeProgramming.org discusses why you should write unit tests. @@ -10771,20 +6689,20 @@ if __name__ == "__main__":

                                                                                                                                              The most fundamental part of unit testing is constructing individual test cases. A test case answers a single question about the code it is testing.

                                                                                                                                              A test case should be able to... -

                                                                                                                                              +
                                                                                                                                              • ...run completely by itself, without any human input. Unit testing is about automation.
                                                                                                                                              • ...determine by itself whether the function it is testing has passed or failed, without a human interpreting the results.
                                                                                                                                              • ...run in isolation, separate from any other test cases (even if they test the same functions). Each test case is an island.

                                                                                                                                              Given that, let's build the first test case. You have the following requirement: -

                                                                                                                                              +
                                                                                                                                              1. toRoman should return the Roman numeral representation for all integers 1 to 3999.
                                                                                                                                              -

                                                                                                                                              Example 13.2. testToRomanKnownValues

                                                                                                                                              -class KnownValues(unittest.TestCase):         1
                                                                                                                                              +

                                                                                                                                              Example 13.2. testToRomanKnownValues

                                                                                                                                              
                                                                                                                                              +class KnownValues(unittest.TestCase):         
                                                                                                                                                   knownValues = ( (1, 'I'),
                                                                                                                                                 (2, 'II'),
                                                                                                                                                 (3, 'III'),
                                                                                                                                              @@ -10840,62 +6758,31 @@ class KnownValues(unittest.TestCase):         2
                                                                                                                                              +  (3999, 'MMMCMXCIX'))      
                                                                                                                                               
                                                                                                                                              -    def testToRomanKnownValues(self):         3
                                                                                                                                              +    def testToRomanKnownValues(self):         
                                                                                                                                                       """toRoman should give known result with known input"""
                                                                                                                                                       for integer, numeral in self.knownValues:              
                                                                                                                                              -            result = roman.toRoman(integer)   4 5
                                                                                                                                              -            self.assertEqual(numeral, result) 6
                                                                                                                                              - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                                              1 -To write a test case, first subclass the TestCase class of the unittest module. This class provides many useful methods which you can use in your test case to test specific conditions. -
                                                                                                                                              2 -This is a list of integer/numeral pairs that I verified manually. It includes the lowest ten numbers, the highest number, + result = roman.toRoman(integer) + self.assertEqual(numeral, result)
                                                                                                                                              +
                                                                                                                                                +
                                                                                                                                              1. To write a test case, first subclass the TestCase class of the unittest module. This class provides many useful methods which you can use in your test case to test specific conditions. +
                                                                                                                                              2. This is a list of integer/numeral pairs that I verified manually. It includes the lowest ten numbers, the highest number, every number that translates to a single-character Roman numeral, and a random sampling of other valid numbers. The point of a unit test is not to test every possible input, but to test a representative sample. -
                                                                                                                                              3 -Every individual test is its own method, which must take no parameters and return no value. If the method exits normally +
                                                                                                                                            • Every individual test is its own method, which must take no parameters and return no value. If the method exits normally without raising an exception, the test is considered passed; if the method raises an exception, the test is considered failed. -
                                                                                                                                            • 4 -Here you call the actual toRoman function. (Well, the function hasn't be written yet, but once it is, this is the line that will call it.) Notice that you +
                                                                                                                                            • Here you call the actual toRoman function. (Well, the function hasn't be written yet, but once it is, this is the line that will call it.) Notice that you have now defined the API for the toRoman function: it must take an integer (the number to convert) and return a string (the Roman numeral representation). If the API is different than that, this test is considered failed. -
                                                                                                                                            • 5 -Also notice that you are not trapping any exceptions when you call toRoman. This is intentional. toRoman shouldn't raise an exception when you call it with valid input, and these input values are all valid. If toRoman raises an exception, this test is considered failed. -
                                                                                                                                              6 -Assuming the toRoman function was defined correctly, called correctly, completed successfully, and returned a value, the last step is to check +
                                                                                                                                            • Also notice that you are not trapping any exceptions when you call toRoman. This is intentional. toRoman shouldn't raise an exception when you call it with valid input, and these input values are all valid. If toRoman raises an exception, this test is considered failed. +
                                                                                                                                            • Assuming the toRoman function was defined correctly, called correctly, completed successfully, and returned a value, the last step is to check whether it returned the right value. This is a common question, and the TestCase class provides a method, assertEqual, to check whether two values are equal. If the result returned from toRoman (result) does not match the known value you were expecting (numeral), assertEqual will raise an exception and the test will fail. If the two values are equal, assertEqual will do nothing. If every value returned from toRoman matches the known value you expect, assertEqual never raises an exception, so testToRomanKnownValues eventually exits normally, which means toRoman has passed this test. -
                                                                                                                                            • 13.5. Testing for failure

                                                                                                                                              It is not enough to test that functions succeed when given good input; you must also test that they fail when given bad input. And not just any sort of failure; they must fail in the way you expect.

                                                                                                                                              Remember the other requirements for toRoman: -

                                                                                                                                              +
                                                                                                                                              1. toRoman should fail when given an integer outside the range 1 to 3999. @@ -10903,15 +6790,15 @@ class KnownValues(unittest.TestCase): exceptions, and the unittest module provides methods for testing whether a function raises a particular exception when given bad input. -

                                                                                                                                                Example 13.3. Testing bad input to toRoman

                                                                                                                                                +

                                                                                                                                                Example 13.3. Testing bad input to toRoman

                                                                                                                                                
                                                                                                                                                 class ToRomanBadInput(unittest.TestCase):          
                                                                                                                                                     def testTooLarge(self):      
                                                                                                                                                         """toRoman should fail with large input""" 
                                                                                                                                                -        self.assertRaises(roman.OutOfRangeError, roman.toRoman, 4000) 1
                                                                                                                                                +        self.assertRaises(roman.OutOfRangeError, roman.toRoman, 4000) 
                                                                                                                                                 
                                                                                                                                                     def testZero(self):          
                                                                                                                                                         """toRoman should fail with 0 input"""     
                                                                                                                                                -        self.assertRaises(roman.OutOfRangeError, roman.toRoman, 0)    2
                                                                                                                                                +        self.assertRaises(roman.OutOfRangeError, roman.toRoman, 0)    
                                                                                                                                                 
                                                                                                                                                     def testNegative(self):      
                                                                                                                                                         """toRoman should fail with negative input"""                
                                                                                                                                                @@ -10919,33 +6806,17 @@ class ToRomanBadInput(unittest.TestCase):
                                                                                                                                                 
                                                                                                                                                     def testNonInteger(self):    
                                                                                                                                                         """toRoman should fail with non-integer input"""             
                                                                                                                                                -        self.assertRaises(roman.NotIntegerError, roman.toRoman, 0.5)  3
                                                                                                                                                - - - - - - - - - - - - - -
                                                                                                                                                1 -The TestCase class of the unittest provides the assertRaises method, which takes the following arguments: the exception you're expecting, the function you're testing, and the arguments + self.assertRaises(roman.NotIntegerError, roman.toRoman, 0.5)
                                                                                                                                                +
                                                                                                                                                  +
                                                                                                                                                1. The TestCase class of the unittest provides the assertRaises method, which takes the following arguments: the exception you're expecting, the function you're testing, and the arguments you're passing that function. (If the function you're testing takes more than one argument, pass them all to assertRaises, in order, and it will pass them right along to the function you're testing.) Pay close attention to what you're doing here: instead of calling toRoman directly and manually checking that it raises a particular exception (by wrapping it in a try...except block), assertRaises has encapsulated all of that for us. All you do is give it the exception (roman.OutOfRangeError), the function (toRoman), and toRoman's arguments (4000), and assertRaises takes care of calling toRoman and checking to make sure that it raises roman.OutOfRangeError. (Also note that you're passing the toRoman function itself as an argument; you're not calling it, and you're not passing the name of it as a string. Have I mentioned recently how handy it is that everything in Python is an object, including functions and exceptions?) -
                                                                                                                                                2 -Along with testing numbers that are too large, you need to test numbers that are too small. Remember, Roman numerals cannot +
                                                                                                                                              2. Along with testing numbers that are too large, you need to test numbers that are too small. Remember, Roman numerals cannot express 0 or negative numbers, so you have a test case for each of those (testZero and testNegative). In testZero, you are testing that toRoman raises a roman.OutOfRangeError exception when called with 0; if it does not raise a roman.OutOfRangeError (either because it returns an actual value, or because it raises some other exception), this test is considered failed. -
                                                                                                                                              3. 3 -Requirement #3 specifies that toRoman cannot accept a non-integer number, so here you test to make sure that toRoman raises a roman.NotIntegerError exception when called with 0.5. If toRoman does not raise a roman.NotIntegerError, this test is considered failed. -
                                                                                                                                                +
                                                                                                                                              4. Requirement #3 specifies that toRoman cannot accept a non-integer number, so here you test to make sure that toRoman raises a roman.NotIntegerError exception when called with 0.5. If toRoman does not raise a roman.NotIntegerError, this test is considered failed.

                                                                                                                                                The next two requirements are similar to the first three, except they apply to fromRoman instead of toRoman: -

                                                                                                                                                +
                                                                                                                                                1. fromRoman should take a valid Roman numeral and return the number that it represents. @@ -10954,12 +6825,12 @@ class ToRomanBadInput(unittest.TestCase):

                                                                                                                                                Requirement #4 is handled in the same way as requirement #1, iterating through a sampling of known values and testing each in turn. Requirement #5 is handled in the same way as requirements #2 and #3, by testing a series of bad inputs and making sure fromRoman raises the appropriate exception. -

                                                                                                                                                Example 13.4. Testing bad input to fromRoman

                                                                                                                                                +

                                                                                                                                                Example 13.4. Testing bad input to fromRoman

                                                                                                                                                
                                                                                                                                                 class FromRomanBadInput(unittest.TestCase):  
                                                                                                                                                     def testTooManyRepeatedNumerals(self):   
                                                                                                                                                         """fromRoman should fail with too many repeated numerals"""              
                                                                                                                                                         for s in ('MMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'):             
                                                                                                                                                -            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s) 1
                                                                                                                                                +            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s) 
                                                                                                                                                 
                                                                                                                                                     def testRepeatedPairs(self):             
                                                                                                                                                         """fromRoman should fail with repeated pairs of numerals"""              
                                                                                                                                                @@ -10970,175 +6841,113 @@ class FromRomanBadInput(unittest.TestCase):
                                                                                                                                                         """fromRoman should fail with malformed antecedents""" 
                                                                                                                                                         for s in ('IIMXCC', 'VX', 'DCM', 'CMM', 'IXIV',
                                                                                                                                                 'MCMC', 'XCX', 'IVI', 'LM', 'LD', 'LC'):     
                                                                                                                                                -            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)
                                                                                                                                                - - - - - -
                                                                                                                                                1 -Not much new to say about these; the pattern is exactly the same as the one you used to test bad input to toRoman. I will briefly note that you have another exception: roman.InvalidRomanNumeralError. That makes a total of three custom exceptions that will need to be defined in roman.py (along with roman.OutOfRangeError and roman.NotIntegerError). You'll see how to define these custom exceptions when you actually start writing roman.py, later in this chapter. -
                                                                                                                                                + self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)
                                                                                                                                                +
                                                                                                                                                  +
                                                                                                                                                1. Not much new to say about these; the pattern is exactly the same as the one you used to test bad input to toRoman. I will briefly note that you have another exception: roman.InvalidRomanNumeralError. That makes a total of three custom exceptions that will need to be defined in roman.py (along with roman.OutOfRangeError and roman.NotIntegerError). You'll see how to define these custom exceptions when you actually start writing roman.py, later in this chapter.

                                                                                                                                                  13.6. Testing for sanity

                                                                                                                                                  Often, you will find that a unit of code contains a set of reciprocal functions, usually in the form of conversion functions where one converts A to B and the other converts B to A. In these cases, it is useful to create a “sanity check” to make sure that you can convert A to B and back to A without losing precision, incurring rounding errors, or triggering any other sort of bug.

                                                                                                                                                  Consider this requirement: -

                                                                                                                                                  +
                                                                                                                                                  1. If you take a number, convert it to Roman numerals, then convert that back to a number, you should end up with the number you started with. So fromRoman(toRoman(n)) == n for all n in 1..3999.
                                                                                                                                                  -

                                                                                                                                                  Example 13.5. Testing toRoman against fromRoman

                                                                                                                                                  +

                                                                                                                                                  Example 13.5. Testing toRoman against fromRoman

                                                                                                                                                  
                                                                                                                                                   class SanityCheck(unittest.TestCase):        
                                                                                                                                                       def testSanity(self):  
                                                                                                                                                           """fromRoman(toRoman(n))==n for all n"""
                                                                                                                                                  -        for integer in range(1, 4000):        1 2
                                                                                                                                                  +        for integer in range(1, 4000):         
                                                                                                                                                               numeral = roman.toRoman(integer) 
                                                                                                                                                               result = roman.fromRoman(numeral)
                                                                                                                                                  -            self.assertEqual(integer, result) 3
                                                                                                                                                  - - - - - - - - - - - - - -
                                                                                                                                                  1 -You've seen the range function before, but here it is called with two arguments, which returns a list of integers starting at the first argument (1) and counting consecutively up to but not including the second argument (4000). Thus, 1..3999, which is the valid range for converting to Roman numerals. -
                                                                                                                                                  2 -I just wanted to mention in passing that integer is not a keyword in Python; here it's just a variable name like any other. -
                                                                                                                                                  3 -The actual testing logic here is straightforward: take a number (integer), convert it to a Roman numeral (numeral), then convert it back to a number (result) and make sure you end up with the same number you started with. If not, assertEqual will raise an exception and the test will immediately be considered failed. If all the numbers match, assertEqual will always return silently, the entire testSanity method will eventually return silently, and the test will be considered passed. -
                                                                                                                                                  + self.assertEqual(integer, result)
                                                                                                                                                  +
                                                                                                                                                    +
                                                                                                                                                  1. You've seen the range function before, but here it is called with two arguments, which returns a list of integers starting at the first argument (1) and counting consecutively up to but not including the second argument (4000). Thus, 1..3999, which is the valid range for converting to Roman numerals. +
                                                                                                                                                  2. I just wanted to mention in passing that integer is not a keyword in Python; here it's just a variable name like any other. +
                                                                                                                                                  3. The actual testing logic here is straightforward: take a number (integer), convert it to a Roman numeral (numeral), then convert it back to a number (result) and make sure you end up with the same number you started with. If not, assertEqual will raise an exception and the test will immediately be considered failed. If all the numbers match, assertEqual will always return silently, the entire testSanity method will eventually return silently, and the test will be considered passed.

                                                                                                                                                    The last two requirements are different from the others because they seem both arbitrary and trivial: -

                                                                                                                                                    +
                                                                                                                                                    1. toRoman should always return a Roman numeral using uppercase letters. -
                                                                                                                                                    2. fromRoman should only accept uppercase Roman numerals (i.e. it should fail when given lowercase input). +
                                                                                                                                                    3. fromRoman should only accept uppercase Roman numerals (i.e. it should fail when given lowercase input).

                                                                                                                                                    In fact, they are somewhat arbitrary. You could, for instance, have stipulated that fromRoman accept lowercase and mixed case input. But they are not completely arbitrary; if toRoman is always returning uppercase output, then fromRoman must at least accept uppercase input, or the “sanity check” (requirement #6) would fail. The fact that it only accepts uppercase input is arbitrary, but as any systems integrator will tell you, case always matters, so it's worth specifying the behavior up front. And if it's worth specifying, it's worth testing. -

                                                                                                                                                    Example 13.6. Testing for case

                                                                                                                                                    +

                                                                                                                                                    Example 13.6. Testing for case

                                                                                                                                                    
                                                                                                                                                     class CaseCheck(unittest.TestCase): 
                                                                                                                                                         def testToRomanCase(self):      
                                                                                                                                                             """toRoman should always return uppercase"""  
                                                                                                                                                             for integer in range(1, 4000):                
                                                                                                                                                                 numeral = roman.toRoman(integer)          
                                                                                                                                                    -            self.assertEqual(numeral, numeral.upper())         1
                                                                                                                                                    +            self.assertEqual(numeral, numeral.upper())         
                                                                                                                                                     
                                                                                                                                                         def testFromRomanCase(self):    
                                                                                                                                                             """fromRoman should only accept uppercase input"""
                                                                                                                                                             for integer in range(1, 4000):                
                                                                                                                                                                 numeral = roman.toRoman(integer)          
                                                                                                                                                    -            roman.fromRoman(numeral.upper()) 2 3
                                                                                                                                                    +            roman.fromRoman(numeral.upper())  
                                                                                                                                                                 self.assertRaises(roman.InvalidRomanNumeralError,
                                                                                                                                                    -            roman.fromRoman, numeral.lower())   4
                                                                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                                                                    1 -The most interesting thing about this test case is all the things it doesn't test. It doesn't test that the value returned + roman.fromRoman, numeral.lower())
                                                                                                                                                    +
                                                                                                                                                      +
                                                                                                                                                    1. The most interesting thing about this test case is all the things it doesn't test. It doesn't test that the value returned from toRoman is right or even consistent; those questions are answered by separate test cases. You have a whole test case just to test for uppercase-ness. You might - be tempted to combine this with the sanity check, since both run through the entire range of values and call toRoman.[6] But that would violate one of the fundamental rules: each test case should answer only a single question. Imagine that you combined this case check with the sanity check, and + be tempted to combine this with the sanity check, since both run through the entire range of values and call toRoman. +[6] But that would violate one of the fundamental rules: each test case should answer only a single question. Imagine that you combined this case check with the sanity check, and then that test case failed. You would need to do further analysis to figure out which part of the test case failed to determine what the problem was. If you need to analyze the results of your unit testing just to figure out what they mean, it's a sure sign that you've mis-designed your test cases. -
                                                                                                                                                    2 -There's a similar lesson to be learned here: even though “you know” that toRoman always returns uppercase, you are explicitly converting its return value to uppercase here to test that fromRoman accepts uppercase input. Why? Because the fact that toRoman always returns uppercase is an independent requirement. If you changed that requirement so that, for instance, it always +
                                                                                                                                                  4. There's a similar lesson to be learned here: even though “you know” that toRoman always returns uppercase, you are explicitly converting its return value to uppercase here to test that fromRoman accepts uppercase input. Why? Because the fact that toRoman always returns uppercase is an independent requirement. If you changed that requirement so that, for instance, it always returned lowercase, the testToRomanCase test case would need to change, but this test case would still work. This was another of the fundamental rules: each test case must be able to work in isolation from any of the others. Every test case is an island. -
                                                                                                                                                  5. 3 -Note that you're not assigning the return value of fromRoman to anything. This is legal syntax in Python; if a function returns a value but nobody's listening, Python just throws away the return value. In this case, that's what you want. This test case doesn't test anything about the return +
                                                                                                                                                  6. Note that you're not assigning the return value of fromRoman to anything. This is legal syntax in Python; if a function returns a value but nobody's listening, Python just throws away the return value. In this case, that's what you want. This test case doesn't test anything about the return value; it just tests that fromRoman accepts the uppercase input without raising an exception. -
                                                                                                                                                  7. 4 -This is a complicated line, but it's very similar to what you did in the ToRomanBadInput and FromRomanBadInput tests. You are testing to make sure that calling a particular function (roman.fromRoman) with a particular value (numeral.lower(), the lowercase version of the current Roman numeral in the loop) raises a particular exception (roman.InvalidRomanNumeralError). If it does (each time through the loop), the test passes; if even one time it does something else (like raises a different +
                                                                                                                                                  8. This is a complicated line, but it's very similar to what you did in the ToRomanBadInput and FromRomanBadInput tests. You are testing to make sure that calling a particular function (roman.fromRoman) with a particular value (numeral.lower(), the lowercase version of the current Roman numeral in the loop) raises a particular exception (roman.InvalidRomanNumeralError). If it does (each time through the loop), the test passes; if even one time it does something else (like raises a different exception, or returning a value without raising an exception at all), the test fails. -
                                                                                                                                                  9. In the next chapter, you'll see how to write code that passes these tests. -



                                                                                                                                                    -
                                                                                                                                                    +


                                                                                                                                                    +

                                                                                                                                                    [6] “I can resist everything except temptation.” --Oscar Wilde -

                                                                                                                                                    +

                                                                                                                                                    Chapter 14. Test-First Programming

                                                                                                                                                    14.1. roman.py, stage 1

                                                                                                                                                    Now that the unit tests are complete, it's time to start writing the code that the test cases are attempting to test. You're going to do this in stages, so you can see all the unit tests fail, then watch them pass one by one as you fill in the gaps in roman.py. -

                                                                                                                                                    Example 14.1. roman1.py

                                                                                                                                                    +

                                                                                                                                                    Example 14.1. roman1.py

                                                                                                                                                    This file is available in py/roman/stage1/ in the examples directory. -

                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                    +

                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                    
                                                                                                                                                     """Convert to and from Roman numerals"""
                                                                                                                                                     
                                                                                                                                                     #Define exceptions
                                                                                                                                                    -class RomanError(Exception): pass                1
                                                                                                                                                    -class OutOfRangeError(RomanError): pass          2
                                                                                                                                                    +class RomanError(Exception): pass                
                                                                                                                                                    +class OutOfRangeError(RomanError): pass          
                                                                                                                                                     class NotIntegerError(RomanError): pass
                                                                                                                                                    -class InvalidRomanNumeralError(RomanError): pass 3
                                                                                                                                                    +class InvalidRomanNumeralError(RomanError): pass 
                                                                                                                                                     
                                                                                                                                                     def toRoman(n):
                                                                                                                                                         """convert integer to Roman numeral"""
                                                                                                                                                    -    pass     4
                                                                                                                                                    +    pass     
                                                                                                                                                     
                                                                                                                                                     def fromRoman(s):
                                                                                                                                                         """convert Roman numeral to integer"""
                                                                                                                                                         pass
                                                                                                                                                    -
                                                                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                                                                    1 -This is how you define your own custom exceptions in Python. Exceptions are classes, and you create your own by subclassing existing exceptions. It is strongly recommended (but not +
                                                                                                                                                    +
                                                                                                                                                      +
                                                                                                                                                    1. This is how you define your own custom exceptions in Python. Exceptions are classes, and you create your own by subclassing existing exceptions. It is strongly recommended (but not required) that you subclass Exception, which is the base class that all built-in exceptions inherit from. Here I am defining RomanError (inherited from Exception) to act as the base class for all my other custom exceptions to follow. This is a matter of style; I could just as easily have inherited each individual exception from the Exception class directly. -
                                                                                                                                                    2 -The OutOfRangeError and NotIntegerError exceptions will eventually be used by toRoman to flag various forms of invalid input, as specified in ToRomanBadInput. -
                                                                                                                                                    3 -The InvalidRomanNumeralError exception will eventually be used by fromRoman to flag invalid input, as specified in FromRomanBadInput. -
                                                                                                                                                    4 -At this stage, you want to define the API of each of your functions, but you don't want to code them yet, so you stub them out using the Python reserved word pass. -
                                                                                                                                                    +
                                                                                                                                                  10. The OutOfRangeError and NotIntegerError exceptions will eventually be used by toRoman to flag various forms of invalid input, as specified in ToRomanBadInput. +
                                                                                                                                                  11. The InvalidRomanNumeralError exception will eventually be used by fromRoman to flag invalid input, as specified in FromRomanBadInput. +
                                                                                                                                                  12. At this stage, you want to define the API of each of your functions, but you don't want to code them yet, so you stub them out using the Python reserved word pass.

                                                                                                                                                    Now for the big moment (drum roll please): you're finally going to run the unit test against this stubby little module. At this point, every test case should fail. In fact, if any test case passes in stage 1, you should go back to romantest.py and re-evaluate why you coded a test so useless that it passes with do-nothing functions.

                                                                                                                                                    Run romantest1.py with the -v command-line option, which will give more verbose output so you can see exactly what's going on as each test case runs. With any luck, your output should look like this: -

                                                                                                                                                    Example 14.2. Output of romantest1.py against roman1.py

                                                                                                                                                    fromRoman should only accept uppercase input ... ERROR
                                                                                                                                                    +

                                                                                                                                                    Example 14.2. Output of romantest1.py against roman1.py

                                                                                                                                                    fromRoman should only accept uppercase input ... ERROR
                                                                                                                                                     toRoman should always return uppercase ... ERROR
                                                                                                                                                     fromRoman should fail with malformed antecedents ... FAIL
                                                                                                                                                     fromRoman should fail with repeated pairs of numerals ... FAIL
                                                                                                                                                    @@ -11154,143 +6963,123 @@ toRoman should fail with 0 input ... FAIL
                                                                                                                                                     ======================================================================
                                                                                                                                                     ERROR: fromRoman should only accept uppercase input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 154, in testFromRomanCase
                                                                                                                                                         roman1.fromRoman(numeral.upper())
                                                                                                                                                    -AttributeError: 'None' object has no attribute 'upper'
                                                                                                                                                    +AttributeError: 'None' object has no attribute 'upper'
                                                                                                                                                     ======================================================================
                                                                                                                                                     ERROR: toRoman should always return uppercase
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 148, in testToRomanCase
                                                                                                                                                         self.assertEqual(numeral, numeral.upper())
                                                                                                                                                    -AttributeError: 'None' object has no attribute 'upper'
                                                                                                                                                    +AttributeError: 'None' object has no attribute 'upper'
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should fail with malformed antecedents
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 133, in testMalformedAntecedent
                                                                                                                                                         self.assertRaises(roman1.InvalidRomanNumeralError, roman1.fromRoman, s)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: InvalidRomanNumeralError
                                                                                                                                                    +AssertionError: InvalidRomanNumeralError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should fail with repeated pairs of numerals
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 127, in testRepeatedPairs
                                                                                                                                                         self.assertRaises(roman1.InvalidRomanNumeralError, roman1.fromRoman, s)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: InvalidRomanNumeralError
                                                                                                                                                    +AssertionError: InvalidRomanNumeralError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should fail with too many repeated numerals
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 122, in testTooManyRepeatedNumerals
                                                                                                                                                         self.assertRaises(roman1.InvalidRomanNumeralError, roman1.fromRoman, s)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: InvalidRomanNumeralError
                                                                                                                                                    +AssertionError: InvalidRomanNumeralError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should give known result with known input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 99, in testFromRomanKnownValues
                                                                                                                                                         self.assertEqual(integer, result)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual
                                                                                                                                                         raise self.failureException, (msg or '%s != %s' % (first, second))
                                                                                                                                                    -AssertionError: 1 != None
                                                                                                                                                    +AssertionError: 1 != None
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: toRoman should give known result with known input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 93, in testToRomanKnownValues
                                                                                                                                                         self.assertEqual(numeral, result)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual
                                                                                                                                                         raise self.failureException, (msg or '%s != %s' % (first, second))
                                                                                                                                                    -AssertionError: I != None
                                                                                                                                                    +AssertionError: I != None
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman(toRoman(n))==n for all n
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 141, in testSanity
                                                                                                                                                         self.assertEqual(integer, result)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual
                                                                                                                                                         raise self.failureException, (msg or '%s != %s' % (first, second))
                                                                                                                                                    -AssertionError: 1 != None
                                                                                                                                                    +AssertionError: 1 != None
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: toRoman should fail with non-integer input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 116, in testNonInteger
                                                                                                                                                         self.assertRaises(roman1.NotIntegerError, roman1.toRoman, 0.5)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: NotIntegerError
                                                                                                                                                    +AssertionError: NotIntegerError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: toRoman should fail with negative input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 112, in testNegative
                                                                                                                                                         self.assertRaises(roman1.OutOfRangeError, roman1.toRoman, -1)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: OutOfRangeError
                                                                                                                                                    +AssertionError: OutOfRangeError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: toRoman should fail with large input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 104, in testTooLarge
                                                                                                                                                         self.assertRaises(roman1.OutOfRangeError, roman1.toRoman, 4000)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: OutOfRangeError
                                                                                                                                                    +AssertionError: OutOfRangeError
                                                                                                                                                     ======================================================================
                                                                                                                                                    -FAIL: toRoman should fail with 0 input               1
                                                                                                                                                    +FAIL: toRoman should fail with 0 input               
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 108, in testZero
                                                                                                                                                         self.assertRaises(roman1.OutOfRangeError, roman1.toRoman, 0)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: OutOfRangeError    2
                                                                                                                                                    +AssertionError: OutOfRangeError    
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Ran 12 tests in 0.040s             3
                                                                                                                                                    +Ran 12 tests in 0.040s             
                                                                                                                                                     
                                                                                                                                                    -FAILED (failures=10, errors=2)     4
                                                                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                                                                    1 -Running the script runs unittest.main(), which runs each test case, which is to say each method defined in each class within romantest.py. For each test case, it prints out the docstring of the method and whether that test passed or failed. As expected, none of the test cases passed. -
                                                                                                                                                    2 -For each failed test case, unittest displays the trace information showing exactly what happened. In this case, the call to assertRaises (also called failUnlessRaises) raised an AssertionError because it was expecting toRoman to raise an OutOfRangeError and it didn't. -
                                                                                                                                                    3 -After the detail, unittest displays a summary of how many tests were performed and how long it took. -
                                                                                                                                                    4 -Overall, the unit test failed because at least one test case did not pass. When a test case doesn't pass, unittest distinguishes between failures and errors. A failure is a call to an assertXYZ method, like assertEqual or assertRaises, that fails because the asserted condition is not true or the expected exception was not raised. An error is any other sort +FAILED (failures=10, errors=2)
                                                                                                                                                    +
                                                                                                                                                      +
                                                                                                                                                    1. Running the script runs unittest.main(), which runs each test case, which is to say each method defined in each class within romantest.py. For each test case, it prints out the docstring of the method and whether that test passed or failed. As expected, none of the test cases passed. +
                                                                                                                                                    2. For each failed test case, unittest displays the trace information showing exactly what happened. In this case, the call to assertRaises (also called failUnlessRaises) raised an AssertionError because it was expecting toRoman to raise an OutOfRangeError and it didn't. +
                                                                                                                                                    3. After the detail, unittest displays a summary of how many tests were performed and how long it took. +
                                                                                                                                                    4. Overall, the unit test failed because at least one test case did not pass. When a test case doesn't pass, unittest distinguishes between failures and errors. A failure is a call to an assertXYZ method, like assertEqual or assertRaises, that fails because the asserted condition is not true or the expected exception was not raised. An error is any other sort of exception raised in the code you're testing or the unit test case itself. For instance, the testFromRomanCase method (“fromRoman should only accept uppercase input”) was an error, because the call to numeral.upper() raised an AttributeError exception, because toRoman was supposed to return a string but didn't. But testZero (“toRoman should fail with 0 input”) was a failure, because the call to fromRoman did not raise the InvalidRomanNumeral exception that assertRaises was looking for. -

                                                                                                                                                    14.2. roman.py, stage 2

                                                                                                                                                    Now that you have the framework of the roman module laid out, it's time to start writing code and passing test cases. -

                                                                                                                                                    Example 14.3. roman2.py

                                                                                                                                                    +

                                                                                                                                                    Example 14.3. roman2.py

                                                                                                                                                    This file is available in py/roman/stage2/ in the examples directory. -

                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                    +

                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                    
                                                                                                                                                     """Convert to and from Roman numerals"""
                                                                                                                                                     
                                                                                                                                                     #Define exceptions
                                                                                                                                                    @@ -11300,7 +7089,7 @@ class NotIntegerError(RomanError): pass
                                                                                                                                                     class InvalidRomanNumeralError(RomanError): pass
                                                                                                                                                     
                                                                                                                                                     #Define digit mapping
                                                                                                                                                    -romanNumeralMap = (('M',  1000), 1
                                                                                                                                                    +romanNumeralMap = (('M',  1000), 
                                                                                                                                                      ('CM', 900),
                                                                                                                                                      ('D',  500),
                                                                                                                                                      ('CD', 400),
                                                                                                                                                    @@ -11318,7 +7107,7 @@ def toRoman(n):
                                                                                                                                                         """convert integer to Roman numeral"""
                                                                                                                                                         result = ""
                                                                                                                                                         for numeral, integer in romanNumeralMap:
                                                                                                                                                    -        while n >= integer:      2
                                                                                                                                                    +        while n >= integer:      
                                                                                                                                                                 result += numeral
                                                                                                                                                                 n -= integer
                                                                                                                                                         return result
                                                                                                                                                    @@ -11326,13 +7115,10 @@ def toRoman(n):
                                                                                                                                                     def fromRoman(s):
                                                                                                                                                         """convert Roman numeral to integer"""
                                                                                                                                                         pass
                                                                                                                                                    -
                                                                                                                                                    - - - - - - - - - -
                                                                                                                                                    1 -romanNumeralMap is a tuple of tuples which defines three things: -
                                                                                                                                                    +
                                                                                                                                                    +
                                                                                                                                                      +
                                                                                                                                                    1. romanNumeralMap is a tuple of tuples which defines three things: +
                                                                                                                                                      1. The character representations of the most basic Roman numerals. Note that this is not just the single-character Roman numerals; you're also defining two-character pairs like CM (“one hundred less than one thousand”); this will make the toRoman code simpler later. @@ -11342,166 +7128,145 @@ def fromRoman(s):
                                                                                                                                                      2. The value of each Roman numeral. Each inner tuple is a pair of (numeral, value).
                                                                                                                                                      -
                                                                                                                                                    2 -Here's where your rich data structure pays off, because you don't need any special logic to handle the subtraction rule. +
                                                                                                                                                  13. Here's where your rich data structure pays off, because you don't need any special logic to handle the subtraction rule. To convert to Roman numerals, you simply iterate through romanNumeralMap looking for the largest integer value less than or equal to the input. Once found, you add the Roman numeral representation to the end of the output, subtract the corresponding integer value from the input, lather, rinse, repeat. -
                                                                                                                                                  14. -

                                                                                                                                                    Example 14.4. How toRoman works

                                                                                                                                                    -

                                                                                                                                                    If you're not clear how toRoman works, add a print statement to the end of the while loop:

                                                                                                                                                    +

                                                                                                                                                    Example 14.4. How toRoman works

                                                                                                                                                    +

                                                                                                                                                    If you're not clear how toRoman works, add a print statement to the end of the while loop:

                                                                                                                                                    
                                                                                                                                                             while n >= integer:
                                                                                                                                                                 result += numeral
                                                                                                                                                                 n -= integer
                                                                                                                                                    -            print 'subtracting', integer, 'from input, adding', numeral, 'to output'
                                                                                                                                                    ->>> import roman2
                                                                                                                                                    ->>> roman2.toRoman(1424)
                                                                                                                                                    -subtracting 1000 from input, adding M to output
                                                                                                                                                    +            print 'subtracting', integer, 'from input, adding', numeral, 'to output'
                                                                                                                                                    +>>> import roman2
                                                                                                                                                    +>>> roman2.toRoman(1424)
                                                                                                                                                    +subtracting 1000 from input, adding M to output
                                                                                                                                                     subtracting 400 from input, adding CD to output
                                                                                                                                                     subtracting 10 from input, adding X to output
                                                                                                                                                     subtracting 10 from input, adding X to output
                                                                                                                                                     subtracting 4 from input, adding IV to output
                                                                                                                                                     'MCDXXIV'
                                                                                                                                                     

                                                                                                                                                    So toRoman appears to work, at least in this manual spot check. But will it pass the unit testing? Well no, not entirely. -

                                                                                                                                                    Example 14.5. Output of romantest2.py against roman2.py

                                                                                                                                                    -

                                                                                                                                                    Remember to run romantest2.py with the -v command-line flag to enable verbose mode.

                                                                                                                                                    fromRoman should only accept uppercase input ... FAIL
                                                                                                                                                    -toRoman should always return uppercase ... ok1
                                                                                                                                                    +

                                                                                                                                                    Example 14.5. Output of romantest2.py against roman2.py

                                                                                                                                                    +

                                                                                                                                                    Remember to run romantest2.py with the -v command-line flag to enable verbose mode. +

                                                                                                                                                    fromRoman should only accept uppercase input ... FAIL
                                                                                                                                                    +toRoman should always return uppercase ... ok
                                                                                                                                                     fromRoman should fail with malformed antecedents ... FAIL
                                                                                                                                                     fromRoman should fail with repeated pairs of numerals ... FAIL
                                                                                                                                                     fromRoman should fail with too many repeated numerals ... FAIL
                                                                                                                                                     fromRoman should give known result with known input ... FAIL
                                                                                                                                                    -toRoman should give known result with known input ... ok       2
                                                                                                                                                    +toRoman should give known result with known input ... ok       
                                                                                                                                                     fromRoman(toRoman(n))==n for all n ... FAIL
                                                                                                                                                    -toRoman should fail with non-integer input ... FAIL            3
                                                                                                                                                    +toRoman should fail with non-integer input ... FAIL            
                                                                                                                                                     toRoman should fail with negative input ... FAIL
                                                                                                                                                     toRoman should fail with large input ... FAIL
                                                                                                                                                    -toRoman should fail with 0 input ... FAIL
                                                                                                                                                    - - - - - - - - - - - - - -
                                                                                                                                                    1 -toRoman does, in fact, always return uppercase, because romanNumeralMap defines the Roman numeral representations as uppercase. So this test passes already. -
                                                                                                                                                    2 -Here's the big news: this version of the toRoman function passes the known values test. Remember, it's not comprehensive, but it does put the function through its paces with a variety of good inputs, including +toRoman should fail with 0 input ... FAIL
                                                                                                                                                    +
                                                                                                                                                      +
                                                                                                                                                    1. toRoman does, in fact, always return uppercase, because romanNumeralMap defines the Roman numeral representations as uppercase. So this test passes already. +
                                                                                                                                                    2. Here's the big news: this version of the toRoman function passes the known values test. Remember, it's not comprehensive, but it does put the function through its paces with a variety of good inputs, including inputs that produce every single-character Roman numeral, the largest possible input (3999), and the input that produces the longest possible Roman numeral (3888). At this point, you can be reasonably confident that the function works for any good input value you could throw at it. -
                                                                                                                                                    3 -However, the function does not “work” for bad values; it fails every single bad input test. That makes sense, because you didn't include any checks for bad input. Those test cases look for specific exceptions to +
                                                                                                                                                  15. However, the function does not “work” for bad values; it fails every single bad input test. That makes sense, because you didn't include any checks for bad input. Those test cases look for specific exceptions to be raised (via assertRaises), and you're never raising them. You'll do that in the next stage. -
                                                                                                                                                  16. -

                                                                                                                                                    Here's the rest of the output of the unit test, listing the details of all the failures. You're down to 10.

                                                                                                                                                    
                                                                                                                                                    +

                                                                                                                                                    Here's the rest of the output of the unit test, listing the details of all the failures. You're down to 10. +

                                                                                                                                                    
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should only accept uppercase input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 156, in testFromRomanCase
                                                                                                                                                         roman2.fromRoman, numeral.lower())
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: InvalidRomanNumeralError
                                                                                                                                                    +AssertionError: InvalidRomanNumeralError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should fail with malformed antecedents
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 133, in testMalformedAntecedent
                                                                                                                                                         self.assertRaises(roman2.InvalidRomanNumeralError, roman2.fromRoman, s)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: InvalidRomanNumeralError
                                                                                                                                                    +AssertionError: InvalidRomanNumeralError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should fail with repeated pairs of numerals
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 127, in testRepeatedPairs
                                                                                                                                                         self.assertRaises(roman2.InvalidRomanNumeralError, roman2.fromRoman, s)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: InvalidRomanNumeralError
                                                                                                                                                    +AssertionError: InvalidRomanNumeralError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should fail with too many repeated numerals
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 122, in testTooManyRepeatedNumerals
                                                                                                                                                         self.assertRaises(roman2.InvalidRomanNumeralError, roman2.fromRoman, s)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: InvalidRomanNumeralError
                                                                                                                                                    +AssertionError: InvalidRomanNumeralError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman should give known result with known input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 99, in testFromRomanKnownValues
                                                                                                                                                         self.assertEqual(integer, result)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual
                                                                                                                                                         raise self.failureException, (msg or '%s != %s' % (first, second))
                                                                                                                                                    -AssertionError: 1 != None
                                                                                                                                                    +AssertionError: 1 != None
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: fromRoman(toRoman(n))==n for all n
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 141, in testSanity
                                                                                                                                                         self.assertEqual(integer, result)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual
                                                                                                                                                         raise self.failureException, (msg or '%s != %s' % (first, second))
                                                                                                                                                    -AssertionError: 1 != None
                                                                                                                                                    +AssertionError: 1 != None
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: toRoman should fail with non-integer input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 116, in testNonInteger
                                                                                                                                                         self.assertRaises(roman2.NotIntegerError, roman2.toRoman, 0.5)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: NotIntegerError
                                                                                                                                                    +AssertionError: NotIntegerError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: toRoman should fail with negative input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 112, in testNegative
                                                                                                                                                         self.assertRaises(roman2.OutOfRangeError, roman2.toRoman, -1)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: OutOfRangeError
                                                                                                                                                    +AssertionError: OutOfRangeError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: toRoman should fail with large input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 104, in testTooLarge
                                                                                                                                                         self.assertRaises(roman2.OutOfRangeError, roman2.toRoman, 4000)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: OutOfRangeError
                                                                                                                                                    +AssertionError: OutOfRangeError
                                                                                                                                                     ======================================================================
                                                                                                                                                     FAIL: toRoman should fail with 0 input
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "C:\docbook\dip\py\roman\stage2\romantest2.py", line 108, in testZero
                                                                                                                                                         self.assertRaises(roman2.OutOfRangeError, roman2.toRoman, 0)
                                                                                                                                                       File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                         raise self.failureException, excName
                                                                                                                                                    -AssertionError: OutOfRangeError
                                                                                                                                                    +AssertionError: OutOfRangeError
                                                                                                                                                     ----------------------------------------------------------------------
                                                                                                                                                     Ran 12 tests in 0.320s
                                                                                                                                                     
                                                                                                                                                     FAILED (failures=10)

                                                                                                                                                    14.3. roman.py, stage 3

                                                                                                                                                    Now that toRoman behaves correctly with good input (integers from 1 to 3999), it's time to make it behave correctly with bad input (everything else). -

                                                                                                                                                    Example 14.6. roman3.py

                                                                                                                                                    +

                                                                                                                                                    Example 14.6. roman3.py

                                                                                                                                                    This file is available in py/roman/stage3/ in the examples directory. -

                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                    +

                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                    
                                                                                                                                                     """Convert to and from Roman numerals"""
                                                                                                                                                     
                                                                                                                                                     #Define exceptions
                                                                                                                                                    @@ -11527,12 +7292,12 @@ romanNumeralMap = (('M',  1000),
                                                                                                                                                     
                                                                                                                                                     def toRoman(n):
                                                                                                                                                         """convert integer to Roman numeral"""
                                                                                                                                                    -    if not (0 < n < 4000):         1
                                                                                                                                                    -        raise OutOfRangeError, "number out of range (must be 1..3999)" 2
                                                                                                                                                    -    if int(n) <> n:                3
                                                                                                                                                    +    if not (0 < n < 4000):         
                                                                                                                                                    +        raise OutOfRangeError, "number out of range (must be 1..3999)" 
                                                                                                                                                    +    if int(n) <> n:                
                                                                                                                                                             raise NotIntegerError, "non-integers can not be converted"
                                                                                                                                                     
                                                                                                                                                    -    result = ""  4
                                                                                                                                                    +    result = ""  
                                                                                                                                                         for numeral, integer in romanNumeralMap:
                                                                                                                                                             while n >= integer:
                                                                                                                                                                 result += numeral
                                                                                                                                                    @@ -11542,162 +7307,116 @@ def toRoman(n):
                                                                                                                                                     def fromRoman(s):
                                                                                                                                                         """convert Roman numeral to integer"""
                                                                                                                                                         pass
                                                                                                                                                    -
                                                                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                                                                    1 -This is a nice Pythonic shortcut: multiple comparisons at once. This is equivalent to if not ((0 < n) and (n < 4000)), but it's much easier to read. This is the range check, and it should catch inputs that are too large, negative, or zero. -
                                                                                                                                                    2 -You raise exceptions yourself with the raise statement. You can raise any of the built-in exceptions, or you can raise any of your custom exceptions that you've defined. +
                                                                                                                                                    +
                                                                                                                                                      +
                                                                                                                                                    1. This is a nice Pythonic shortcut: multiple comparisons at once. This is equivalent to if not ((0 < n) and (n < 4000)), but it's much easier to read. This is the range check, and it should catch inputs that are too large, negative, or zero. +
                                                                                                                                                    2. You raise exceptions yourself with the raise statement. You can raise any of the built-in exceptions, or you can raise any of your custom exceptions that you've defined. The second parameter, the error message, is optional; if given, it is displayed in the traceback that is printed if the exception is never handled. -
                                                                                                                                                    3 -This is the non-integer check. Non-integers can not be converted to Roman numerals.
                                                                                                                                                    4 -The rest of the function is unchanged.
                                                                                                                                                    -

                                                                                                                                                    Example 14.7. Watching toRoman handle bad input

                                                                                                                                                    ->>> import roman3
                                                                                                                                                    ->>> roman3.toRoman(4000)
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +
                                                                                                                                                  17. This is the non-integer check. Non-integers can not be converted to Roman numerals. +
                                                                                                                                                  18. The rest of the function is unchanged. +

                                                                                                                                                    Example 14.7. Watching toRoman handle bad input

                                                                                                                                                    +>>> import roman3
                                                                                                                                                    +>>> roman3.toRoman(4000)
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "<interactive input>", line 1, in ?
                                                                                                                                                       File "roman3.py", line 27, in toRoman
                                                                                                                                                         raise OutOfRangeError, "number out of range (must be 1..3999)"
                                                                                                                                                     OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                    ->>> roman3.toRoman(1.5)
                                                                                                                                                    -Traceback (most recent call last):
                                                                                                                                                    +>>> roman3.toRoman(1.5)
                                                                                                                                                    +Traceback (most recent call last):
                                                                                                                                                       File "<interactive input>", line 1, in ?
                                                                                                                                                       File "roman3.py", line 29, in toRoman
                                                                                                                                                         raise NotIntegerError, "non-integers can not be converted"
                                                                                                                                                     NotIntegerError: non-integers can not be converted
                                                                                                                                                    -

                                                                                                                                                    Example 14.8. Output of romantest3.py against roman3.py

                                                                                                                                                    fromRoman should only accept uppercase input ... FAIL
                                                                                                                                                    +

                                                                                                                                                    Example 14.8. Output of romantest3.py against roman3.py

                                                                                                                                                    fromRoman should only accept uppercase input ... FAIL
                                                                                                                                                     toRoman should always return uppercase ... ok
                                                                                                                                                     fromRoman should fail with malformed antecedents ... FAIL
                                                                                                                                                     fromRoman should fail with repeated pairs of numerals ... FAIL
                                                                                                                                                     fromRoman should fail with too many repeated numerals ... FAIL
                                                                                                                                                     fromRoman should give known result with known input ... FAIL
                                                                                                                                                    -toRoman should give known result with known input ... ok 1
                                                                                                                                                    +toRoman should give known result with known input ... ok 
                                                                                                                                                     fromRoman(toRoman(n))==n for all n ... FAIL
                                                                                                                                                    -toRoman should fail with non-integer input ... ok        2
                                                                                                                                                    -toRoman should fail with negative input ... ok           3
                                                                                                                                                    +toRoman should fail with non-integer input ... ok        
                                                                                                                                                    +toRoman should fail with negative input ... ok           
                                                                                                                                                     toRoman should fail with large input ... ok
                                                                                                                                                    -toRoman should fail with 0 input ... ok
                                                                                                                                                    - - - - - - - - - - - - - -
                                                                                                                                                    1 -toRoman still passes the known values test, which is comforting. All the tests that passed in stage 2 still pass, so the latest code hasn't broken anything. -
                                                                                                                                                    2 -More exciting is the fact that all of the bad input tests now pass. This test, testNonInteger, passes because of the int(n) <> n check. When a non-integer is passed to toRoman, the int(n) <> n check notices it and raises the NotIntegerError exception, which is what testNonInteger is looking for. -
                                                                                                                                                    3 -This test, testNegative, passes because of the not (0 < n < 4000) check, which raises an OutOfRangeError exception, which is what testNegative is looking for. -
                                                                                                                                                    -
                                                                                                                                                    
                                                                                                                                                    +toRoman should fail with 0 input ... ok
                                                                                                                                                    +
                                                                                                                                                      +
                                                                                                                                                    1. toRoman still passes the known values test, which is comforting. All the tests that passed in stage 2 still pass, so the latest code hasn't broken anything. +
                                                                                                                                                    2. More exciting is the fact that all of the bad input tests now pass. This test, testNonInteger, passes because of the int(n) <> n check. When a non-integer is passed to toRoman, the int(n) <> n check notices it and raises the NotIntegerError exception, which is what testNonInteger is looking for. +
                                                                                                                                                    3. This test, testNegative, passes because of the not (0 < n < 4000) check, which raises an OutOfRangeError exception, which is what testNegative is looking for. +
                                                                                                                                                      
                                                                                                                                                       ======================================================================
                                                                                                                                                       FAIL: fromRoman should only accept uppercase input
                                                                                                                                                       ----------------------------------------------------------------------
                                                                                                                                                      -Traceback (most recent call last):
                                                                                                                                                      +Traceback (most recent call last):
                                                                                                                                                         File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 156, in testFromRomanCase
                                                                                                                                                           roman3.fromRoman, numeral.lower())
                                                                                                                                                         File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                           raise self.failureException, excName
                                                                                                                                                      -AssertionError: InvalidRomanNumeralError
                                                                                                                                                      +AssertionError: InvalidRomanNumeralError
                                                                                                                                                       ======================================================================
                                                                                                                                                       FAIL: fromRoman should fail with malformed antecedents
                                                                                                                                                       ----------------------------------------------------------------------
                                                                                                                                                      -Traceback (most recent call last):
                                                                                                                                                      +Traceback (most recent call last):
                                                                                                                                                         File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 133, in testMalformedAntecedent
                                                                                                                                                           self.assertRaises(roman3.InvalidRomanNumeralError, roman3.fromRoman, s)
                                                                                                                                                         File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                           raise self.failureException, excName
                                                                                                                                                      -AssertionError: InvalidRomanNumeralError
                                                                                                                                                      +AssertionError: InvalidRomanNumeralError
                                                                                                                                                       ======================================================================
                                                                                                                                                       FAIL: fromRoman should fail with repeated pairs of numerals
                                                                                                                                                       ----------------------------------------------------------------------
                                                                                                                                                      -Traceback (most recent call last):
                                                                                                                                                      +Traceback (most recent call last):
                                                                                                                                                         File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 127, in testRepeatedPairs
                                                                                                                                                           self.assertRaises(roman3.InvalidRomanNumeralError, roman3.fromRoman, s)
                                                                                                                                                         File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                           raise self.failureException, excName
                                                                                                                                                      -AssertionError: InvalidRomanNumeralError
                                                                                                                                                      +AssertionError: InvalidRomanNumeralError
                                                                                                                                                       ======================================================================
                                                                                                                                                       FAIL: fromRoman should fail with too many repeated numerals
                                                                                                                                                       ----------------------------------------------------------------------
                                                                                                                                                      -Traceback (most recent call last):
                                                                                                                                                      +Traceback (most recent call last):
                                                                                                                                                         File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 122, in testTooManyRepeatedNumerals
                                                                                                                                                           self.assertRaises(roman3.InvalidRomanNumeralError, roman3.fromRoman, s)
                                                                                                                                                         File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                           raise self.failureException, excName
                                                                                                                                                      -AssertionError: InvalidRomanNumeralError
                                                                                                                                                      +AssertionError: InvalidRomanNumeralError
                                                                                                                                                       ======================================================================
                                                                                                                                                       FAIL: fromRoman should give known result with known input
                                                                                                                                                       ----------------------------------------------------------------------
                                                                                                                                                      -Traceback (most recent call last):
                                                                                                                                                      +Traceback (most recent call last):
                                                                                                                                                         File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 99, in testFromRomanKnownValues
                                                                                                                                                           self.assertEqual(integer, result)
                                                                                                                                                         File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual
                                                                                                                                                           raise self.failureException, (msg or '%s != %s' % (first, second))
                                                                                                                                                      -AssertionError: 1 != None
                                                                                                                                                      +AssertionError: 1 != None
                                                                                                                                                       ======================================================================
                                                                                                                                                       FAIL: fromRoman(toRoman(n))==n for all n
                                                                                                                                                       ----------------------------------------------------------------------
                                                                                                                                                      -Traceback (most recent call last):
                                                                                                                                                      +Traceback (most recent call last):
                                                                                                                                                         File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 141, in testSanity
                                                                                                                                                           self.assertEqual(integer, result)
                                                                                                                                                         File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual
                                                                                                                                                           raise self.failureException, (msg or '%s != %s' % (first, second))
                                                                                                                                                      -AssertionError: 1 != None
                                                                                                                                                      +AssertionError: 1 != None
                                                                                                                                                       ----------------------------------------------------------------------
                                                                                                                                                       Ran 12 tests in 0.401s
                                                                                                                                                       
                                                                                                                                                      -FAILED (failures=6) 1
                                                                                                                                                      - - - - - -
                                                                                                                                                      1 -You're down to 6 failures, and all of them involve fromRoman: the known values test, the three separate bad input tests, the case check, and the sanity check. That means that toRoman has passed all the tests it can pass by itself. (It's involved in the sanity check, but that also requires that fromRoman be written, which it isn't yet.) Which means that you must stop coding toRoman now. No tweaking, no twiddling, no extra checks “just in case”. Stop. Now. Back away from the keyboard. -
                                                                                                                                                      -
                                                                                                                                                      - - - - -
                                                                                                                                                      Note
                                                                                                                                                      The most important thing that comprehensive unit testing can tell you is when to stop coding. When all the unit tests for +FAILED (failures=6)
                                                                                                                                                      +
                                                                                                                                                        +
                                                                                                                                                      1. You're down to 6 failures, and all of them involve fromRoman: the known values test, the three separate bad input tests, the case check, and the sanity check. That means that toRoman has passed all the tests it can pass by itself. (It's involved in the sanity check, but that also requires that fromRoman be written, which it isn't yet.) Which means that you must stop coding toRoman now. No tweaking, no twiddling, no extra checks “just in case”. Stop. Now. Back away from the keyboard. + + + - -
                                                                                                                                                        NoteThe most important thing that comprehensive unit testing can tell you is when to stop coding. When all the unit tests for a function pass, stop coding the function. When all the unit tests for an entire module pass, stop coding the module. -

                                                                                                                                                        14.4. roman.py, stage 4

                                                                                                                                                        Now that toRoman is done, it's time to start coding fromRoman. Thanks to the rich data structure that maps individual Roman numerals to integer values, this is no more difficult than the toRoman function. -

                                                                                                                                                        Example 14.9. roman4.py

                                                                                                                                                        +

                                                                                                                                                        Example 14.9. roman4.py

                                                                                                                                                        This file is available in py/roman/stage4/ in the examples directory. -

                                                                                                                                                        If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                        +

                                                                                                                                                        If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                        
                                                                                                                                                         """Convert to and from Roman numerals"""
                                                                                                                                                         
                                                                                                                                                         #Define exceptions
                                                                                                                                                        @@ -11728,99 +7447,82 @@ def fromRoman(s):
                                                                                                                                                             result = 0
                                                                                                                                                             index = 0
                                                                                                                                                             for numeral, integer in romanNumeralMap:
                                                                                                                                                        -        while s[index:index+len(numeral)] == numeral: 1
                                                                                                                                                        +        while s[index:index+len(numeral)] == numeral: 
                                                                                                                                                                     result += integer
                                                                                                                                                                     index += len(numeral)
                                                                                                                                                             return result
                                                                                                                                                        -
                                                                                                                                                        - - - - - -
                                                                                                                                                        1 -The pattern here is the same as toRoman. You iterate through your Roman numeral data structure (a tuple of tuples), and instead of matching the highest integer +
                                                                                                                                                        +
                                                                                                                                                          +
                                                                                                                                                        1. The pattern here is the same as toRoman. You iterate through your Roman numeral data structure (a tuple of tuples), and instead of matching the highest integer values as often as possible, you match the “highest” Roman numeral character strings as often as possible. -
                                                                                                                                                        -

                                                                                                                                                        Example 14.10. How fromRoman works

                                                                                                                                                        -

                                                                                                                                                        If you're not clear how fromRoman works, add a print statement to the end of the while loop:

                                                                                                                                                        +

                                                                                                                                                        Example 14.10. How fromRoman works

                                                                                                                                                        +

                                                                                                                                                        If you're not clear how fromRoman works, add a print statement to the end of the while loop:

                                                                                                                                                        
                                                                                                                                                                 while s[index:index+len(numeral)] == numeral:
                                                                                                                                                                     result += integer
                                                                                                                                                                     index += len(numeral)
                                                                                                                                                        -            print 'found', numeral, 'of length', len(numeral), ', adding', integer
                                                                                                                                                        ->>> import roman4
                                                                                                                                                        ->>> roman4.fromRoman('MCMLXXII')
                                                                                                                                                        -found M , of length 1, adding 1000
                                                                                                                                                        +            print 'found', numeral, 'of length', len(numeral), ', adding', integer
                                                                                                                                                        +>>> import roman4
                                                                                                                                                        +>>> roman4.fromRoman('MCMLXXII')
                                                                                                                                                        +found M , of length 1, adding 1000
                                                                                                                                                         found CM , of length 2, adding 900
                                                                                                                                                         found L , of length 1, adding 50
                                                                                                                                                         found X , of length 1, adding 10
                                                                                                                                                         found X , of length 1, adding 10
                                                                                                                                                         found I , of length 1, adding 1
                                                                                                                                                         found I , of length 1, adding 1
                                                                                                                                                        -1972

                                                                                                                                                        Example 14.11. Output of romantest4.py against roman4.py

                                                                                                                                                        fromRoman should only accept uppercase input ... FAIL
                                                                                                                                                        +1972

                                                                                                                                                        Example 14.11. Output of romantest4.py against roman4.py

                                                                                                                                                        fromRoman should only accept uppercase input ... FAIL
                                                                                                                                                         toRoman should always return uppercase ... ok
                                                                                                                                                         fromRoman should fail with malformed antecedents ... FAIL
                                                                                                                                                         fromRoman should fail with repeated pairs of numerals ... FAIL
                                                                                                                                                         fromRoman should fail with too many repeated numerals ... FAIL
                                                                                                                                                        -fromRoman should give known result with known input ... ok 1
                                                                                                                                                        +fromRoman should give known result with known input ... ok 
                                                                                                                                                         toRoman should give known result with known input ... ok
                                                                                                                                                        -fromRoman(toRoman(n))==n for all n ... ok2
                                                                                                                                                        +fromRoman(toRoman(n))==n for all n ... ok
                                                                                                                                                         toRoman should fail with non-integer input ... ok
                                                                                                                                                         toRoman should fail with negative input ... ok
                                                                                                                                                         toRoman should fail with large input ... ok
                                                                                                                                                        -toRoman should fail with 0 input ... ok
                                                                                                                                                        - - - - - - - - - -
                                                                                                                                                        1 -Two pieces of exciting news here. The first is that fromRoman works for good input, at least for all the known values you test. -
                                                                                                                                                        2 -The second is that the sanity check also passed. Combined with the known values tests, you can be reasonably sure that both toRoman and fromRoman work properly for all possible good values. (This is not guaranteed; it is theoretically possible that toRoman has a bug that produces the wrong Roman numeral for some particular set of inputs, and that fromRoman has a reciprocal bug that produces the same wrong integer values for exactly that set of Roman numerals that toRoman generated incorrectly. Depending on your application and your requirements, this possibility may bother you; if so, write +toRoman should fail with 0 input ... ok
                                                                                                                                                        +
                                                                                                                                                          +
                                                                                                                                                        1. Two pieces of exciting news here. The first is that fromRoman works for good input, at least for all the known values you test. +
                                                                                                                                                        2. The second is that the sanity check also passed. Combined with the known values tests, you can be reasonably sure that both toRoman and fromRoman work properly for all possible good values. (This is not guaranteed; it is theoretically possible that toRoman has a bug that produces the wrong Roman numeral for some particular set of inputs, and that fromRoman has a reciprocal bug that produces the same wrong integer values for exactly that set of Roman numerals that toRoman generated incorrectly. Depending on your application and your requirements, this possibility may bother you; if so, write more comprehensive test cases until it doesn't bother you.) -
                                                                                                                                                        -
                                                                                                                                                        
                                                                                                                                                        +
                                                                                                                                                        
                                                                                                                                                         ======================================================================
                                                                                                                                                         FAIL: fromRoman should only accept uppercase input
                                                                                                                                                         ----------------------------------------------------------------------
                                                                                                                                                        -Traceback (most recent call last):
                                                                                                                                                        +Traceback (most recent call last):
                                                                                                                                                           File "C:\docbook\dip\py\roman\stage4\romantest4.py", line 156, in testFromRomanCase
                                                                                                                                                             roman4.fromRoman, numeral.lower())
                                                                                                                                                           File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                             raise self.failureException, excName
                                                                                                                                                        -AssertionError: InvalidRomanNumeralError
                                                                                                                                                        +AssertionError: InvalidRomanNumeralError
                                                                                                                                                         ======================================================================
                                                                                                                                                         FAIL: fromRoman should fail with malformed antecedents
                                                                                                                                                         ----------------------------------------------------------------------
                                                                                                                                                        -Traceback (most recent call last):
                                                                                                                                                        +Traceback (most recent call last):
                                                                                                                                                           File "C:\docbook\dip\py\roman\stage4\romantest4.py", line 133, in testMalformedAntecedent
                                                                                                                                                             self.assertRaises(roman4.InvalidRomanNumeralError, roman4.fromRoman, s)
                                                                                                                                                           File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                             raise self.failureException, excName
                                                                                                                                                        -AssertionError: InvalidRomanNumeralError
                                                                                                                                                        +AssertionError: InvalidRomanNumeralError
                                                                                                                                                         ======================================================================
                                                                                                                                                         FAIL: fromRoman should fail with repeated pairs of numerals
                                                                                                                                                         ----------------------------------------------------------------------
                                                                                                                                                        -Traceback (most recent call last):
                                                                                                                                                        +Traceback (most recent call last):
                                                                                                                                                           File "C:\docbook\dip\py\roman\stage4\romantest4.py", line 127, in testRepeatedPairs
                                                                                                                                                             self.assertRaises(roman4.InvalidRomanNumeralError, roman4.fromRoman, s)
                                                                                                                                                           File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                             raise self.failureException, excName
                                                                                                                                                        -AssertionError: InvalidRomanNumeralError
                                                                                                                                                        +AssertionError: InvalidRomanNumeralError
                                                                                                                                                         ======================================================================
                                                                                                                                                         FAIL: fromRoman should fail with too many repeated numerals
                                                                                                                                                         ----------------------------------------------------------------------
                                                                                                                                                        -Traceback (most recent call last):
                                                                                                                                                        +Traceback (most recent call last):
                                                                                                                                                           File "C:\docbook\dip\py\roman\stage4\romantest4.py", line 122, in testTooManyRepeatedNumerals
                                                                                                                                                             self.assertRaises(roman4.InvalidRomanNumeralError, roman4.fromRoman, s)
                                                                                                                                                           File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                             raise self.failureException, excName
                                                                                                                                                        -AssertionError: InvalidRomanNumeralError
                                                                                                                                                        +AssertionError: InvalidRomanNumeralError
                                                                                                                                                         ----------------------------------------------------------------------
                                                                                                                                                         Ran 12 tests in 1.222s
                                                                                                                                                         
                                                                                                                                                        @@ -11830,7 +7532,7 @@ FAILED (failures=4)

                                                                                                                                                        14.5. roman.pyvalidating numeric input in toRoman, but you have a powerful tool at your disposal: regular expressions.

                                                                                                                                                        If you're not familiar with regular expressions and didn't read Chapter 7, Regular Expressions, now would be a good time.

                                                                                                                                                        As you saw in Section 7.3, “Case Study: Roman Numerals”, there are several simple rules for constructing a Roman numeral, using the letters M, D, C, L, X, V, and I. Let's review the rules: -

                                                                                                                                                        +
                                                                                                                                                        1. Characters are additive. I is 1, II is 2, and III is 3. VI is 6 (literally, “5 and 1”), VII is 7, and VIII is 8. @@ -11843,9 +7545,10 @@ FAILED (failures=4)

                                                                                                                                                        14.5. roman.pyRoman numerals are always written highest to lowest, and read left to right, so order of characters matters very much. DC is 600; CD is a completely different number (400, “100 less than 500”). CI is 101; IC is not even a valid Roman numeral (because you can't subtract 1 directly from 100; you would need to write it as XCIX, “10 less than 100, then 1 less than 10”).

                                                                                                                                                      -

                                                                                                                                                      Example 14.12. roman5.py

                                                                                                                                                      +

                                                                                                                                                      Example 14.12. roman5.py

                                                                                                                                                      This file is available in py/roman/stage5/ in the examples directory. -

                                                                                                                                                      If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                      +

                                                                                                                                                      If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                      
                                                                                                                                                       """Convert to and from Roman numerals"""
                                                                                                                                                       import re
                                                                                                                                                       
                                                                                                                                                      @@ -11885,11 +7588,11 @@ def toRoman(n):
                                                                                                                                                           return result
                                                                                                                                                       
                                                                                                                                                       #Define pattern to detect valid Roman numerals
                                                                                                                                                      -romanNumeralPattern = '^M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$' 1
                                                                                                                                                      +romanNumeralPattern = '^M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$' 
                                                                                                                                                       
                                                                                                                                                       def fromRoman(s):
                                                                                                                                                           """convert Roman numeral to integer"""
                                                                                                                                                      -    if not re.search(romanNumeralPattern, s):2
                                                                                                                                                      +    if not re.search(romanNumeralPattern, s):
                                                                                                                                                               raise InvalidRomanNumeralError, 'Invalid Roman numeral: %s' % s
                                                                                                                                                       
                                                                                                                                                           result = 0
                                                                                                                                                      @@ -11899,29 +7602,18 @@ def fromRoman(s):
                                                                                                                                                                   result += integer
                                                                                                                                                                   index += len(numeral)
                                                                                                                                                           return result
                                                                                                                                                      -
                                                                                                                                                      - - - - - - - - - -
                                                                                                                                                      1 -This is just a continuation of the pattern you discussed in Section 7.3, “Case Study: Roman Numerals”. The tens places is either XC (90), XL (40), or an optional L followed by 0 to 3 optional X characters. The ones place is either IX (9), IV (4), or an optional V followed by 0 to 3 optional I characters. -
                                                                                                                                                      2 -Having encoded all that logic into a regular expression, the code to check for invalid Roman numerals becomes trivial. If +
                                                                                                                                                      +
                                                                                                                                                        +
                                                                                                                                                      1. This is just a continuation of the pattern you discussed in Section 7.3, “Case Study: Roman Numerals”. The tens places is either XC (90), XL (40), or an optional L followed by 0 to 3 optional X characters. The ones place is either IX (9), IV (4), or an optional V followed by 0 to 3 optional I characters. +
                                                                                                                                                      2. Having encoded all that logic into a regular expression, the code to check for invalid Roman numerals becomes trivial. If re.search returns an object, then the regular expression matched and the input is valid; otherwise, the input is invalid. -

                                                                                                                                                      At this point, you are allowed to be skeptical that that big ugly regular expression could possibly catch all the types of invalid Roman numerals. But don't take my word for it, look at the results: -

                                                                                                                                                      Example 14.13. Output of romantest5.py against roman5.py

                                                                                                                                                      
                                                                                                                                                      -fromRoman should only accept uppercase input ... ok          1
                                                                                                                                                      +

                                                                                                                                                      Example 14.13. Output of romantest5.py against roman5.py

                                                                                                                                                      
                                                                                                                                                      +fromRoman should only accept uppercase input ... ok          
                                                                                                                                                       toRoman should always return uppercase ... ok
                                                                                                                                                      -fromRoman should fail with malformed antecedents ... ok      2
                                                                                                                                                      -fromRoman should fail with repeated pairs of numerals ... ok 3
                                                                                                                                                      +fromRoman should fail with malformed antecedents ... ok      
                                                                                                                                                      +fromRoman should fail with repeated pairs of numerals ... ok 
                                                                                                                                                       fromRoman should fail with too many repeated numerals ... ok
                                                                                                                                                       fromRoman should give known result with known input ... ok
                                                                                                                                                       toRoman should give known result with known input ... ok
                                                                                                                                                      @@ -11934,80 +7626,42 @@ toRoman should fail with 0 input ... ok
                                                                                                                                                       ----------------------------------------------------------------------
                                                                                                                                                       Ran 12 tests in 2.864s
                                                                                                                                                       
                                                                                                                                                      -OK     4
                                                                                                                                                      - - - - - - - - - - - - - - - - - -
                                                                                                                                                      1 -One thing I didn't mention about regular expressions is that, by default, they are case-sensitive. Since the regular expression +OK
                                                                                                                                                      +
                                                                                                                                                        +
                                                                                                                                                      1. One thing I didn't mention about regular expressions is that, by default, they are case-sensitive. Since the regular expression romanNumeralPattern was expressed in uppercase characters, the re.search check will reject any input that isn't completely uppercase. So the uppercase input test passes. -
                                                                                                                                                      2 -More importantly, the bad input tests pass. For instance, the malformed antecedents test checks cases like MCMC. As you've seen, this does not match the regular expression, so fromRoman raises an InvalidRomanNumeralError exception, which is what the malformed antecedents test case is looking for, so the test passes. -
                                                                                                                                                      3 -In fact, all the bad input tests pass. This regular expression catches everything you could think of when you made your test +
                                                                                                                                                    4. More importantly, the bad input tests pass. For instance, the malformed antecedents test checks cases like MCMC. As you've seen, this does not match the regular expression, so fromRoman raises an InvalidRomanNumeralError exception, which is what the malformed antecedents test case is looking for, so the test passes. +
                                                                                                                                                    5. In fact, all the bad input tests pass. This regular expression catches everything you could think of when you made your test cases. -
                                                                                                                                                    6. 4 -And the anticlimax award of the year goes to the word “OK”, which is printed by the unittest module when all the tests pass. -
                                                                                                                                                      -
                                                                                                                                                      - - - - - - -
                                                                                                                                                      Note
                                                                                                                                                      When all of your tests pass, stop coding.
                                                                                                                                                      -
                                                                                                                                                      +
                                                                                                                                                    7. And the anticlimax award of the year goes to the word “OK”, which is printed by the unittest module when all the tests pass. + + +
                                                                                                                                                      NoteWhen all of your tests pass, stop coding. +

                                                                                                                                                      Chapter 15. Refactoring

                                                                                                                                                      15.1. Handling bugs

                                                                                                                                                      Despite your best efforts to write comprehensive unit tests, bugs happen. What do I mean by “bug”? A bug is a test case you haven't written yet. -

                                                                                                                                                      Example 15.1. The bug

                                                                                                                                                      >>> import roman5
                                                                                                                                                      ->>> roman5.fromRoman("") 1
                                                                                                                                                      -0
                                                                                                                                                      - - - - - -
                                                                                                                                                      1 -Remember in the previous section when you kept seeing that an empty string would match the regular expression you were using to check for valid Roman numerals? +

                                                                                                                                                      Example 15.1. The bug

                                                                                                                                                      >>> import roman5
                                                                                                                                                      +>>> roman5.fromRoman("") 
                                                                                                                                                      +0
                                                                                                                                                      +
                                                                                                                                                        +
                                                                                                                                                      1. Remember in the previous section when you kept seeing that an empty string would match the regular expression you were using to check for valid Roman numerals? Well, it turns out that this is still true for the final version of the regular expression. And that's a bug; you want an empty string to raise an InvalidRomanNumeralError exception just like any other sequence of characters that don't represent a valid Roman numeral. -

                                                                                                                                                      After reproducing the bug, and before fixing it, you should write a test case that fails, thus illustrating the bug. -

                                                                                                                                                      Example 15.2. Testing for the bug (romantest61.py)

                                                                                                                                                      +

                                                                                                                                                      Example 15.2. Testing for the bug (romantest61.py)

                                                                                                                                                      
                                                                                                                                                       class FromRomanBadInput(unittest.TestCase):  
                                                                                                                                                       
                                                                                                                                                           # previous test cases omitted for clarity (they haven't changed)
                                                                                                                                                       
                                                                                                                                                           def testBlank(self):
                                                                                                                                                               """fromRoman should fail with blank string"""
                                                                                                                                                      -        self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, "") 1
                                                                                                                                                      -
                                                                                                                                                      - - - - - -
                                                                                                                                                      1 -Pretty simple stuff here. Call fromRoman with an empty string and make sure it raises an InvalidRomanNumeralError exception. The hard part was finding the bug; now that you know about it, testing for it is the easy part. -
                                                                                                                                                      + self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, "") +
                                                                                                                                                      +
                                                                                                                                                        +
                                                                                                                                                      1. Pretty simple stuff here. Call fromRoman with an empty string and make sure it raises an InvalidRomanNumeralError exception. The hard part was finding the bug; now that you know about it, testing for it is the easy part.

                                                                                                                                                        Since your code has a bug, and you now have a test case that tests this bug, the test case will fail: -

                                                                                                                                                        Example 15.3. Output of romantest61.py against roman61.py

                                                                                                                                                        fromRoman should only accept uppercase input ... ok
                                                                                                                                                        +

                                                                                                                                                        Example 15.3. Output of romantest61.py against roman61.py

                                                                                                                                                        fromRoman should only accept uppercase input ... ok
                                                                                                                                                         toRoman should always return uppercase ... ok
                                                                                                                                                         fromRoman should fail with blank string ... FAIL
                                                                                                                                                         fromRoman should fail with malformed antecedents ... ok
                                                                                                                                                        @@ -12024,21 +7678,22 @@ toRoman should fail with 0 input ... ok
                                                                                                                                                         ======================================================================
                                                                                                                                                         FAIL: fromRoman should fail with blank string
                                                                                                                                                         ----------------------------------------------------------------------
                                                                                                                                                        -Traceback (most recent call last):
                                                                                                                                                        +Traceback (most recent call last):
                                                                                                                                                           File "C:\docbook\dip\py\roman\stage6\romantest61.py", line 137, in testBlank
                                                                                                                                                             self.assertRaises(roman61.InvalidRomanNumeralError, roman61.fromRoman, "")
                                                                                                                                                           File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises
                                                                                                                                                             raise self.failureException, excName
                                                                                                                                                        -AssertionError: InvalidRomanNumeralError
                                                                                                                                                        +AssertionError: InvalidRomanNumeralError
                                                                                                                                                         ----------------------------------------------------------------------
                                                                                                                                                         Ran 13 tests in 2.864s
                                                                                                                                                         
                                                                                                                                                         FAILED (failures=1)

                                                                                                                                                        Now you can fix the bug. -

                                                                                                                                                        Example 15.4. Fixing the bug (roman62.py)

                                                                                                                                                        -

                                                                                                                                                        This file is available in py/roman/stage6/ in the examples directory.

                                                                                                                                                        +

                                                                                                                                                        Example 15.4. Fixing the bug (roman62.py)

                                                                                                                                                        +

                                                                                                                                                        This file is available in py/roman/stage6/ in the examples directory. +

                                                                                                                                                        
                                                                                                                                                         def fromRoman(s):
                                                                                                                                                             """convert Roman numeral to integer"""
                                                                                                                                                        -    if not s: 1
                                                                                                                                                        +    if not s: 
                                                                                                                                                                 raise InvalidRomanNumeralError, 'Input can not be blank'
                                                                                                                                                             if not re.search(romanNumeralPattern, s):
                                                                                                                                                                 raise InvalidRomanNumeralError, 'Invalid Roman numeral: %s' % s
                                                                                                                                                        @@ -12050,18 +7705,12 @@ def fromRoman(s):
                                                                                                                                                                     result += integer
                                                                                                                                                                     index += len(numeral)
                                                                                                                                                             return result
                                                                                                                                                        -
                                                                                                                                                        - - - - - -
                                                                                                                                                        1 -Only two lines of code are required: an explicit check for an empty string, and a raise statement. -
                                                                                                                                                        -

                                                                                                                                                        Example 15.5. Output of romantest62.py against roman62.py

                                                                                                                                                        fromRoman should only accept uppercase input ... ok
                                                                                                                                                        +
                                                                                                                                                        +
                                                                                                                                                          +
                                                                                                                                                        1. Only two lines of code are required: an explicit check for an empty string, and a raise statement. +

                                                                                                                                                          Example 15.5. Output of romantest62.py against roman62.py

                                                                                                                                                          fromRoman should only accept uppercase input ... ok
                                                                                                                                                           toRoman should always return uppercase ... ok
                                                                                                                                                          -fromRoman should fail with blank string ... ok 1
                                                                                                                                                          +fromRoman should fail with blank string ... ok 
                                                                                                                                                           fromRoman should fail with malformed antecedents ... ok
                                                                                                                                                           fromRoman should fail with repeated pairs of numerals ... ok
                                                                                                                                                           fromRoman should fail with too many repeated numerals ... ok
                                                                                                                                                          @@ -12076,19 +7725,10 @@ toRoman should fail with 0 input ... ok
                                                                                                                                                           ----------------------------------------------------------------------
                                                                                                                                                           Ran 13 tests in 2.834s
                                                                                                                                                           
                                                                                                                                                          -OK 2
                                                                                                                                                          - - - - - - - - - -
                                                                                                                                                          1 -The blank string test case now passes, so the bug is fixed.
                                                                                                                                                          2 -All the other test cases still pass, which means that this bug fix didn't break anything else. Stop coding.
                                                                                                                                                          +OK
                                                                                                                                                        +
                                                                                                                                                          +
                                                                                                                                                        1. The blank string test case now passes, so the bug is fixed. +
                                                                                                                                                        2. All the other test cases still pass, which means that this bug fix didn't break anything else. Stop coding.

                                                                                                                                                          Coding this way does not make fixing bugs any easier. Simple bugs (like this one) require simple test cases; complex bugs will require complex test cases. In a testing-centric environment, it may seem like it takes longer to fix a bug, since you need to articulate in code exactly what the bug is (to write the test case), then fix the bug itself. Then if the test case doesn't pass right away, you need to figure out whether the fix was wrong, @@ -12103,9 +7743,10 @@ is tomorrow's regression test. if they do, they'll want more in the next release anyway. So be prepared to update your test cases as requirements change.

                                                                                                                                                          Suppose, for instance, that you wanted to expand the range of the Roman numeral conversion functions. Remember the rule that said that no character could be repeated more than three times? Well, the Romans were willing to make an exception to that rule by having 4 M characters in a row to represent 4000. If you make this change, you'll be able to expand the range of convertible numbers from 1..3999 to 1..4999. But first, you need to make some changes to the test cases. -

                                                                                                                                                          Example 15.6. Modifying test cases for new requirements (romantest71.py)

                                                                                                                                                          +

                                                                                                                                                          Example 15.6. Modifying test cases for new requirements (romantest71.py)

                                                                                                                                                          This file is available in py/roman/stage7/ in the examples directory. -

                                                                                                                                                          If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                          +

                                                                                                                                                          If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                          
                                                                                                                                                           import roman71
                                                                                                                                                           import unittest
                                                                                                                                                           
                                                                                                                                                          @@ -12166,7 +7807,7 @@ class KnownValues(unittest.TestCase):
                                                                                                                                                             (3888, 'MMMDCCCLXXXVIII'),
                                                                                                                                                             (3940, 'MMMCMXL'),
                                                                                                                                                             (3999, 'MMMCMXCIX'),
                                                                                                                                                          -  (4000, 'MMMM'),   1
                                                                                                                                                          +  (4000, 'MMMM'),   
                                                                                                                                                             (4500, 'MMMMD'),
                                                                                                                                                             (4888, 'MMMMDCCCLXXXVIII'),
                                                                                                                                                             (4999, 'MMMMCMXCIX'))
                                                                                                                                                          @@ -12186,7 +7827,7 @@ class KnownValues(unittest.TestCase):
                                                                                                                                                           class ToRomanBadInput(unittest.TestCase):
                                                                                                                                                               def testTooLarge(self):
                                                                                                                                                                   """toRoman should fail with large input"""
                                                                                                                                                          -        self.assertRaises(roman71.OutOfRangeError, roman71.toRoman, 5000) 2
                                                                                                                                                          +        self.assertRaises(roman71.OutOfRangeError, roman71.toRoman, 5000) 
                                                                                                                                                           
                                                                                                                                                               def testZero(self):
                                                                                                                                                                   """toRoman should fail with 0 input"""
                                                                                                                                                          @@ -12203,7 +7844,7 @@ class ToRomanBadInput(unittest.TestCase):
                                                                                                                                                           class FromRomanBadInput(unittest.TestCase):
                                                                                                                                                               def testTooManyRepeatedNumerals(self):
                                                                                                                                                                   """fromRoman should fail with too many repeated numerals"""
                                                                                                                                                          -        for s in ('MMMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'):     3
                                                                                                                                                          +        for s in ('MMMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'):     
                                                                                                                                                                       self.assertRaises(roman71.InvalidRomanNumeralError, roman71.fromRoman, s)
                                                                                                                                                           
                                                                                                                                                               def testRepeatedPairs(self):
                                                                                                                                                          @@ -12224,7 +7865,7 @@ class FromRomanBadInput(unittest.TestCase):
                                                                                                                                                           class SanityCheck(unittest.TestCase):
                                                                                                                                                               def testSanity(self):
                                                                                                                                                                   """fromRoman(toRoman(n))==n for all n"""
                                                                                                                                                          -        for integer in range(1, 5000):4
                                                                                                                                                          +        for integer in range(1, 5000):
                                                                                                                                                                       numeral = roman71.toRoman(integer)
                                                                                                                                                                       result = roman71.fromRoman(numeral)
                                                                                                                                                                       self.assertEqual(integer, result)
                                                                                                                                                          @@ -12246,123 +7887,81 @@ class CaseCheck(unittest.TestCase):
                                                                                                                                                           
                                                                                                                                                           if __name__ == "__main__":
                                                                                                                                                               unittest.main()
                                                                                                                                                          -
                                                                                                                                                          - - - - - - - - - - - - - - - - - -
                                                                                                                                                          1 -The existing known values don't change (they're all still reasonable values to test), but you need to add a few more in the +
                                                                                                                                                          +
                                                                                                                                                            +
                                                                                                                                                          1. The existing known values don't change (they're all still reasonable values to test), but you need to add a few more in the 4000 range. Here I've included 4000 (the shortest), 4500 (the second shortest), 4888 (the longest), and 4999 (the largest). -
                                                                                                                                                          2 -The definition of “large input” has changed. This test used to call toRoman with 4000 and expect an error; now that 4000-4999 are good values, you need to bump this up to 5000. -
                                                                                                                                                          3 -The definition of “too many repeated numerals” has also changed. This test used to call fromRoman with 'MMMM' and expect an error; now that MMMM is considered a valid Roman numeral, you need to bump this up to 'MMMMM'. -
                                                                                                                                                          4 -The sanity check and case checks loop through every number in the range, from 1 to 3999. Since the range has now expanded, these for loops need to be updated as well to go up to 4999. -
                                                                                                                                                          +
                                                                                                                                                        3. The definition of “large input” has changed. This test used to call toRoman with 4000 and expect an error; now that 4000-4999 are good values, you need to bump this up to 5000. +
                                                                                                                                                        4. The definition of “too many repeated numerals” has also changed. This test used to call fromRoman with 'MMMM' and expect an error; now that MMMM is considered a valid Roman numeral, you need to bump this up to 'MMMMM'. +
                                                                                                                                                        5. The sanity check and case checks loop through every number in the range, from 1 to 3999. Since the range has now expanded, these for loops need to be updated as well to go up to 4999.

                                                                                                                                                          Now your test cases are up to date with the new requirements, but your code is not, so you expect several of the test cases to fail. -

                                                                                                                                                          Example 15.7. Output of romantest71.py against roman71.py

                                                                                                                                                          
                                                                                                                                                          -fromRoman should only accept uppercase input ... ERROR        1
                                                                                                                                                          +

                                                                                                                                                          Example 15.7. Output of romantest71.py against roman71.py

                                                                                                                                                          
                                                                                                                                                          +fromRoman should only accept uppercase input ... ERROR        
                                                                                                                                                           toRoman should always return uppercase ... ERROR
                                                                                                                                                           fromRoman should fail with blank string ... ok
                                                                                                                                                           fromRoman should fail with malformed antecedents ... ok
                                                                                                                                                           fromRoman should fail with repeated pairs of numerals ... ok
                                                                                                                                                           fromRoman should fail with too many repeated numerals ... ok
                                                                                                                                                          -fromRoman should give known result with known input ... ERROR 2
                                                                                                                                                          -toRoman should give known result with known input ... ERROR   3
                                                                                                                                                          -fromRoman(toRoman(n))==n for all n ... ERROR4
                                                                                                                                                          +fromRoman should give known result with known input ... ERROR 
                                                                                                                                                          +toRoman should give known result with known input ... ERROR   
                                                                                                                                                          +fromRoman(toRoman(n))==n for all n ... ERROR
                                                                                                                                                           toRoman should fail with non-integer input ... ok
                                                                                                                                                           toRoman should fail with negative input ... ok
                                                                                                                                                           toRoman should fail with large input ... ok
                                                                                                                                                           toRoman should fail with 0 input ... ok
                                                                                                                                                          -
                                                                                                                                                          - - - - - - - - - - - - - - - - - -
                                                                                                                                                          1 -Our case checks now fail because they loop from 1 to 4999, but toRoman only accepts numbers from 1 to 3999, so it will fail as soon the test case hits 4000. -
                                                                                                                                                          2 -The fromRoman known values test will fail as soon as it hits 'MMMM', because fromRoman still thinks this is an invalid Roman numeral. -
                                                                                                                                                          3 -The toRoman known values test will fail as soon as it hits 4000, because toRoman still thinks this is out of range. -
                                                                                                                                                          4 -The sanity check will also fail as soon as it hits 4000, because toRoman still thinks this is out of range. -
                                                                                                                                                          -
                                                                                                                                                          
                                                                                                                                                          +
                                                                                                                                                          +
                                                                                                                                                            +
                                                                                                                                                          1. Our case checks now fail because they loop from 1 to 4999, but toRoman only accepts numbers from 1 to 3999, so it will fail as soon the test case hits 4000. +
                                                                                                                                                          2. The fromRoman known values test will fail as soon as it hits 'MMMM', because fromRoman still thinks this is an invalid Roman numeral. +
                                                                                                                                                          3. The toRoman known values test will fail as soon as it hits 4000, because toRoman still thinks this is out of range. +
                                                                                                                                                          4. The sanity check will also fail as soon as it hits 4000, because toRoman still thinks this is out of range. +
                                                                                                                                                            
                                                                                                                                                             ======================================================================
                                                                                                                                                             ERROR: fromRoman should only accept uppercase input
                                                                                                                                                             ----------------------------------------------------------------------
                                                                                                                                                            -Traceback (most recent call last):
                                                                                                                                                            +Traceback (most recent call last):
                                                                                                                                                               File "C:\docbook\dip\py\roman\stage7\romantest71.py", line 161, in testFromRomanCase
                                                                                                                                                                 numeral = roman71.toRoman(integer)
                                                                                                                                                               File "roman71.py", line 28, in toRoman
                                                                                                                                                                 raise OutOfRangeError, "number out of range (must be 1..3999)"
                                                                                                                                                            -OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                            +OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                             ======================================================================
                                                                                                                                                             ERROR: toRoman should always return uppercase
                                                                                                                                                             ----------------------------------------------------------------------
                                                                                                                                                            -Traceback (most recent call last):
                                                                                                                                                            +Traceback (most recent call last):
                                                                                                                                                               File "C:\docbook\dip\py\roman\stage7\romantest71.py", line 155, in testToRomanCase
                                                                                                                                                                 numeral = roman71.toRoman(integer)
                                                                                                                                                               File "roman71.py", line 28, in toRoman
                                                                                                                                                                 raise OutOfRangeError, "number out of range (must be 1..3999)"
                                                                                                                                                            -OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                            +OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                             ======================================================================
                                                                                                                                                             ERROR: fromRoman should give known result with known input
                                                                                                                                                             ----------------------------------------------------------------------
                                                                                                                                                            -Traceback (most recent call last):
                                                                                                                                                            +Traceback (most recent call last):
                                                                                                                                                               File "C:\docbook\dip\py\roman\stage7\romantest71.py", line 102, in testFromRomanKnownValues
                                                                                                                                                                 result = roman71.fromRoman(numeral)
                                                                                                                                                               File "roman71.py", line 47, in fromRoman
                                                                                                                                                                 raise InvalidRomanNumeralError, 'Invalid Roman numeral: %s' % s
                                                                                                                                                            -InvalidRomanNumeralError: Invalid Roman numeral: MMMM
                                                                                                                                                            +InvalidRomanNumeralError: Invalid Roman numeral: MMMM
                                                                                                                                                             ======================================================================
                                                                                                                                                             ERROR: toRoman should give known result with known input
                                                                                                                                                             ----------------------------------------------------------------------
                                                                                                                                                            -Traceback (most recent call last):
                                                                                                                                                            +Traceback (most recent call last):
                                                                                                                                                               File "C:\docbook\dip\py\roman\stage7\romantest71.py", line 96, in testToRomanKnownValues
                                                                                                                                                                 result = roman71.toRoman(integer)
                                                                                                                                                               File "roman71.py", line 28, in toRoman
                                                                                                                                                                 raise OutOfRangeError, "number out of range (must be 1..3999)"
                                                                                                                                                            -OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                            +OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                             ======================================================================
                                                                                                                                                             ERROR: fromRoman(toRoman(n))==n for all n
                                                                                                                                                             ----------------------------------------------------------------------
                                                                                                                                                            -Traceback (most recent call last):
                                                                                                                                                            +Traceback (most recent call last):
                                                                                                                                                               File "C:\docbook\dip\py\roman\stage7\romantest71.py", line 147, in testSanity
                                                                                                                                                                 numeral = roman71.toRoman(integer)
                                                                                                                                                               File "roman71.py", line 28, in toRoman
                                                                                                                                                                 raise OutOfRangeError, "number out of range (must be 1..3999)"
                                                                                                                                                            -OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                            +OutOfRangeError: number out of range (must be 1..3999)
                                                                                                                                                             ----------------------------------------------------------------------
                                                                                                                                                             Ran 13 tests in 2.213s
                                                                                                                                                             
                                                                                                                                                            @@ -12370,8 +7969,9 @@ FAILED (errors=5)

                                                                                                                                                            Now that you have test cases that fail due to t with the test cases. (One thing that takes some getting used to when you first start coding unit tests is that the code being tested is never “ahead” of the test cases. While it's behind, you still have some work to do, and as soon as it catches up to the test cases, you stop coding.) -

                                                                                                                                                            Example 15.8. Coding the new requirements (roman72.py)

                                                                                                                                                            -

                                                                                                                                                            This file is available in py/roman/stage7/ in the examples directory.

                                                                                                                                                            +

                                                                                                                                                            Example 15.8. Coding the new requirements (roman72.py)

                                                                                                                                                            +

                                                                                                                                                            This file is available in py/roman/stage7/ in the examples directory. +

                                                                                                                                                            
                                                                                                                                                             """Convert to and from Roman numerals"""
                                                                                                                                                             import re
                                                                                                                                                             
                                                                                                                                                            @@ -12398,7 +7998,7 @@ romanNumeralMap = (('M',  1000),
                                                                                                                                                             
                                                                                                                                                             def toRoman(n):
                                                                                                                                                                 """convert integer to Roman numeral"""
                                                                                                                                                            -    if not (0 < n < 5000):   1
                                                                                                                                                            +    if not (0 < n < 5000):   
                                                                                                                                                                     raise OutOfRangeError, "number out of range (must be 1..4999)"
                                                                                                                                                                 if int(n) <> n:
                                                                                                                                                                     raise NotIntegerError, "non-integers can not be converted"
                                                                                                                                                            @@ -12411,7 +8011,7 @@ def toRoman(n):
                                                                                                                                                                 return result
                                                                                                                                                             
                                                                                                                                                             #Define pattern to detect valid Roman numerals
                                                                                                                                                            -romanNumeralPattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$' 2
                                                                                                                                                            +romanNumeralPattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$' 
                                                                                                                                                             
                                                                                                                                                             def fromRoman(s):
                                                                                                                                                                 """convert Roman numeral to integer"""
                                                                                                                                                            @@ -12427,24 +8027,13 @@ def fromRoman(s):
                                                                                                                                                                         result += integer
                                                                                                                                                                         index += len(numeral)
                                                                                                                                                                 return result
                                                                                                                                                            -
                                                                                                                                                            - - - - - - - - - -
                                                                                                                                                            1 -toRoman only needs one small change, in the range check. Where you used to check 0 < n < 4000, you now check 0 < n < 5000. And you change the error message that you raise to reflect the new acceptable range (1..4999 instead of 1..3999). You don't need to make any changes to the rest of the function; it handles the new cases already. (It merrily adds 'M' for each thousand that it finds; given 4000, it will spit out 'MMMM'. The only reason it didn't do this before is that you explicitly stopped it with the range check.) -
                                                                                                                                                            2 -You don't need to make any changes to fromRoman at all. The only change is to romanNumeralPattern; if you look closely, you'll notice that you added another optional M in the first section of the regular expression. This will allow up to 4 M characters instead of 3, meaning you will allow the Roman numeral equivalents of 4999 instead of 3999. The actual fromRoman function is completely general; it just looks for repeated Roman numeral characters and adds them up, without caring how +
                                                                                                                                                            +
                                                                                                                                                              +
                                                                                                                                                            1. toRoman only needs one small change, in the range check. Where you used to check 0 < n < 4000, you now check 0 < n < 5000. And you change the error message that you raise to reflect the new acceptable range (1..4999 instead of 1..3999). You don't need to make any changes to the rest of the function; it handles the new cases already. (It merrily adds 'M' for each thousand that it finds; given 4000, it will spit out 'MMMM'. The only reason it didn't do this before is that you explicitly stopped it with the range check.) +
                                                                                                                                                            2. You don't need to make any changes to fromRoman at all. The only change is to romanNumeralPattern; if you look closely, you'll notice that you added another optional M in the first section of the regular expression. This will allow up to 4 M characters instead of 3, meaning you will allow the Roman numeral equivalents of 4999 instead of 3999. The actual fromRoman function is completely general; it just looks for repeated Roman numeral characters and adds them up, without caring how many times they repeat. The only reason it didn't handle 'MMMM' before is that you explicitly stopped it with the regular expression pattern matching. -

                                                                                                                                                            You may be skeptical that these two small changes are all that you need. Hey, don't take my word for it; see for yourself: -

                                                                                                                                                            Example 15.9. Output of romantest72.py against roman72.py

                                                                                                                                                            fromRoman should only accept uppercase input ... ok
                                                                                                                                                            +

                                                                                                                                                            Example 15.9. Output of romantest72.py against roman72.py

                                                                                                                                                            fromRoman should only accept uppercase input ... ok
                                                                                                                                                             toRoman should always return uppercase ... ok
                                                                                                                                                             fromRoman should fail with blank string ... ok
                                                                                                                                                             fromRoman should fail with malformed antecedents ... ok
                                                                                                                                                            @@ -12461,14 +8050,9 @@ toRoman should fail with 0 input ... ok
                                                                                                                                                             ----------------------------------------------------------------------
                                                                                                                                                             Ran 13 tests in 3.685s
                                                                                                                                                             
                                                                                                                                                            -OK 1
                                                                                                                                                            - - - - - -
                                                                                                                                                            1 -All the test cases pass. Stop coding.
                                                                                                                                                            +OK
                                                                                                                                                            +
                                                                                                                                                              +
                                                                                                                                                            1. All the test cases pass. Stop coding.

                                                                                                                                                              Comprehensive unit testing means never having to rely on a programmer who says “Trust me.”

                                                                                                                                                              15.3. Refactoring

                                                                                                                                                              The best thing about comprehensive unit testing is not the feeling you get when all your test cases finally pass, or even @@ -12478,69 +8062,43 @@ program.

                                                                                                                                                              Here, “better” means “faster”. Specifically, the fromRoman function is slower than it needs to be, because of that big nasty regular expression that you use to validate Roman numerals. It's probably not worth trying to do away with the regular expression altogether (it would be difficult, and it might not end up any faster), but you can speed up the function by precompiling the regular expression. -

                                                                                                                                                              Example 15.10. Compiling regular expressions

                                                                                                                                                              ->>> import re
                                                                                                                                                              ->>> pattern = '^M?M?M?$'
                                                                                                                                                              ->>> re.search(pattern, 'M')               1
                                                                                                                                                              +

                                                                                                                                                              Example 15.10. Compiling regular expressions

                                                                                                                                                              +>>> import re
                                                                                                                                                              +>>> pattern = '^M?M?M?$'
                                                                                                                                                              +>>> re.search(pattern, 'M')               
                                                                                                                                                               <SRE_Match object at 01090490>
                                                                                                                                                              ->>> compiledPattern = re.compile(pattern) 2
                                                                                                                                                              ->>> compiledPattern
                                                                                                                                                              +>>> compiledPattern = re.compile(pattern) 
                                                                                                                                                              +>>> compiledPattern
                                                                                                                                                               <SRE_Pattern object at 00F06E28>
                                                                                                                                                              ->>> dir(compiledPattern)3
                                                                                                                                                              +>>> dir(compiledPattern)
                                                                                                                                                               ['findall', 'match', 'scanner', 'search', 'split', 'sub', 'subn']
                                                                                                                                                              ->>> compiledPattern.search('M')           4
                                                                                                                                                              -<SRE_Match object at 01104928>
                                                                                                                                                              - - - - - - - - - - - - - - - - - -
                                                                                                                                                              1 -This is the syntax you've seen before: re.search takes a regular expression as a string (pattern) and a string to match against it ('M'). If the pattern matches, the function returns a match object which can be queried to find out exactly what matched and +>>> compiledPattern.search('M') +<SRE_Match object at 01104928>
                                                                                                                                                              +
                                                                                                                                                                +
                                                                                                                                                              1. This is the syntax you've seen before: re.search takes a regular expression as a string (pattern) and a string to match against it ('M'). If the pattern matches, the function returns a match object which can be queried to find out exactly what matched and how. -
                                                                                                                                                              2 -This is the new syntax: re.compile takes a regular expression as a string and returns a pattern object. Note there is no string to match here. Compiling a +
                                                                                                                                                            2. This is the new syntax: re.compile takes a regular expression as a string and returns a pattern object. Note there is no string to match here. Compiling a regular expression has nothing to do with matching it against any specific strings (like 'M'); it only involves the regular expression itself. -
                                                                                                                                                            3. 3 -The compiled pattern object returned from re.compile has several useful-looking functions, including several (like search and sub) that are available directly in the re module. -
                                                                                                                                                              4 -Calling the compiled pattern object's search function with the string 'M' accomplishes the same thing as calling re.search with both the regular expression and the string 'M'. Only much, much faster. (In fact, the re.search function simply compiles the regular expression and calls the resulting pattern object's search method for you.) -
                                                                                                                                                              -
                                                                                                                                                              - - - - -
                                                                                                                                                              Note
                                                                                                                                                              Whenever you are going to use a regular expression more than once, you should compile it to get a pattern object, then call +
                                                                                                                                                            4. The compiled pattern object returned from re.compile has several useful-looking functions, including several (like search and sub) that are available directly in the re module. +
                                                                                                                                                            5. Calling the compiled pattern object's search function with the string 'M' accomplishes the same thing as calling re.search with both the regular expression and the string 'M'. Only much, much faster. (In fact, the re.search function simply compiles the regular expression and calls the resulting pattern object's search method for you.) + + + - -
                                                                                                                                                              NoteWhenever you are going to use a regular expression more than once, you should compile it to get a pattern object, then call the methods on the pattern object directly. -
                                                                                                                                                              -

                                                                                                                                                              Example 15.11. Compiled regular expressions in roman81.py

                                                                                                                                                              +

                                                                                                                                                              Example 15.11. Compiled regular expressions in roman81.py

                                                                                                                                                              This file is available in py/roman/stage8/ in the examples directory. -

                                                                                                                                                              If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                              +

                                                                                                                                                              If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                              
                                                                                                                                                               # toRoman and rest of module omitted for clarity
                                                                                                                                                               
                                                                                                                                                               romanNumeralPattern = \
                                                                                                                                                              -    re.compile('^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$') 1
                                                                                                                                                              +    re.compile('^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$') 
                                                                                                                                                               
                                                                                                                                                               def fromRoman(s):
                                                                                                                                                                   """convert Roman numeral to integer"""
                                                                                                                                                                   if not s:
                                                                                                                                                                       raise InvalidRomanNumeralError, 'Input can not be blank'
                                                                                                                                                              -    if not romanNumeralPattern.search(s):2
                                                                                                                                                              +    if not romanNumeralPattern.search(s):
                                                                                                                                                                       raise InvalidRomanNumeralError, 'Invalid Roman numeral: %s' % s
                                                                                                                                                               
                                                                                                                                                                   result = 0
                                                                                                                                                              @@ -12550,55 +8108,30 @@ def fromRoman(s):
                                                                                                                                                                           result += integer
                                                                                                                                                                           index += len(numeral)
                                                                                                                                                                   return result
                                                                                                                                                              -
                                                                                                                                                              - - - - - - - - - -
                                                                                                                                                              1 -This looks very similar, but in fact a lot has changed. romanNumeralPattern is no longer a string; it is a pattern object which was returned from re.compile. -
                                                                                                                                                              2 -That means that you can call methods on romanNumeralPattern directly. This will be much, much faster than calling re.search every time. The regular expression is compiled once and stored in romanNumeralPattern when the module is first imported; then, every time you call fromRoman, you can immediately match the input string against the regular expression, without any intermediate steps occurring under +
                                                                                                                                                              +
                                                                                                                                                                +
                                                                                                                                                              1. This looks very similar, but in fact a lot has changed. romanNumeralPattern is no longer a string; it is a pattern object which was returned from re.compile. +
                                                                                                                                                              2. That means that you can call methods on romanNumeralPattern directly. This will be much, much faster than calling re.search every time. The regular expression is compiled once and stored in romanNumeralPattern when the module is first imported; then, every time you call fromRoman, you can immediately match the input string against the regular expression, without any intermediate steps occurring under the covers. -

                                                                                                                                                              So how much faster is it to compile regular expressions? See for yourself: -

                                                                                                                                                              Example 15.12. Output of romantest81.py against roman81.py

                                                                                                                                                              .............         1
                                                                                                                                                              +

                                                                                                                                                              Example 15.12. Output of romantest81.py against roman81.py

                                                                                                                                                              .............         
                                                                                                                                                               ----------------------------------------------------------------------
                                                                                                                                                              -Ran 13 tests in 3.385s 2
                                                                                                                                                              +Ran 13 tests in 3.385s 
                                                                                                                                                               
                                                                                                                                                              -OK   3
                                                                                                                                                              - - - - - - - - - - - - - -
                                                                                                                                                              1 -Just a note in passing here: this time, I ran the unit test without the -v option, so instead of the full docstring for each test, you only get a dot for each test that passes. (If a test failed, you'd get an F, and if it had an error, you'd get an E. You'd still get complete tracebacks for each failure and error, so you could track down any problems.) -
                                                                                                                                                              2 -You ran 13 tests in 3.385 seconds, compared to 3.685 seconds without precompiling the regular expressions. That's an 8% improvement overall, and remember that most of the time spent during the unit test is spent doing other things. (Separately, +OK
                                                                                                                                                              +
                                                                                                                                                                +
                                                                                                                                                              1. Just a note in passing here: this time, I ran the unit test without the -v option, so instead of the full docstring for each test, you only get a dot for each test that passes. (If a test failed, you'd get an F, and if it had an error, you'd get an E. You'd still get complete tracebacks for each failure and error, so you could track down any problems.) +
                                                                                                                                                              2. You ran 13 tests in 3.385 seconds, compared to 3.685 seconds without precompiling the regular expressions. That's an 8% improvement overall, and remember that most of the time spent during the unit test is spent doing other things. (Separately, I time-tested the regular expressions by themselves, apart from the rest of the unit tests, and found that compiling this regular expression speeds up the search by an average of 54%.) Not bad for such a simple fix. -
                                                                                                                                                              3 -Oh, and in case you were wondering, precompiling the regular expression didn't break anything, and you just proved it.
                                                                                                                                                              +
                                                                                                                                                            6. Oh, and in case you were wondering, precompiling the regular expression didn't break anything, and you just proved it.

                                                                                                                                                              There is one other performance optimization that I want to try. Given the complexity of regular expression syntax, it should come as no surprise that there is frequently more than one way to write the same expression. After some discussion about this module on comp.lang.python, someone suggested that I try using the {m,n} syntax for the optional repeated characters. -

                                                                                                                                                              Example 15.13. roman82.py

                                                                                                                                                              +

                                                                                                                                                              Example 15.13. roman82.py

                                                                                                                                                              This file is available in py/roman/stage8/ in the examples directory. -

                                                                                                                                                              If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                              +

                                                                                                                                                              If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                              
                                                                                                                                                               # rest of program omitted for clarity
                                                                                                                                                               
                                                                                                                                                               #old version
                                                                                                                                                              @@ -12607,50 +8140,34 @@ this module on 
                                                                                                                                                               
                                                                                                                                                               #new version
                                                                                                                                                               romanNumeralPattern = \
                                                                                                                                                              -    re.compile('^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$') 1
                                                                                                                                                              -
                                                                                                                                                              +
                                                                                                                                                                +
                                                                                                                                                              1. You have replaced M?M?M?M? with M{0,4}. Both mean the same thing: “match 0 to 4 M characters”. Similarly, C?C?C? became C{0,3} (“match 0 to 3 C characters”) and so forth for X and I.

                                                                                                                                                                This form of the regular expression is a little shorter (though not any more readable). The big question is, is it any faster? -

                                                                                                                                                                Example 15.14. Output of romantest82.py against roman82.py

                                                                                                                                                                .............
                                                                                                                                                                +

                                                                                                                                                                Example 15.14. Output of romantest82.py against roman82.py

                                                                                                                                                                .............
                                                                                                                                                                 ----------------------------------------------------------------------
                                                                                                                                                                -Ran 13 tests in 3.315s 1
                                                                                                                                                                +Ran 13 tests in 3.315s 
                                                                                                                                                                 
                                                                                                                                                                -OK   2
                                                                                                                                                                - - - - - - - - - -
                                                                                                                                                                1 -Overall, the unit tests run 2% faster with this form of regular expression. That doesn't sound exciting, but remember that +OK
                                                                                                                                                                +
                                                                                                                                                                  +
                                                                                                                                                                1. Overall, the unit tests run 2% faster with this form of regular expression. That doesn't sound exciting, but remember that the search function is a small part of the overall unit test; most of the time is spent doing other things. (Separately, I time-tested just the regular expressions, and found that the search function is 11% faster with this syntax.) By precompiling the regular expression and rewriting part of it to use this new syntax, you've improved the regular expression performance by over 60%, and improved the overall performance of the entire unit test by over 10%. -
                                                                                                                                                                2 -More important than any performance boost is the fact that the module still works perfectly. This is the freedom I was talking +
                                                                                                                                                              2. More important than any performance boost is the fact that the module still works perfectly. This is the freedom I was talking about earlier: the freedom to tweak, change, or rewrite any piece of it and verify that you haven't messed anything up in the process. This is not a license to endlessly tweak your code just for the sake of tweaking it; you had a very specific objective (“make fromRoman faster”), and you were able to accomplish that objective without any lingering doubts about whether you introduced new bugs in the process. -
                                                                                                                                                              3. One other tweak I would like to make, and then I promise I'll stop refactoring and put this module to bed. As you've seen repeatedly, regular expressions can get pretty hairy and unreadable pretty quickly. I wouldn't like to come back to this module in six months and try to maintain it. Sure, the test cases pass, so I know that it works, but if I can't figure out how it works, it's still going to be difficult to add new features, fix new bugs, or otherwise maintain it. As you saw in Section 7.5, “Verbose Regular Expressions”, Python provides a way to document your logic line-by-line. -

                                                                                                                                                                Example 15.15. roman83.py

                                                                                                                                                                +

                                                                                                                                                                Example 15.15. roman83.py

                                                                                                                                                                This file is available in py/roman/stage8/ in the examples directory. -

                                                                                                                                                                If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                                +

                                                                                                                                                                If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                                
                                                                                                                                                                 # rest of program omitted for clarity
                                                                                                                                                                 
                                                                                                                                                                 #old version
                                                                                                                                                                @@ -12668,39 +8185,22 @@ romanNumeralPattern = re.compile('''
                                                                                                                                                                     (IX|IV|V?I{0,3})    # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
                                                                                                                                                                       #        or 5-8 (V, followed by 0 to 3 I's)
                                                                                                                                                                     $ # end of string
                                                                                                                                                                -    ''', re.VERBOSE) 1
                                                                                                                                                                -
                                                                                                                                                                - - - - - -
                                                                                                                                                                1 -The re.compile function can take an optional second argument, which is a set of one or more flags that control various options about the + ''', re.VERBOSE) +
                                                                                                                                                                +
                                                                                                                                                                  +
                                                                                                                                                                1. The re.compile function can take an optional second argument, which is a set of one or more flags that control various options about the compiled regular expression. Here you're specifying the re.VERBOSE flag, which tells Python that there are in-line comments within the regular expression itself. The comments and all the whitespace around them are not considered part of the regular expression; the re.compile function simply strips them all out when it compiles the expression. This new, “verbose” version is identical to the old version, but it is infinitely more readable. -
                                                                                                                                                                -

                                                                                                                                                                Example 15.16. Output of romantest83.py against roman83.py

                                                                                                                                                                .............
                                                                                                                                                                +

                                                                                                                                                                Example 15.16. Output of romantest83.py against roman83.py

                                                                                                                                                                .............
                                                                                                                                                                 ----------------------------------------------------------------------
                                                                                                                                                                -Ran 13 tests in 3.315s 1
                                                                                                                                                                +Ran 13 tests in 3.315s 
                                                                                                                                                                 
                                                                                                                                                                -OK   2
                                                                                                                                                                - - - - - - - - - -
                                                                                                                                                                1 -This new, “verbose” version runs at exactly the same speed as the old version. In fact, the compiled pattern objects are the same, since the +OK
                                                                                                                                                                +
                                                                                                                                                                  +
                                                                                                                                                                1. This new, “verbose” version runs at exactly the same speed as the old version. In fact, the compiled pattern objects are the same, since the re.compile function strips out all the stuff you added. -
                                                                                                                                                                2 -This new, “verbose” version passes all the same tests as the old version. Nothing has changed, except that the programmer who comes back to +
                                                                                                                                                              4. This new, “verbose” version passes all the same tests as the old version. Nothing has changed, except that the programmer who comes back to this module in six months stands a fighting chance of understanding how the function works. -
                                                                                                                                                              5. 15.4. Postscript

                                                                                                                                                                A clever reader read the previous section and took it to the next level. The biggest headache (and performance drain) in the program as it is currently written is the regular expression, which is required because you have no other way of breaking down a Roman numeral. But there's only @@ -12709,9 +8209,10 @@ OK 2And best of all, he already had a complete set of unit tests. He changed over half the code in the module, but the unit tests stayed the same, so he could prove that his code worked just as well as the original. -

                                                                                                                                                                Example 15.17. roman9.py

                                                                                                                                                                +

                                                                                                                                                                Example 15.17. roman9.py

                                                                                                                                                                This file is available in py/roman/stage9/ in the examples directory. -

                                                                                                                                                                If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                                +

                                                                                                                                                                If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                                
                                                                                                                                                                 #Define exceptions
                                                                                                                                                                 class RomanError(Exception): pass
                                                                                                                                                                 class OutOfRangeError(RomanError): pass
                                                                                                                                                                @@ -12779,8 +8280,8 @@ def fillLookupTables():
                                                                                                                                                                 
                                                                                                                                                                 fillLookupTables()
                                                                                                                                                                 

                                                                                                                                                                So how fast is it? -

                                                                                                                                                                Example 15.18. Output of romantest9.py against roman9.py

                                                                                                                                                                -
                                                                                                                                                                +

                                                                                                                                                                Example 15.18. Output of romantest9.py against roman9.py

                                                                                                                                                                +
                                                                                                                                                                 .............
                                                                                                                                                                 ----------------------------------------------------------------------
                                                                                                                                                                 Ran 13 tests in 0.791s
                                                                                                                                                                @@ -12791,7 +8292,7 @@ OK
                                                                                                                                                                 a fair comparison, because this version will take longer to import (when it fills the lookup tables). But since import is
                                                                                                                                                                 only done once, this is negligible in the long run.
                                                                                                                                                                 

                                                                                                                                                                The moral of the story? -

                                                                                                                                                                +
                                                                                                                                                                • Simplicity is a virtue.
                                                                                                                                                                • Especially when regular expressions are involved. @@ -12805,8 +8306,8 @@ only done once, this is negligible in the long run. testing, integration testing, and user acceptance testing. But it is feasible, and it does work, and once you've seen it work, you'll wonder how you ever got along without it.

                                                                                                                                                                  This chapter covered a lot of ground, and much of it wasn't even Python-specific. There are unit testing frameworks for many languages, all of which require you to understand the same basic concepts: -

                                                                                                                                                                  -
                                                                                                                                                                  +
                                                                                                                                                                  +
                                                                                                                                                                  • Designing test cases that are specific, automated, and independent
                                                                                                                                                                  • Writing test cases before the code they are testing @@ -12820,8 +8321,8 @@ only done once, this is negligible in the long run.

                                                                                                                                                                  Additionally, you should be comfortable doing all of the following Python-specific things: -

                                                                                                                                                                  -
                                                                                                                                                                  +
                                                                                                                                                                  +
                                                                                                                                                                  -
                                                                                                                                                                  +

                                                                                                                                                                  Further reading

                                                                                                                                                                  -
                                                                                                                                                                  +

                                                                                                                                                                  Chapter 16. Functional Programming

                                                                                                                                                                  16.1. Diving in

                                                                                                                                                                  In Chapter 13, Unit Testing, you learned about the philosophy of unit testing. In Chapter 14, Test-First Programming, you stepped through the implementation of basic unit tests in Python. In Chapter 15, Refactoring, you saw how unit testing makes large-scale refactoring easier. This chapter will build on those sample programs, but here @@ -12850,8 +8351,9 @@ modules, collects them all into one big test suite, and runs them all at once. I build process for this book; I have unit tests for several of the example programs (not just the roman.py module featured in Chapter 13, Unit Testing), and the first thing my automated build script does is run this program to make sure all my examples still work. If this regression test fails, the build immediately stops. I don't want to release non-working examples any more than you want to download them and sit around scratching your head and yelling at your monitor and wondering why they don't work. -

                                                                                                                                                                  Example 16.1. regression.py

                                                                                                                                                                  -

                                                                                                                                                                  If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                                  +

                                                                                                                                                                  Example 16.1. regression.py

                                                                                                                                                                  +

                                                                                                                                                                  If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                                  
                                                                                                                                                                   """Regression testing framework
                                                                                                                                                                   
                                                                                                                                                                   This module will search for scripts in the same directory named
                                                                                                                                                                  @@ -12878,18 +8380,18 @@ if __name__ == "__main__":
                                                                                                                                                                       unittest.main(defaultTest="regressionTest")
                                                                                                                                                                   

                                                                                                                                                                  Running this script in the same directory as the rest of the example scripts that come with this book will find all the unit tests, named moduletest.py, run them as a single test, and pass or fail them all at once. -

                                                                                                                                                                  Example 16.2. Sample output of regression.py

                                                                                                                                                                  -[you@localhost py]$ python regression.py -v
                                                                                                                                                                  -help should fail with no object ... ok           1
                                                                                                                                                                  +

                                                                                                                                                                  Example 16.2. Sample output of regression.py

                                                                                                                                                                  +[you@localhost py]$ python regression.py -v
                                                                                                                                                                  +help should fail with no object ... ok           
                                                                                                                                                                   help should return known result for apihelper ... ok
                                                                                                                                                                   help should honor collapse argument ... ok
                                                                                                                                                                   help should honor spacing argument ... ok
                                                                                                                                                                  -buildConnectionString should fail with list input ... ok           2
                                                                                                                                                                  +buildConnectionString should fail with list input ... ok           
                                                                                                                                                                   buildConnectionString should fail with string input ... ok
                                                                                                                                                                   buildConnectionString should fail with tuple input ... ok
                                                                                                                                                                   buildConnectionString handles empty dictionary ... ok
                                                                                                                                                                   buildConnectionString returns known result with known input ... ok
                                                                                                                                                                  -fromRoman should only accept uppercase input ... ok                3
                                                                                                                                                                  +fromRoman should only accept uppercase input ... ok                
                                                                                                                                                                   toRoman should always return uppercase ... ok
                                                                                                                                                                   fromRoman should fail with blank string ... ok
                                                                                                                                                                   fromRoman should fail with malformed antecedents ... ok
                                                                                                                                                                  @@ -12913,361 +8415,181 @@ kgp g ref test ... ok
                                                                                                                                                                   ----------------------------------------------------------------------
                                                                                                                                                                   Ran 29 tests in 2.799s
                                                                                                                                                                   
                                                                                                                                                                  -OK
                                                                                                                                                                  - - - - - - - - - - - - - -
                                                                                                                                                                  1 -The first 5 tests are from apihelpertest.py, which tests the example script from Chapter 4, The Power Of Introspection. -
                                                                                                                                                                  2 -The next 5 tests are from odbchelpertest.py, which tests the example script from Chapter 2, Your First Python Program. -
                                                                                                                                                                  3 -The rest are from romantest.py, which you studied in depth in Chapter 13, Unit Testing. -
                                                                                                                                                                  +OK
                                                                                                                                                                  +
                                                                                                                                                                    +
                                                                                                                                                                  1. The first 5 tests are from apihelpertest.py, which tests the example script from Chapter 4, The Power Of Introspection. +
                                                                                                                                                                  2. The next 5 tests are from odbchelpertest.py, which tests the example script from Chapter 2, Your First Python Program. +
                                                                                                                                                                  3. The rest are from romantest.py, which you studied in depth in Chapter 13, Unit Testing.

                                                                                                                                                                    16.2. Finding the path

                                                                                                                                                                    When running Python scripts from the command line, it is sometimes useful to know where the currently running script is located on disk.

                                                                                                                                                                    This is one of those obscure little tricks that is virtually impossible to figure out on your own, but simple to remember once you see it. The key to it is sys.argv. As you saw in Chapter 9, XML Processing, this is a list that holds the list of command-line arguments. However, it also holds the name of the running script, exactly as it was called from the command line, and this is enough information to determine its location. -

                                                                                                                                                                    Example 16.3. fullpath.py

                                                                                                                                                                    -

                                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                                    +

                                                                                                                                                                    Example 16.3. fullpath.py

                                                                                                                                                                    +

                                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                                    
                                                                                                                                                                     import sys, os
                                                                                                                                                                     
                                                                                                                                                                    -print 'sys.argv[0] =', sys.argv[0]             1
                                                                                                                                                                    -pathname = os.path.dirname(sys.argv[0])        2
                                                                                                                                                                    +print 'sys.argv[0] =', sys.argv[0]             
                                                                                                                                                                    +pathname = os.path.dirname(sys.argv[0])        
                                                                                                                                                                     print 'path =', pathname
                                                                                                                                                                    -print 'full path =', os.path.abspath(pathname) 3
                                                                                                                                                                    - - - - - - - - - - - - - -
                                                                                                                                                                    1 -Regardless of how you run a script, sys.argv[0] will always contain the name of the script, exactly as it appears on the command line. This may or may not include any path +print 'full path =', os.path.abspath(pathname)
                                                                                                                                                                    +
                                                                                                                                                                      +
                                                                                                                                                                    1. Regardless of how you run a script, sys.argv[0] will always contain the name of the script, exactly as it appears on the command line. This may or may not include any path information, as you'll see shortly. -
                                                                                                                                                                    2 -os.path.dirname takes a filename as a string and returns the directory path portion. If the given filename does not include any path information, +
                                                                                                                                                                  4. os.path.dirname takes a filename as a string and returns the directory path portion. If the given filename does not include any path information, os.path.dirname returns an empty string. -
                                                                                                                                                                  5. 3 -os.path.abspath is the key here. It takes a pathname, which can be partial or even blank, and returns a fully qualified pathname. -
                                                                                                                                                                    +
                                                                                                                                                                  6. os.path.abspath is the key here. It takes a pathname, which can be partial or even blank, and returns a fully qualified pathname.

                                                                                                                                                                    os.path.abspath deserves further explanation. It is very flexible; it can take any kind of pathname. -

                                                                                                                                                                    Example 16.4. Further explanation of os.path.abspath

                                                                                                                                                                    ->>> import os
                                                                                                                                                                    ->>> os.getcwd()      1
                                                                                                                                                                    +

                                                                                                                                                                    Example 16.4. Further explanation of os.path.abspath

                                                                                                                                                                    +>>> import os
                                                                                                                                                                    +>>> os.getcwd()      
                                                                                                                                                                     /home/you
                                                                                                                                                                    ->>> os.path.abspath('')                2
                                                                                                                                                                    +>>> os.path.abspath('')                
                                                                                                                                                                     /home/you
                                                                                                                                                                    ->>> os.path.abspath('.ssh')            3
                                                                                                                                                                    +>>> os.path.abspath('.ssh')            
                                                                                                                                                                     /home/you/.ssh
                                                                                                                                                                    ->>> os.path.abspath('/home/you/.ssh') 4
                                                                                                                                                                    +>>> os.path.abspath('/home/you/.ssh') 
                                                                                                                                                                     /home/you/.ssh
                                                                                                                                                                    ->>> os.path.abspath('.ssh/../foo/')    5
                                                                                                                                                                    -/home/you/foo
                                                                                                                                                                    - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                                                                    1 -os.getcwd() returns the current working directory. -
                                                                                                                                                                    2 -Calling os.path.abspath with an empty string returns the current working directory, same as os.getcwd(). -
                                                                                                                                                                    3 -Calling os.path.abspath with a partial pathname constructs a fully qualified pathname out of it, based on the current working directory. -
                                                                                                                                                                    4 -Calling os.path.abspath with a full pathname simply returns it. -
                                                                                                                                                                    5 -os.path.abspath also normalizes the pathname it returns. Note that this example worked even though I don't actually have a 'foo' directory. os.path.abspath never checks your actual disk; this is all just string manipulation. -
                                                                                                                                                                    -
                                                                                                                                                                    - - - - - - -
                                                                                                                                                                    Note
                                                                                                                                                                    The pathnames and filenames you pass to os.path.abspath do not need to exist. -
                                                                                                                                                                    - - - - -
                                                                                                                                                                    Note
                                                                                                                                                                    os.path.abspath not only constructs full path names, it also normalizes them. That means that if you are in the /usr/ directory, os.path.abspath('bin/../local/bin') will return /usr/local/bin. It normalizes the path by making it as simple as possible. If you just want to normalize a pathname like this without +>>> os.path.abspath('.ssh/../foo/') +/home/you/foo
                                                                                                                                                                    +
                                                                                                                                                                      +
                                                                                                                                                                    1. os.getcwd() returns the current working directory. +
                                                                                                                                                                    2. Calling os.path.abspath with an empty string returns the current working directory, same as os.getcwd(). +
                                                                                                                                                                    3. Calling os.path.abspath with a partial pathname constructs a fully qualified pathname out of it, based on the current working directory. +
                                                                                                                                                                    4. Calling os.path.abspath with a full pathname simply returns it. +
                                                                                                                                                                    5. os.path.abspath also normalizes the pathname it returns. Note that this example worked even though I don't actually have a 'foo' directory. os.path.abspath never checks your actual disk; this is all just string manipulation. + + +
                                                                                                                                                                      NoteThe pathnames and filenames you pass to os.path.abspath do not need to exist. + + + - -
                                                                                                                                                                      Noteos.path.abspath not only constructs full path names, it also normalizes them. That means that if you are in the /usr/ directory, os.path.abspath('bin/../local/bin') will return /usr/local/bin. It normalizes the path by making it as simple as possible. If you just want to normalize a pathname like this without turning it into a full pathname, use os.path.normpath instead. -
                                                                                                                                                                      -

                                                                                                                                                                      Example 16.5. Sample output from fullpath.py

                                                                                                                                                                      -[you@localhost py]$ python /home/you/diveintopython3/common/py/fullpath.py 1
                                                                                                                                                                      -sys.argv[0] = /home/you/diveintopython3/common/py/fullpath.py
                                                                                                                                                                      +

                                                                                                                                                                      Example 16.5. Sample output from fullpath.py

                                                                                                                                                                      +[you@localhost py]$ python /home/you/diveintopython3/common/py/fullpath.py 
                                                                                                                                                                      +sys.argv[0] = /home/you/diveintopython3/common/py/fullpath.py
                                                                                                                                                                       path = /home/you/diveintopython3/common/py
                                                                                                                                                                       full path = /home/you/diveintopython3/common/py
                                                                                                                                                                      -[you@localhost diveintopython3]$ python common/py/fullpath.py               2
                                                                                                                                                                      -sys.argv[0] = common/py/fullpath.py
                                                                                                                                                                      +[you@localhost diveintopython3]$ python common/py/fullpath.py               
                                                                                                                                                                      +sys.argv[0] = common/py/fullpath.py
                                                                                                                                                                       path = common/py
                                                                                                                                                                       full path = /home/you/diveintopython3/common/py
                                                                                                                                                                      -[you@localhost diveintopython3]$ cd common/py
                                                                                                                                                                      -[you@localhost py]$ python fullpath.py 3
                                                                                                                                                                      -sys.argv[0] = fullpath.py
                                                                                                                                                                      +[you@localhost diveintopython3]$ cd common/py
                                                                                                                                                                      +[you@localhost py]$ python fullpath.py 
                                                                                                                                                                      +sys.argv[0] = fullpath.py
                                                                                                                                                                       path = 
                                                                                                                                                                      -full path = /home/you/diveintopython3/common/py
                                                                                                                                                                      - - - - - - - - - - - - - -
                                                                                                                                                                      1 -In the first case, sys.argv[0] includes the full path of the script. You can then use the os.path.dirname function to strip off the script name and return the full directory name, and os.path.abspath simply returns what you give it. -
                                                                                                                                                                      2 -If the script is run by using a partial pathname, sys.argv[0] will still contain exactly what appears on the command line. os.path.dirname will then give you a partial pathname (relative to the current directory), and os.path.abspath will construct a full pathname from the partial pathname. -
                                                                                                                                                                      3 -If the script is run from the current directory without giving any path, os.path.dirname will simply return an empty string. Given an empty string, os.path.abspath returns the current directory, which is what you want, since the script was run from the current directory. -
                                                                                                                                                                      -
                                                                                                                                                                      - - - - -
                                                                                                                                                                      Note
                                                                                                                                                                      Like the other functions in the os and os.path modules, os.path.abspath is cross-platform. Your results will look slightly different than my examples if you're running on Windows (which uses backslash +full path = /home/you/diveintopython3/common/py
                                                                                                                                                                      +
                                                                                                                                                                        +
                                                                                                                                                                      1. In the first case, sys.argv[0] includes the full path of the script. You can then use the os.path.dirname function to strip off the script name and return the full directory name, and os.path.abspath simply returns what you give it. +
                                                                                                                                                                      2. If the script is run by using a partial pathname, sys.argv[0] will still contain exactly what appears on the command line. os.path.dirname will then give you a partial pathname (relative to the current directory), and os.path.abspath will construct a full pathname from the partial pathname. +
                                                                                                                                                                      3. If the script is run from the current directory without giving any path, os.path.dirname will simply return an empty string. Given an empty string, os.path.abspath returns the current directory, which is what you want, since the script was run from the current directory. + + + - -
                                                                                                                                                                        NoteLike the other functions in the os and os.path modules, os.path.abspath is cross-platform. Your results will look slightly different than my examples if you're running on Windows (which uses backslash as a path separator) or Mac OS (which uses colons), but they'll still work. That's the whole point of the os module. -

                                                                                                                                                                        Addendum. One reader was dissatisfied with this solution, and wanted to be able to run all the unit tests in the current directory, not the directory where regression.py is located. He suggests this approach instead: -

                                                                                                                                                                        Example 16.6. Running scripts in the current directory

                                                                                                                                                                        import sys, os, re, unittest
                                                                                                                                                                        +

                                                                                                                                                                        Example 16.6. Running scripts in the current directory

                                                                                                                                                                        import sys, os, re, unittest
                                                                                                                                                                         
                                                                                                                                                                         def regressionTest():
                                                                                                                                                                        -    path = os.getcwd()       1
                                                                                                                                                                        -    sys.path.append(path)    2
                                                                                                                                                                        -    files = os.listdir(path) 3
                                                                                                                                                                        -
                                                                                                                                                                        - - - - - - - - - - - - - -
                                                                                                                                                                        1 -Instead of setting path to the directory where the currently running script is located, you set it to the current working directory instead. This + path = os.getcwd() + sys.path.append(path) + files = os.listdir(path) +
                                                                                                                                                                        +
                                                                                                                                                                          +
                                                                                                                                                                        1. Instead of setting path to the directory where the currently running script is located, you set it to the current working directory instead. This will be whatever directory you were in before you ran the script, which is not necessarily the same as the directory the script is in. (Read that sentence a few times until you get it.) -
                                                                                                                                                                        2 -Append this directory to the Python library search path, so that when you dynamically import the unit test modules later, Python can find them. You didn't need to do this when path was the directory of the currently running script, because Python always looks in that directory. -
                                                                                                                                                                        3 -The rest of the function is the same.
                                                                                                                                                                        +
                                                                                                                                                                      4. Append this directory to the Python library search path, so that when you dynamically import the unit test modules later, Python can find them. You didn't need to do this when path was the directory of the currently running script, because Python always looks in that directory. +
                                                                                                                                                                      5. The rest of the function is the same.

                                                                                                                                                                        This technique will allow you to re-use this regression.py script on multiple projects. Just put the script in a common directory, then change to the project's directory before running it. All of that project's unit tests will be found and tested, instead of the unit tests in the common directory where regression.py is located.

                                                                                                                                                                        16.3. Filtering lists revisited

                                                                                                                                                                        You're already familiar with using list comprehensions to filter lists. There is another way to accomplish this same thing, which some people feel is more expressive. -

                                                                                                                                                                        Python has a built-in filter function which takes two arguments, a function and a list, and returns a list.[7] The function passed as the first argument to filter must itself take one argument, and the list that filter returns will contain all the elements from the list passed to filter for which the function passed to filter returns true. +

                                                                                                                                                                        Python has a built-in filter function which takes two arguments, a function and a list, and returns a list. +[7] The function passed as the first argument to filter must itself take one argument, and the list that filter returns will contain all the elements from the list passed to filter for which the function passed to filter returns true.

                                                                                                                                                                        Got all that? It's not as difficult as it sounds. -

                                                                                                                                                                        Example 16.7. Introducing filter

                                                                                                                                                                        ->>> def odd(n):                 1
                                                                                                                                                                        -...    return n % 2
                                                                                                                                                                        -...    
                                                                                                                                                                        ->>> li = [1, 2, 3, 5, 9, 10, 256, -3]
                                                                                                                                                                        ->>> filter(odd, li)             2
                                                                                                                                                                        +

                                                                                                                                                                        Example 16.7. Introducing filter

                                                                                                                                                                        +>>> def odd(n):                 
                                                                                                                                                                        +...    return n % 2
                                                                                                                                                                        +...    
                                                                                                                                                                        +>>> li = [1, 2, 3, 5, 9, 10, 256, -3]
                                                                                                                                                                        +>>> filter(odd, li)             
                                                                                                                                                                         [1, 3, 5, 9, -3]
                                                                                                                                                                        ->>> [e for e in li if odd(e)]   3
                                                                                                                                                                        ->>> filteredList = []
                                                                                                                                                                        ->>> for n in li:                4
                                                                                                                                                                        -...    if odd(n):
                                                                                                                                                                        -...        filteredList.append(n)
                                                                                                                                                                        -...    
                                                                                                                                                                        ->>> filteredList
                                                                                                                                                                        -[1, 3, 5, 9, -3]
                                                                                                                                                                        - - - - - - - - - - - - - - - - - -
                                                                                                                                                                        1 -odd uses the built-in mod function “%” to return True if n is odd and False if n is even. -
                                                                                                                                                                        2 -filter takes two arguments, a function (odd) and a list (li). It loops through the list and calls odd with each element. If odd returns a true value (remember, any non-zero value is true in Python), then the element is included in the returned list, otherwise it is filtered out. The result is a list of only the odd +>>> [e for e in li if odd(e)] +>>> filteredList = [] +>>> for n in li: +... if odd(n): +... filteredList.append(n) +... +>>> filteredList +[1, 3, 5, 9, -3]
                                                                                                                                                                        +
                                                                                                                                                                          +
                                                                                                                                                                        1. odd uses the built-in mod function “%” to return True if n is odd and False if n is even. +
                                                                                                                                                                        2. filter takes two arguments, a function (odd) and a list (li). It loops through the list and calls odd with each element. If odd returns a true value (remember, any non-zero value is true in Python), then the element is included in the returned list, otherwise it is filtered out. The result is a list of only the odd numbers from the original list, in the same order as they appeared in the original. -
                                                                                                                                                                        3 -You could accomplish the same thing using list comprehensions, as you saw in Section 4.5, “Filtering Lists”. -
                                                                                                                                                                        4 -You could also accomplish the same thing with a for loop. Depending on your programming background, this may seem more “straightforward”, but functions like filter are much more expressive. Not only is it easier to write, it's easier to read, too. Reading the for loop is like standing too close to a painting; you see all the details, but it may take a few seconds to be able to step +
                                                                                                                                                                      6. You could accomplish the same thing using list comprehensions, as you saw in Section 4.5, “Filtering Lists”. +
                                                                                                                                                                      7. You could also accomplish the same thing with a for loop. Depending on your programming background, this may seem more “straightforward”, but functions like filter are much more expressive. Not only is it easier to write, it's easier to read, too. Reading the for loop is like standing too close to a painting; you see all the details, but it may take a few seconds to be able to step back and see the bigger picture: “Oh, you're just filtering the list!” -
                                                                                                                                                                      8. -

                                                                                                                                                                        Example 16.8. filter in regression.py

                                                                                                                                                                        -    files = os.listdir(path)              1
                                                                                                                                                                        -    test = re.compile("test\.py$", re.IGNORECASE)           2
                                                                                                                                                                        -    files = filter(test.search, files)    3
                                                                                                                                                                        - - - - - - - - - - - - - -
                                                                                                                                                                        1 -As you saw in Section 16.2, “Finding the path”, path may contain the full or partial pathname of the directory of the currently running script, or it may contain an empty string +

                                                                                                                                                                        Example 16.8. filter in regression.py

                                                                                                                                                                        
                                                                                                                                                                        +    files = os.listdir(path)              
                                                                                                                                                                        +    test = re.compile("test\.py$", re.IGNORECASE)           
                                                                                                                                                                        +    files = filter(test.search, files)    
                                                                                                                                                                        +
                                                                                                                                                                          +
                                                                                                                                                                        1. As you saw in Section 16.2, “Finding the path”, path may contain the full or partial pathname of the directory of the currently running script, or it may contain an empty string if the script is being run from the current directory. Either way, files will end up with the names of the files in the same directory as this script you're running. -
                                                                                                                                                                        2 -This is a compiled regular expression. As you saw in Section 15.3, “Refactoring”, if you're going to use the same regular expression over and over, you should compile it for faster performance. The compiled +
                                                                                                                                                                      9. This is a compiled regular expression. As you saw in Section 15.3, “Refactoring”, if you're going to use the same regular expression over and over, you should compile it for faster performance. The compiled object has a search method which takes a single argument, the string to search. If the regular expression matches the string, the search method returns a Match object containing information about the regular expression match; otherwise it returns None, the Python null value. -
                                                                                                                                                                      10. 3 -For each element in the files list, you're going to call the search method of the compiled regular expression object, test. If the regular expression matches, the method will return a Match object, which Python considers to be true, so the element will be included in the list returned by filter. If the regular expression does not match, the search method will return None, which Python considers to be false, so the element will not be included. -
                                                                                                                                                                        +
                                                                                                                                                                      11. For each element in the files list, you're going to call the search method of the compiled regular expression object, test. If the regular expression matches, the method will return a Match object, which Python considers to be true, so the element will be included in the list returned by filter. If the regular expression does not match, the search method will return None, which Python considers to be false, so the element will not be included.

                                                                                                                                                                        Historical note. Versions of Python prior to 2.0 did not have list comprehensions, so you couldn't filter using list comprehensions; the filter function was the only game in town. Even with the introduction of list comprehensions in 2.0, some people still prefer the old-style filter (and its companion function, map, which you'll see later in this chapter). Both techniques work at the moment, so which one you use is a matter of style. There is discussion that map and filter might be deprecated in a future version of Python, but no decision has been made. -

                                                                                                                                                                        Example 16.9. Filtering using list comprehensions instead

                                                                                                                                                                        +

                                                                                                                                                                        Example 16.9. Filtering using list comprehensions instead

                                                                                                                                                                        
                                                                                                                                                                             files = os.listdir(path)             
                                                                                                                                                                             test = re.compile("test\.py$", re.IGNORECASE)          
                                                                                                                                                                        -    files = [f for f in files if test.search(f)] 1
                                                                                                                                                                        - - - - - -
                                                                                                                                                                        1 -This will accomplish exactly the same result as using the filter function. Which way is more expressive? That's up to you. -
                                                                                                                                                                        + files = [f for f in files if test.search(f)]
                                                                                                                                                                        +
                                                                                                                                                                          +
                                                                                                                                                                        1. This will accomplish exactly the same result as using the filter function. Which way is more expressive? That's up to you.

                                                                                                                                                                          16.4. Mapping lists revisited

                                                                                                                                                                          You're already familiar with using list comprehensions to map one list into another. There is another way to accomplish the same thing, using the built-in map function. It works much the same way as the filter function. -

                                                                                                                                                                          Example 16.10. Introducing map

                                                                                                                                                                          ->>> def double(n):
                                                                                                                                                                          -...    return n*2
                                                                                                                                                                          -...    
                                                                                                                                                                          ->>> li = [1, 2, 3, 5, 9, 10, 256, -3]
                                                                                                                                                                          ->>> map(double, li)     1
                                                                                                                                                                          +

                                                                                                                                                                          Example 16.10. Introducing map

                                                                                                                                                                          +>>> def double(n):
                                                                                                                                                                          +...    return n*2
                                                                                                                                                                          +...    
                                                                                                                                                                          +>>> li = [1, 2, 3, 5, 9, 10, 256, -3]
                                                                                                                                                                          +>>> map(double, li)     
                                                                                                                                                                           [2, 4, 6, 10, 18, 20, 512, -6]
                                                                                                                                                                          ->>> [double(n) for n in li]               2
                                                                                                                                                                          +>>> [double(n) for n in li]               
                                                                                                                                                                           [2, 4, 6, 10, 18, 20, 512, -6]
                                                                                                                                                                          ->>> newlist = []
                                                                                                                                                                          ->>> for n in li:        3
                                                                                                                                                                          -...    newlist.append(double(n))
                                                                                                                                                                          -...    
                                                                                                                                                                          ->>> newlist
                                                                                                                                                                          -[2, 4, 6, 10, 18, 20, 512, -6]
                                                                                                                                                                          - - - - - - - - - - - - - -
                                                                                                                                                                          1 -map takes a function and a list[8] and returns a new list by calling the function with each element of the list in order. In this case, the function simply +>>> newlist = [] +>>> for n in li: +... newlist.append(double(n)) +... +>>> newlist +[2, 4, 6, 10, 18, 20, 512, -6]
                                                                                                                                                                          +
                                                                                                                                                                            +
                                                                                                                                                                          1. map takes a function and a list[8] and returns a new list by calling the function with each element of the list in order. In this case, the function simply multiplies each element by 2. -
                                                                                                                                                                          2 -You could accomplish the same thing with a list comprehension. List comprehensions were first introduced in Python 2.0; map has been around forever. -
                                                                                                                                                                          3 -You could, if you insist on thinking like a Visual Basic programmer, use a for loop to accomplish the same thing. -
                                                                                                                                                                          -

                                                                                                                                                                          Example 16.11. map with lists of mixed datatypes

                                                                                                                                                                          ->>> li = [5, 'a', (2, 'b')]
                                                                                                                                                                          ->>> map(double, li)     1
                                                                                                                                                                          -[10, 'aa', (2, 'b', 2, 'b')]
                                                                                                                                                                          - - - - - -
                                                                                                                                                                          1 -As a side note, I'd like to point out that map works just as well with lists of mixed datatypes, as long as the function you're using correctly handles each type. In this +
                                                                                                                                                                        2. You could accomplish the same thing with a list comprehension. List comprehensions were first introduced in Python 2.0; map has been around forever. +
                                                                                                                                                                        3. You could, if you insist on thinking like a Visual Basic programmer, use a for loop to accomplish the same thing. +

                                                                                                                                                                          Example 16.11. map with lists of mixed datatypes

                                                                                                                                                                          +>>> li = [5, 'a', (2, 'b')]
                                                                                                                                                                          +>>> map(double, li)     
                                                                                                                                                                          +[10, 'aa', (2, 'b', 2, 'b')]
                                                                                                                                                                          +
                                                                                                                                                                            +
                                                                                                                                                                          1. As a side note, I'd like to point out that map works just as well with lists of mixed datatypes, as long as the function you're using correctly handles each type. In this case, the double function simply multiplies the given argument by 2, and Python Does The Right Thing depending on the datatype of the argument. For integers, this means actually multiplying it by 2; for strings, it means concatenating the string with itself; for tuples, it means making a new tuple that has all of the elements of the original, then all of the elements of the original again. -
                                                                                                                                                                        4. All right, enough play time. Let's look at some real code. -

                                                                                                                                                                          Example 16.12. map in regression.py

                                                                                                                                                                          -    filenameToModuleName = lambda f: os.path.splitext(f)[0] 1
                                                                                                                                                                          -    moduleNames = map(filenameToModuleName, files)          2
                                                                                                                                                                          - - - - - - - - - -
                                                                                                                                                                          1 -As you saw in Section 4.7, “Using lambda Functions”, lambda defines an inline function. And as you saw in Example 6.17, “Splitting Pathnames”, os.path.splitext takes a filename and returns a tuple (name, extension). So filenameToModuleName is a function which will take a filename and strip off the file extension, and return just the name. -
                                                                                                                                                                          2 -Calling map takes each filename listed in files, passes it to the function filenameToModuleName, and returns a list of the return values of each of those function calls. In other words, you strip the file extension off +

                                                                                                                                                                          Example 16.12. map in regression.py

                                                                                                                                                                          
                                                                                                                                                                          +    filenameToModuleName = lambda f: os.path.splitext(f)[0] 
                                                                                                                                                                          +    moduleNames = map(filenameToModuleName, files)          
                                                                                                                                                                          +
                                                                                                                                                                            +
                                                                                                                                                                          1. As you saw in Section 4.7, “Using lambda Functions”, lambda defines an inline function. And as you saw in Example 6.17, “Splitting Pathnames”, os.path.splitext takes a filename and returns a tuple (name, extension). So filenameToModuleName is a function which will take a filename and strip off the file extension, and return just the name. +
                                                                                                                                                                          2. Calling map takes each filename listed in files, passes it to the function filenameToModuleName, and returns a list of the return values of each of those function calls. In other words, you strip the file extension off of each filename, and store the list of all those stripped filenames in moduleNames. -

                                                                                                                                                                          As you'll see in the rest of the chapter, you can extend this type of data-centric thinking all the way to the final goal, which is to define and execute a single test suite that contains the tests from all of those individual test suites.

                                                                                                                                                                          16.5. Data-centric programming

                                                                                                                                                                          @@ -13297,94 +8619,56 @@ too much, filter it. If it's not what you want, map it. Focus on the data; leave

                                                                                                                                                                          OK, enough philosophizing. Let's talk about dynamically importing modules.

                                                                                                                                                                          First, let's look at how you normally import modules. The import module syntax looks in the search path for the named module and imports it by name. You can even import multiple modules at once this way, with a comma-separated list. You did this on the very first line of this chapter's script. -

                                                                                                                                                                          Example 16.13. Importing multiple modules at once

                                                                                                                                                                          -import sys, os, re, unittest 1
                                                                                                                                                                          -
                                                                                                                                                                          - - - - - -
                                                                                                                                                                          1 -This imports four modules at once: sys (for system functions and access to the command line parameters), os (for operating system functions like directory listings), re (for regular expressions), and unittest (for unit testing). -
                                                                                                                                                                          +

                                                                                                                                                                          Example 16.13. Importing multiple modules at once

                                                                                                                                                                          
                                                                                                                                                                          +import sys, os, re, unittest 
                                                                                                                                                                          +
                                                                                                                                                                          +
                                                                                                                                                                            +
                                                                                                                                                                          1. This imports four modules at once: sys (for system functions and access to the command line parameters), os (for operating system functions like directory listings), re (for regular expressions), and unittest (for unit testing).

                                                                                                                                                                            Now let's do the same thing, but with dynamic imports. -

                                                                                                                                                                            Example 16.14. Importing modules dynamically

                                                                                                                                                                            ->>> sys = __import__('sys')           1
                                                                                                                                                                            ->>> os = __import__('os')
                                                                                                                                                                            ->>> re = __import__('re')
                                                                                                                                                                            ->>> unittest = __import__('unittest')
                                                                                                                                                                            ->>> sys             2
                                                                                                                                                                            ->>> <module 'sys' (built-in)>
                                                                                                                                                                            ->>> os
                                                                                                                                                                            ->>> <module 'os' from '/usr/local/lib/python2.2/os.pyc'>
                                                                                                                                                                            -
                                                                                                                                                                            - - - - - - - - - -
                                                                                                                                                                            1 -The built-in __import__ function accomplishes the same goal as using the import statement, but it's an actual function, and it takes a string as an argument. -
                                                                                                                                                                            2 -The variable sys is now the sys module, just as if you had said import sys. The variable os is now the os module, and so forth. -
                                                                                                                                                                            +

                                                                                                                                                                            Example 16.14. Importing modules dynamically

                                                                                                                                                                            +>>> sys = __import__('sys')           
                                                                                                                                                                            +>>> os = __import__('os')
                                                                                                                                                                            +>>> re = __import__('re')
                                                                                                                                                                            +>>> unittest = __import__('unittest')
                                                                                                                                                                            +>>> sys             
                                                                                                                                                                            +>>> <module 'sys' (built-in)>
                                                                                                                                                                            +>>> os
                                                                                                                                                                            +>>> <module 'os' from '/usr/local/lib/python2.2/os.pyc'>
                                                                                                                                                                            +
                                                                                                                                                                            +
                                                                                                                                                                              +
                                                                                                                                                                            1. The built-in __import__ function accomplishes the same goal as using the import statement, but it's an actual function, and it takes a string as an argument. +
                                                                                                                                                                            2. The variable sys is now the sys module, just as if you had said import sys. The variable os is now the os module, and so forth.

                                                                                                                                                                              So __import__ imports a module, but takes a string argument to do it. In this case the module you imported was just a hard-coded string, but it could just as easily be a variable, or the result of a function call. And the variable that you assign the module to doesn't need to match the module name, either. You could import a series of modules and assign them to a list. -

                                                                                                                                                                              Example 16.15. Importing a list of modules dynamically

                                                                                                                                                                              ->>> moduleNames = ['sys', 'os', 're', 'unittest'] 1
                                                                                                                                                                              ->>> moduleNames
                                                                                                                                                                              +

                                                                                                                                                                              Example 16.15. Importing a list of modules dynamically

                                                                                                                                                                              +>>> moduleNames = ['sys', 'os', 're', 'unittest'] 
                                                                                                                                                                              +>>> moduleNames
                                                                                                                                                                               ['sys', 'os', 're', 'unittest']
                                                                                                                                                                              ->>> modules = map(__import__, moduleNames)        2
                                                                                                                                                                              ->>> modules   3
                                                                                                                                                                              -[<module 'sys' (built-in)>,
                                                                                                                                                                              +>>> modules = map(__import__, moduleNames)        
                                                                                                                                                                              +>>> modules   
                                                                                                                                                                              +[<module 'sys' (built-in)>,
                                                                                                                                                                               <module 'os' from 'c:\Python22\lib\os.pyc'>,
                                                                                                                                                                               <module 're' from 'c:\Python22\lib\re.pyc'>,
                                                                                                                                                                               <module 'unittest' from 'c:\Python22\lib\unittest.pyc'>]
                                                                                                                                                                              ->>> modules[0].version          4
                                                                                                                                                                              +>>> modules[0].version          
                                                                                                                                                                               '2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)]'
                                                                                                                                                                              ->>> import sys
                                                                                                                                                                              ->>> sys.version
                                                                                                                                                                              +>>> import sys
                                                                                                                                                                              +>>> sys.version
                                                                                                                                                                               '2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)]'
                                                                                                                                                                              -
                                                                                                                                                                              - - - - - - - - - - - - - - - - - -
                                                                                                                                                                              1 -moduleNames is just a list of strings. Nothing fancy, except that the strings happen to be names of modules that you could import, if +
                                                                                                                                                                              +
                                                                                                                                                                                +
                                                                                                                                                                              1. moduleNames is just a list of strings. Nothing fancy, except that the strings happen to be names of modules that you could import, if you wanted to. -
                                                                                                                                                                              2 -Surprise, you wanted to import them, and you did, by mapping the __import__ function onto the list. Remember, this takes each element of the list (moduleNames) and calls the function (__import__) over and over, once with each element of the list, builds a list of the return values, and returns the result. -
                                                                                                                                                                              3 -So now from a list of strings, you've created a list of actual modules. (Your paths may be different, depending on your operating +
                                                                                                                                                                            3. Surprise, you wanted to import them, and you did, by mapping the __import__ function onto the list. Remember, this takes each element of the list (moduleNames) and calls the function (__import__) over and over, once with each element of the list, builds a list of the return values, and returns the result. +
                                                                                                                                                                            4. So now from a list of strings, you've created a list of actual modules. (Your paths may be different, depending on your operating system, where you installed Python, the phase of the moon, etc.) -
                                                                                                                                                                            5. 4 -To drive home the point that these are real modules, let's look at some module attributes. Remember, modules[0] is the sys module, so modules[0].version is sys.version. All the other attributes and methods of these modules are also available. There's nothing magic about the import statement, and there's nothing magic about modules. Modules are objects. Everything is an object. -
                                                                                                                                                                              +
                                                                                                                                                                            6. To drive home the point that these are real modules, let's look at some module attributes. Remember, modules[0] is the sys module, so modules[0].version is sys.version. All the other attributes and methods of these modules are also available. There's nothing magic about the import statement, and there's nothing magic about modules. Modules are objects. Everything is an object.

                                                                                                                                                                              Now you should be able to put this all together and figure out what most of this chapter's code sample is doing.

                                                                                                                                                                              16.7. Putting it all together

                                                                                                                                                                              You've learned enough now to deconstruct the first seven lines of this chapter's code sample: reading a directory and importing selected modules within it. -

                                                                                                                                                                              Example 16.16. The regressionTest function

                                                                                                                                                                              +

                                                                                                                                                                              Example 16.16. The regressionTest function

                                                                                                                                                                              
                                                                                                                                                                               def regressionTest():
                                                                                                                                                                                   path = os.path.abspath(os.path.dirname(sys.argv[0]))   
                                                                                                                                                                                   files = os.listdir(path)             
                                                                                                                                                                              @@ -13396,127 +8680,69 @@ def regressionTest():
                                                                                                                                                                               load = unittest.defaultTestLoader.loadTestsFromModule  
                                                                                                                                                                               return unittest.TestSuite(map(load, modules))          
                                                                                                                                                                               

                                                                                                                                                                              Let's look at it line by line, interactively. Assume that the current directory is c:\diveintopython3\py, which contains the examples that come with this book, including this chapter's script. As you saw in Section 16.2, “Finding the path”, the script directory will end up in the path variable, so let's start hard-code that and go from there. -

                                                                                                                                                                              Example 16.17. Step 1: Get all the files

                                                                                                                                                                              ->>> import sys, os, re, unittest
                                                                                                                                                                              ->>> path = r'c:\diveintopython3\py'
                                                                                                                                                                              ->>> files = os.listdir(path)             
                                                                                                                                                                              ->>> files 1
                                                                                                                                                                              -['BaseHTMLProcessor.py', 'LICENSE.txt', 'apihelper.py', 'apihelpertest.py',
                                                                                                                                                                              +

                                                                                                                                                                              Example 16.17. Step 1: Get all the files

                                                                                                                                                                              +>>> import sys, os, re, unittest
                                                                                                                                                                              +>>> path = r'c:\diveintopython3\py'
                                                                                                                                                                              +>>> files = os.listdir(path)             
                                                                                                                                                                              +>>> files 
                                                                                                                                                                              +['BaseHTMLProcessor.py', 'LICENSE.txt', 'apihelper.py', 'apihelpertest.py',
                                                                                                                                                                               'argecho.py', 'autosize.py', 'builddialectexamples.py', 'dialect.py',
                                                                                                                                                                               'fileinfo.py', 'fullpath.py', 'kgptest.py', 'makerealworddoc.py',
                                                                                                                                                                               'odbchelper.py', 'odbchelpertest.py', 'parsephone.py', 'piglatin.py',
                                                                                                                                                                               'plural.py', 'pluraltest.py', 'pyfontify.py', 'regression.py', 'roman.py', 'romantest.py',
                                                                                                                                                                               'uncurly.py', 'unicode2koi8r.py', 'urllister.py', 'kgp', 'plural', 'roman',
                                                                                                                                                                               'colorize.py']
                                                                                                                                                                              -
                                                                                                                                                                              - - - - - -
                                                                                                                                                                              1 -files is a list of all the files and directories in the script's directory. (If you've been running some of the examples already, +
                                                                                                                                                                              +
                                                                                                                                                                                +
                                                                                                                                                                              1. files is a list of all the files and directories in the script's directory. (If you've been running some of the examples already, you may also see some .pyc files in there as well.) -
                                                                                                                                                                              -

                                                                                                                                                                              Example 16.18. Step 2: Filter to find the files you care about

                                                                                                                                                                              ->>> test = re.compile("test\.py$", re.IGNORECASE)           1
                                                                                                                                                                              ->>> files = filter(test.search, files)    2
                                                                                                                                                                              ->>> files               3
                                                                                                                                                                              +

                                                                                                                                                                              Example 16.18. Step 2: Filter to find the files you care about

                                                                                                                                                                              +>>> test = re.compile("test\.py$", re.IGNORECASE)           
                                                                                                                                                                              +>>> files = filter(test.search, files)    
                                                                                                                                                                              +>>> files               
                                                                                                                                                                               ['apihelpertest.py', 'kgptest.py', 'odbchelpertest.py', 'pluraltest.py', 'romantest.py']
                                                                                                                                                                              -
                                                                                                                                                                              - - - - - - - - - - - - - -
                                                                                                                                                                              1 -This regular expression will match any string that ends with test.py. Note that you need to escape the period, since a period in a regular expression usually means “match any single character”, but you actually want to match a literal period instead. -
                                                                                                                                                                              2 -The compiled regular expression acts like a function, so you can use it to filter the large list of files and directories, +
                                                                                                                                                                              +
                                                                                                                                                                                +
                                                                                                                                                                              1. This regular expression will match any string that ends with test.py. Note that you need to escape the period, since a period in a regular expression usually means “match any single character”, but you actually want to match a literal period instead. +
                                                                                                                                                                              2. The compiled regular expression acts like a function, so you can use it to filter the large list of files and directories, to find the ones that match the regular expression. -
                                                                                                                                                                              3 -And you're left with the list of unit testing scripts, because they were the only ones named SOMETHINGtest.py. -
                                                                                                                                                                              -

                                                                                                                                                                              Example 16.19. Step 3: Map filenames to module names

                                                                                                                                                                              ->>> filenameToModuleName = lambda f: os.path.splitext(f)[0] 1
                                                                                                                                                                              ->>> filenameToModuleName('romantest.py')  2
                                                                                                                                                                              +
                                                                                                                                                                            7. And you're left with the list of unit testing scripts, because they were the only ones named SOMETHINGtest.py. +

                                                                                                                                                                              Example 16.19. Step 3: Map filenames to module names

                                                                                                                                                                              +>>> filenameToModuleName = lambda f: os.path.splitext(f)[0] 
                                                                                                                                                                              +>>> filenameToModuleName('romantest.py')  
                                                                                                                                                                               'romantest'
                                                                                                                                                                              ->>> filenameToModuleName('odchelpertest.py')
                                                                                                                                                                              +>>> filenameToModuleName('odchelpertest.py')
                                                                                                                                                                               'odbchelpertest'
                                                                                                                                                                              ->>> moduleNames = map(filenameToModuleName, files)          3
                                                                                                                                                                              ->>> moduleNames         4
                                                                                                                                                                              +>>> moduleNames = map(filenameToModuleName, files)          
                                                                                                                                                                              +>>> moduleNames         
                                                                                                                                                                               ['apihelpertest', 'kgptest', 'odbchelpertest', 'pluraltest', 'romantest']
                                                                                                                                                                              -
                                                                                                                                                                              - - - - - - - - - - - - - - - - - -
                                                                                                                                                                              1 -As you saw in Section 4.7, “Using lambda Functions”, lambda is a quick-and-dirty way of creating an inline, one-line function. This one takes a filename with an extension and returns +
                                                                                                                                                                              +
                                                                                                                                                                                +
                                                                                                                                                                              1. As you saw in Section 4.7, “Using lambda Functions”, lambda is a quick-and-dirty way of creating an inline, one-line function. This one takes a filename with an extension and returns just the filename part, using the standard library function os.path.splitext that you saw in Example 6.17, “Splitting Pathnames”. -
                                                                                                                                                                              2 -filenameToModuleName is a function. There's nothing magic about lambda functions as opposed to regular functions that you define with a def statement. You can call the filenameToModuleName function like any other, and it does just what you wanted it to do: strips the file extension off of its argument. -
                                                                                                                                                                              3 -Now you can apply this function to each file in the list of unit test files, using map. -
                                                                                                                                                                              4 -And the result is just what you wanted: a list of modules, as strings.
                                                                                                                                                                              -

                                                                                                                                                                              Example 16.20. Step 4: Mapping module names to modules

                                                                                                                                                                              ->>> modules = map(__import__, moduleNames)1
                                                                                                                                                                              ->>> modules             2
                                                                                                                                                                              -[<module 'apihelpertest' from 'apihelpertest.py'>,
                                                                                                                                                                              +
                                                                                                                                                                            8. filenameToModuleName is a function. There's nothing magic about lambda functions as opposed to regular functions that you define with a def statement. You can call the filenameToModuleName function like any other, and it does just what you wanted it to do: strips the file extension off of its argument. +
                                                                                                                                                                            9. Now you can apply this function to each file in the list of unit test files, using map. +
                                                                                                                                                                            10. And the result is just what you wanted: a list of modules, as strings. +

                                                                                                                                                                              Example 16.20. Step 4: Mapping module names to modules

                                                                                                                                                                              +>>> modules = map(__import__, moduleNames)
                                                                                                                                                                              +>>> modules             
                                                                                                                                                                              +[<module 'apihelpertest' from 'apihelpertest.py'>,
                                                                                                                                                                               <module 'kgptest' from 'kgptest.py'>,
                                                                                                                                                                               <module 'odbchelpertest' from 'odbchelpertest.py'>,
                                                                                                                                                                               <module 'pluraltest' from 'pluraltest.py'>,
                                                                                                                                                                               <module 'romantest' from 'romantest.py'>]
                                                                                                                                                                              ->>> modules[-1]         3
                                                                                                                                                                              +>>> modules[-1]         
                                                                                                                                                                               <module 'romantest' from 'romantest.py'>
                                                                                                                                                                              -
                                                                                                                                                                              - - - - - - - - - - - - - -
                                                                                                                                                                              1 -As you saw in Section 16.6, “Dynamically importing modules”, you can use a combination of map and __import__ to map a list of module names (as strings) into actual modules (which you can call or access like any other module). +
                                                                                                                                                                              +
                                                                                                                                                                                +
                                                                                                                                                                              1. As you saw in Section 16.6, “Dynamically importing modules”, you can use a combination of map and __import__ to map a list of module names (as strings) into actual modules (which you can call or access like any other module). -
                                                                                                                                                                              2 -modules is now a list of modules, fully accessible like any other module. -
                                                                                                                                                                              3 -The last module in the list is the romantest module, just as if you had said import romantest. -
                                                                                                                                                                              -

                                                                                                                                                                              Example 16.21. Step 5: Loading the modules into a test suite

                                                                                                                                                                              ->>> load = unittest.defaultTestLoader.loadTestsFromModule  
                                                                                                                                                                              ->>> map(load, modules)   1
                                                                                                                                                                              -[<unittest.TestSuite tests=[
                                                                                                                                                                              +
                                                                                                                                                                            11. modules is now a list of modules, fully accessible like any other module. +
                                                                                                                                                                            12. The last module in the list is the romantest module, just as if you had said import romantest. +

                                                                                                                                                                              Example 16.21. Step 5: Loading the modules into a test suite

                                                                                                                                                                              +>>> load = unittest.defaultTestLoader.loadTestsFromModule  
                                                                                                                                                                              +>>> map(load, modules)   
                                                                                                                                                                              +[<unittest.TestSuite tests=[
                                                                                                                                                                                 <unittest.TestSuite tests=[<apihelpertest.BadInput testMethod=testNoObject>]>,
                                                                                                                                                                                 <unittest.TestSuite tests=[<apihelpertest.KnownValues testMethod=testApiHelper>]>,
                                                                                                                                                                                 <unittest.TestSuite tests=[
                                                                                                                                                                              @@ -13525,44 +8751,27 @@ return unittest.TestSuite(map(load, modules))
                                                                                                                                                                                   ...
                                                                                                                                                                                 ]
                                                                                                                                                                               ]
                                                                                                                                                                              ->>> unittest.TestSuite(map(load, modules)) 2
                                                                                                                                                                              -
                                                                                                                                                                              - - - - - - - - - -
                                                                                                                                                                              1 -These are real module objects. Not only can you access them like any other module, instantiate classes and call functions, +>>> unittest.TestSuite(map(load, modules)) +
                                                                                                                                                                              +
                                                                                                                                                                                +
                                                                                                                                                                              1. These are real module objects. Not only can you access them like any other module, instantiate classes and call functions, you can also introspect into the module to figure out which classes and functions it has in the first place. That's what the loadTestsFromModule method does: it introspects into each module and returns a unittest.TestSuite object for each module. Each TestSuite object actually contains a list of TestSuite objects, one for each TestCase class in your module, and each of those TestSuite objects contains a list of tests, one for each test method in your module. -
                                                                                                                                                                              2 -Finally, you wrap the list of TestSuite objects into one big test suite. The unittest module has no problem traversing this tree of nested test suites within test suites; eventually it gets down to an individual +
                                                                                                                                                                            13. Finally, you wrap the list of TestSuite objects into one big test suite. The unittest module has no problem traversing this tree of nested test suites within test suites; eventually it gets down to an individual test method and executes it, verifies that it passes or fails, and moves on to the next one. -
                                                                                                                                                                            14. This introspection process is what the unittest module usually does for us. Remember that magic-looking unittest.main() function that our individual test modules called to kick the whole thing off? unittest.main() actually creates an instance of unittest.TestProgram, which in turn creates an instance of a unittest.defaultTestLoader and loads it up with the module that called it. (How does it get a reference to the module that called it if you don't give it one? By using the equally-magic __import__('__main__') command, which dynamically imports the currently-running module. I could write a book on all the tricks and techniques used in the unittest module, but then I'd never finish this one.) -

                                                                                                                                                                              Example 16.22. Step 6: Telling unittest to use your test suite

                                                                                                                                                                              +

                                                                                                                                                                              Example 16.22. Step 6: Telling unittest to use your test suite

                                                                                                                                                                              
                                                                                                                                                                               if __name__ == "__main__": 
                                                                                                                                                                              -    unittest.main(defaultTest="regressionTest") 1
                                                                                                                                                                              -
                                                                                                                                                                              - - - - - -
                                                                                                                                                                              1 -Instead of letting the unittest module do all its magic for us, you've done most of it yourself. You've created a function (regressionTest) that imports the modules yourself, calls unittest.defaultTestLoader yourself, and wraps it all up in a test suite. Now all you need to do is tell unittest that, instead of looking for tests and building a test suite in the usual way, it should just call the regressionTest function, which returns a ready-to-use TestSuite. -
                                                                                                                                                                              + unittest.main(defaultTest="regressionTest") +
                                                                                                                                                                              +
                                                                                                                                                                                +
                                                                                                                                                                              1. Instead of letting the unittest module do all its magic for us, you've done most of it yourself. You've created a function (regressionTest) that imports the modules yourself, calls unittest.defaultTestLoader yourself, and wraps it all up in a test suite. Now all you need to do is tell unittest that, instead of looking for tests and building a test suite in the usual way, it should just call the regressionTest function, which returns a ready-to-use TestSuite.

                                                                                                                                                                                16.8. Summary

                                                                                                                                                                                The regression.py program and its output should now make perfect sense.

                                                                                                                                                                                You should now feel comfortable doing all of these things: -

                                                                                                                                                                                +
                                                                                                                                                                                -


                                                                                                                                                                                -
                                                                                                                                                                                +


                                                                                                                                                                                +

                                                                                                                                                                                [7] Technically, the second argument to filter can be any sequence, including lists, tuples, and custom classes that act like lists by defining the __getitem__ special method. If possible, filter will return the same datatype as you give it, so filtering a list returns a list, but filtering a tuple returns a tuple. -

                                                                                                                                                                                +

                                                                                                                                                                                [8] Again, I should point out that map can take a list, a tuple, or any object that acts like a sequence. See previous footnote about filter. -

                                                                                                                                                                                +

                                                                                                                                                                                Chapter 17. Dynamic functions

                                                                                                                                                                                17.1. Diving in

                                                                                                                                                                                I want to talk about plural nouns. Also, functions that return other functions, advanced regular expressions, and generators. @@ -13590,7 +8799,7 @@ plural nouns are varied and complex. There are rules, and then there are excepti to the exceptions.

                                                                                                                                                                                If you grew up in an English-speaking country or learned English in a formal school setting, you're probably familiar with the basic rules: -

                                                                                                                                                                                +
                                                                                                                                                                                1. If a word ends in S, X, or Z, add ES. “Bass” becomes “basses”, “fax” becomes “faxes”, and “waltz” becomes “waltzes”. @@ -13609,165 +8818,90 @@ you'll inevitably need to add more rules, and you may eventually need to add mor

                                                                                                                                                                                  17.2. plural.py, stage 1

                                                                                                                                                                                  So you're looking at words, which at least in English are strings of characters. And you have rules that say you need to find different combinations of characters, and then do different things to them. This sounds like a job for regular expressions. -

                                                                                                                                                                                  Example 17.1. plural1.py

                                                                                                                                                                                  +

                                                                                                                                                                                  Example 17.1. plural1.py

                                                                                                                                                                                  
                                                                                                                                                                                   import re
                                                                                                                                                                                   
                                                                                                                                                                                   def plural(noun):          
                                                                                                                                                                                  -    if re.search('[sxz]$', noun):             1
                                                                                                                                                                                  -        return re.sub('$', 'es', noun)        2
                                                                                                                                                                                  +    if re.search('[sxz]$', noun):             
                                                                                                                                                                                  +        return re.sub('$', 'es', noun)        
                                                                                                                                                                                       elif re.search('[^aeioudgkprt]h$', noun):
                                                                                                                                                                                           return re.sub('$', 'es', noun)       
                                                                                                                                                                                       elif re.search('[^aeiou]y$', noun):      
                                                                                                                                                                                           return re.sub('y$', 'ies', noun)     
                                                                                                                                                                                       else:
                                                                                                                                                                                           return noun + 's'  
                                                                                                                                                                                  -
                                                                                                                                                                                  - - - - - - - - - -
                                                                                                                                                                                  1 -OK, this is a regular expression, but it uses a syntax you didn't see in Chapter 7, Regular Expressions. The square brackets mean “match exactly one of these characters”. So [sxz] means “s, or x, or z”, but only one of them. The $ should be familiar; it matches the end of string. So you're checking to see if noun ends with s, x, or z. -
                                                                                                                                                                                  2 -This re.sub function performs regular expression-based string substitutions. Let's look at it in more detail. -
                                                                                                                                                                                  -

                                                                                                                                                                                  Example 17.2. Introducing re.sub

                                                                                                                                                                                  ->>> import re
                                                                                                                                                                                  ->>> re.search('[abc]', 'Mark')   1
                                                                                                                                                                                  +
                                                                                                                                                                                  +
                                                                                                                                                                                    +
                                                                                                                                                                                  1. OK, this is a regular expression, but it uses a syntax you didn't see in Chapter 7, Regular Expressions. The square brackets mean “match exactly one of these characters”. So [sxz] means “s, or x, or z”, but only one of them. The $ should be familiar; it matches the end of string. So you're checking to see if noun ends with s, x, or z. +
                                                                                                                                                                                  2. This re.sub function performs regular expression-based string substitutions. Let's look at it in more detail. +

                                                                                                                                                                                    Example 17.2. Introducing re.sub

                                                                                                                                                                                    +>>> import re
                                                                                                                                                                                    +>>> re.search('[abc]', 'Mark')   
                                                                                                                                                                                     <_sre.SRE_Match object at 0x001C1FA8>
                                                                                                                                                                                    ->>> re.sub('[abc]', 'o', 'Mark') 2
                                                                                                                                                                                    +>>> re.sub('[abc]', 'o', 'Mark') 
                                                                                                                                                                                     'Mork'
                                                                                                                                                                                    ->>> re.sub('[abc]', 'o', 'rock') 3
                                                                                                                                                                                    +>>> re.sub('[abc]', 'o', 'rock') 
                                                                                                                                                                                     'rook'
                                                                                                                                                                                    ->>> re.sub('[abc]', 'o', 'caps') 4
                                                                                                                                                                                    +>>> re.sub('[abc]', 'o', 'caps') 
                                                                                                                                                                                     'oops'
                                                                                                                                                                                    -
                                                                                                                                                                                    - - - - - - - - - - - - - - - - - -
                                                                                                                                                                                    1 -Does the string Mark contain a, b, or c? Yes, it contains a. -
                                                                                                                                                                                    2 -OK, now find a, b, or c, and replace it with o. Mark becomes Mork. -
                                                                                                                                                                                    3 -The same function turns rock into rook. -
                                                                                                                                                                                    4 -You might think this would turn caps into oaps, but it doesn't. re.sub replaces all of the matches, not just the first one. So this regular expression turns caps into oops, because both the c and the a get turned into o. -
                                                                                                                                                                                    -

                                                                                                                                                                                    Example 17.3. Back to plural1.py

                                                                                                                                                                                    +
                                                                                                                                                                                    +
                                                                                                                                                                                      +
                                                                                                                                                                                    1. Does the string Mark contain a, b, or c? Yes, it contains a. +
                                                                                                                                                                                    2. OK, now find a, b, or c, and replace it with o. Mark becomes Mork. +
                                                                                                                                                                                    3. The same function turns rock into rook. +
                                                                                                                                                                                    4. You might think this would turn caps into oaps, but it doesn't. re.sub replaces all of the matches, not just the first one. So this regular expression turns caps into oops, because both the c and the a get turned into o. +

                                                                                                                                                                                      Example 17.3. Back to plural1.py

                                                                                                                                                                                      
                                                                                                                                                                                       import re
                                                                                                                                                                                       
                                                                                                                                                                                       def plural(noun):          
                                                                                                                                                                                           if re.search('[sxz]$', noun):            
                                                                                                                                                                                      -        return re.sub('$', 'es', noun)        1
                                                                                                                                                                                      -    elif re.search('[^aeioudgkprt]h$', noun): 2
                                                                                                                                                                                      -        return re.sub('$', 'es', noun)        3
                                                                                                                                                                                      +        return re.sub('$', 'es', noun)        
                                                                                                                                                                                      +    elif re.search('[^aeioudgkprt]h$', noun): 
                                                                                                                                                                                      +        return re.sub('$', 'es', noun)        
                                                                                                                                                                                           elif re.search('[^aeiou]y$', noun):      
                                                                                                                                                                                               return re.sub('y$', 'ies', noun)     
                                                                                                                                                                                           else:
                                                                                                                                                                                               return noun + 's'  
                                                                                                                                                                                      -
                                                                                                                                                                                      - - - - - - - - - - - - - -
                                                                                                                                                                                      1 -Back to the plural function. What are you doing? You're replacing the end of string with es. In other words, adding es to the string. You could accomplish the same thing with string concatenation, for example noun + 'es', but I'm using regular expressions for everything, for consistency, for reasons that will become clear later in the chapter. -
                                                                                                                                                                                      2 -Look closely, this is another new variation. The ^ as the first character inside the square brackets means something special: negation. [^abc] means “any single character except a, b, or c”. So [^aeioudgkprt] means any character except a, e, i, o, u, d, g, k, p, r, or t. Then that character needs to be followed by h, followed by end of string. You're looking for words that end in H where the H can be heard. -
                                                                                                                                                                                      3 -Same pattern here: match words that end in Y, where the character before the Y is not a, e, i, o, or u. You're looking for words that end in Y that sounds like I. -
                                                                                                                                                                                      -

                                                                                                                                                                                      Example 17.4. More on negation regular expressions

                                                                                                                                                                                      ->>> import re
                                                                                                                                                                                      ->>> re.search('[^aeiou]y$', 'vacancy') 1
                                                                                                                                                                                      +
                                                                                                                                                                                      +
                                                                                                                                                                                        +
                                                                                                                                                                                      1. Back to the plural function. What are you doing? You're replacing the end of string with es. In other words, adding es to the string. You could accomplish the same thing with string concatenation, for example noun + 'es', but I'm using regular expressions for everything, for consistency, for reasons that will become clear later in the chapter. +
                                                                                                                                                                                      2. Look closely, this is another new variation. The ^ as the first character inside the square brackets means something special: negation. [^abc] means “any single character except a, b, or c”. So [^aeioudgkprt] means any character except a, e, i, o, u, d, g, k, p, r, or t. Then that character needs to be followed by h, followed by end of string. You're looking for words that end in H where the H can be heard. +
                                                                                                                                                                                      3. Same pattern here: match words that end in Y, where the character before the Y is not a, e, i, o, or u. You're looking for words that end in Y that sounds like I. +

                                                                                                                                                                                        Example 17.4. More on negation regular expressions

                                                                                                                                                                                        +>>> import re
                                                                                                                                                                                        +>>> re.search('[^aeiou]y$', 'vacancy') 
                                                                                                                                                                                         <_sre.SRE_Match object at 0x001C1FA8>
                                                                                                                                                                                        ->>> re.search('[^aeiou]y$', 'boy')     2
                                                                                                                                                                                        ->>> 
                                                                                                                                                                                        ->>> re.search('[^aeiou]y$', 'day')
                                                                                                                                                                                        ->>> 
                                                                                                                                                                                        ->>> re.search('[^aeiou]y$', 'pita')    3
                                                                                                                                                                                        ->>> 
                                                                                                                                                                                        -
                                                                                                                                                                                        - - - - - - - - - - - - - -
                                                                                                                                                                                        1 -vacancy matches this regular expression, because it ends in cy, and c is not a, e, i, o, or u. -
                                                                                                                                                                                        2 -boy does not match, because it ends in oy, and you specifically said that the character before the y could not be o. day does not match, because it ends in ay. -
                                                                                                                                                                                        3 -pita does not match, because it does not end in y. -
                                                                                                                                                                                        -

                                                                                                                                                                                        Example 17.5. More on re.sub

                                                                                                                                                                                        ->>> re.sub('y$', 'ies', 'vacancy')              1
                                                                                                                                                                                        +>>> re.search('[^aeiou]y$', 'boy')     
                                                                                                                                                                                        +>>> 
                                                                                                                                                                                        +>>> re.search('[^aeiou]y$', 'day')
                                                                                                                                                                                        +>>> 
                                                                                                                                                                                        +>>> re.search('[^aeiou]y$', 'pita')    
                                                                                                                                                                                        +>>> 
                                                                                                                                                                                        +
                                                                                                                                                                                        +
                                                                                                                                                                                          +
                                                                                                                                                                                        1. vacancy matches this regular expression, because it ends in cy, and c is not a, e, i, o, or u. +
                                                                                                                                                                                        2. boy does not match, because it ends in oy, and you specifically said that the character before the y could not be o. day does not match, because it ends in ay. +
                                                                                                                                                                                        3. pita does not match, because it does not end in y. +

                                                                                                                                                                                          Example 17.5. More on re.sub

                                                                                                                                                                                          +>>> re.sub('y$', 'ies', 'vacancy')              
                                                                                                                                                                                           'vacancies'
                                                                                                                                                                                          ->>> re.sub('y$', 'ies', 'agency')
                                                                                                                                                                                          +>>> re.sub('y$', 'ies', 'agency')
                                                                                                                                                                                           'agencies'
                                                                                                                                                                                          ->>> re.sub('([^aeiou])y$', r'\1ies', 'vacancy') 2
                                                                                                                                                                                          +>>> re.sub('([^aeiou])y$', r'\1ies', 'vacancy') 
                                                                                                                                                                                           'vacancies'
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - -
                                                                                                                                                                                          1 -This regular expression turns vacancy into vacancies and agency into agencies, which is what you wanted. Note that it would also turn boy into boies, but that will never happen in the function because you did that re.search first to find out whether you should do this re.sub. -
                                                                                                                                                                                          2 -Just in passing, I want to point out that it is possible to combine these two regular expressions (one to find out if the +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. This regular expression turns vacancy into vacancies and agency into agencies, which is what you wanted. Note that it would also turn boy into boies, but that will never happen in the function because you did that re.search first to find out whether you should do this re.sub. +
                                                                                                                                                                                          2. Just in passing, I want to point out that it is possible to combine these two regular expressions (one to find out if the rule applies, and another to actually apply it) into a single regular expression. Here's what that would look like. Most of it should look familiar: you're using a remembered group, which you learned in Section 7.6, “Case study: Parsing Phone Numbers”, to remember the character before the y. Then in the substitution string, you use a new syntax, \1, which means “hey, that first group you remembered? put it here”. In this case, you remember the c before the y, and then when you do the substitution, you substitute c in place of c, and ies in place of y. (If you have more than one remembered group, you can use \2 and \3 and so on.) -

                                                                                                                                                                                          Regular expression substitutions are extremely powerful, and the \1 syntax makes them even more powerful. But combining the entire operation into one regular expression is also much harder to read, and it doesn't directly map to the way you first described the pluralizing rules. You originally laid out rules like “if the word ends in S, X, or Z, then add ES”. And if you look at this function, you have two lines of code that say “if the word ends in S, X, or Z, then add ES”. It doesn't get much more direct than that.

                                                                                                                                                                                          17.3. plural.py, stage 2

                                                                                                                                                                                          Now you're going to add a level of abstraction. You started by defining a list of rules: if this, then do that, otherwise go to the next rule. Let's temporarily complicate part of the program so you can simplify another part. -

                                                                                                                                                                                          Example 17.6. plural2.py

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.6. plural2.py

                                                                                                                                                                                          
                                                                                                                                                                                           import re
                                                                                                                                                                                           
                                                                                                                                                                                           def match_sxz(noun):        
                                                                                                                                                                                          @@ -13798,43 +8932,22 @@ rules = ((match_sxz, apply_sxz),
                                                                                                                                                                                                    (match_h, apply_h),
                                                                                                                                                                                                    (match_y, apply_y),
                                                                                                                                                                                                    (match_default, apply_default)
                                                                                                                                                                                          -         ) 1
                                                                                                                                                                                          +         ) 
                                                                                                                                                                                           
                                                                                                                                                                                           def plural(noun):           
                                                                                                                                                                                          -    for matchesRule, applyRule in rules:       2
                                                                                                                                                                                          -        if matchesRule(noun):3
                                                                                                                                                                                          -            return applyRule(noun)             4
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - - - - - - - - - -
                                                                                                                                                                                          1 -This version looks more complicated (it's certainly longer), but it does exactly the same thing: try to match four different + for matchesRule, applyRule in rules: + if matchesRule(noun): + return applyRule(noun) +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. This version looks more complicated (it's certainly longer), but it does exactly the same thing: try to match four different rules, in order, and apply the appropriate regular expression when a match is found. The difference is that each individual match and apply rule is defined in its own function, and the functions are then listed in this rules variable, which is a tuple of tuples. -
                                                                                                                                                                                          2 -Using a for loop, you can pull out the match and apply rules two at a time (one match, one apply) from the rules tuple. On the first iteration of the for loop, matchesRule will get match_sxz, and applyRule will get apply_sxz. On the second iteration (assuming you get that far), matchesRule will be assigned match_h, and applyRule will be assigned apply_h. -
                                                                                                                                                                                          3 -Remember that everything in Python is an object, including functions. rules contains actual functions; not names of functions, but actual functions. When they get assigned in the for loop, then matchesRule and applyRule are actual functions that you can call. So on the first iteration of the for loop, this is equivalent to calling matches_sxz(noun). -
                                                                                                                                                                                          4 -On the first iteration of the for loop, this is equivalent to calling apply_sxz(noun), and so forth. -
                                                                                                                                                                                          +
                                                                                                                                                                                        4. Using a for loop, you can pull out the match and apply rules two at a time (one match, one apply) from the rules tuple. On the first iteration of the for loop, matchesRule will get match_sxz, and applyRule will get apply_sxz. On the second iteration (assuming you get that far), matchesRule will be assigned match_h, and applyRule will be assigned apply_h. +
                                                                                                                                                                                        5. Remember that everything in Python is an object, including functions. rules contains actual functions; not names of functions, but actual functions. When they get assigned in the for loop, then matchesRule and applyRule are actual functions that you can call. So on the first iteration of the for loop, this is equivalent to calling matches_sxz(noun). +
                                                                                                                                                                                        6. On the first iteration of the for loop, this is equivalent to calling apply_sxz(noun), and so forth.

                                                                                                                                                                                          If this additional level of abstraction is confusing, try unrolling the function to see the equivalence. This for loop is equivalent to the following: -

                                                                                                                                                                                          Example 17.7. Unrolling the plural function

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.7. Unrolling the plural function

                                                                                                                                                                                          
                                                                                                                                                                                           def plural(noun):
                                                                                                                                                                                               if match_sxz(noun):
                                                                                                                                                                                                   return apply_sxz(noun)
                                                                                                                                                                                          @@ -13852,7 +8965,7 @@ the function. Well, in the previous example, it would require adding an if
                                                                                                                                                                                           

                                                                                                                                                                                          17.4. plural.py, stage 3

                                                                                                                                                                                          Defining separate named functions for each match and apply rule isn't really necessary. You never call them directly; you define them in the rules list and call them through there. Let's streamline the rules definition by anonymizing those functions. -

                                                                                                                                                                                          Example 17.8. plural3.py

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.8. plural3.py

                                                                                                                                                                                          
                                                                                                                                                                                           import re
                                                                                                                                                                                           
                                                                                                                                                                                           rules = \
                                                                                                                                                                                          @@ -13873,94 +8986,56 @@ rules = \
                                                                                                                                                                                                lambda word: re.search('$', word),
                                                                                                                                                                                                lambda word: re.sub('$', 's', word)
                                                                                                                                                                                               )
                                                                                                                                                                                          -   )       1
                                                                                                                                                                                          +   )       
                                                                                                                                                                                           
                                                                                                                                                                                           def plural(noun):           
                                                                                                                                                                                          -    for matchesRule, applyRule in rules:       2
                                                                                                                                                                                          +    for matchesRule, applyRule in rules:       
                                                                                                                                                                                                   if matchesRule(noun):                 
                                                                                                                                                                                                       return applyRule(noun)            
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - -
                                                                                                                                                                                          1 -This is the same set of rules as you defined in stage 2. The only difference is that instead of defining named functions +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. This is the same set of rules as you defined in stage 2. The only difference is that instead of defining named functions like match_sxz and apply_sxz, you have “inlined” those function definitions directly into the rules list itself, using lambda functions. -
                                                                                                                                                                                          2 -Note that the plural function hasn't changed at all. It iterates through a set of rule functions, checks the first rule, and if it returns a +
                                                                                                                                                                                        7. Note that the plural function hasn't changed at all. It iterates through a set of rule functions, checks the first rule, and if it returns a true value, calls the second rule and returns the value. Same as above, word for word. The only difference is that the rule functions were defined inline, anonymously, using lambda functions. But the plural function doesn't care how they were defined; it just gets a list of rules and blindly works through them. -
                                                                                                                                                                                        8. Now to add a new rule, all you need to do is define the functions directly in the rules list itself: one match rule, and one apply rule. But defining the rule functions inline like this makes it very clear that you have some unnecessary duplication here. You have four pairs of functions, and they all follow the same pattern. The match function is a single call to re.search, and the apply function is a single call to re.sub. Let's factor out these similarities.

                                                                                                                                                                                          17.5. plural.py, stage 4

                                                                                                                                                                                          Let's factor out the duplication in the code so that defining new rules can be easier. -

                                                                                                                                                                                          Example 17.9. plural4.py

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.9. plural4.py

                                                                                                                                                                                          
                                                                                                                                                                                           import re
                                                                                                                                                                                           
                                                                                                                                                                                           def buildMatchAndApplyFunctions((pattern, search, replace)):  
                                                                                                                                                                                          -    matchFunction = lambda word: re.search(pattern, word)      1
                                                                                                                                                                                          -    applyFunction = lambda word: re.sub(search, replace, word) 2
                                                                                                                                                                                          -    return (matchFunction, applyFunction)    3
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - - - - - -
                                                                                                                                                                                          1 -buildMatchAndApplyFunctions is a function that builds other functions dynamically. It takes pattern, search and replace (actually it takes a tuple, but more on that in a minute), and you can build the match function using the lambda syntax to be a function that takes one parameter (word) and calls re.search with the pattern that was passed to the buildMatchAndApplyFunctions function, and the word that was passed to the match function you're building. Whoa. -
                                                                                                                                                                                          2 -Building the apply function works the same way. The apply function is a function that takes one parameter, and calls re.sub with the search and replace parameters that were passed to the buildMatchAndApplyFunctions function, and the word that was passed to the apply function you're building. This technique of using the values of outside parameters within a + matchFunction = lambda word: re.search(pattern, word) + applyFunction = lambda word: re.sub(search, replace, word) + return (matchFunction, applyFunction) +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. buildMatchAndApplyFunctions is a function that builds other functions dynamically. It takes pattern, search and replace (actually it takes a tuple, but more on that in a minute), and you can build the match function using the lambda syntax to be a function that takes one parameter (word) and calls re.search with the pattern that was passed to the buildMatchAndApplyFunctions function, and the word that was passed to the match function you're building. Whoa. +
                                                                                                                                                                                          2. Building the apply function works the same way. The apply function is a function that takes one parameter, and calls re.sub with the search and replace parameters that were passed to the buildMatchAndApplyFunctions function, and the word that was passed to the apply function you're building. This technique of using the values of outside parameters within a dynamic function is called closures. You're essentially defining constants within the apply function you're building: it takes one parameter (word), but it then acts on that plus two other values (search and replace) which were set when you defined the apply function. -
                                                                                                                                                                                          3 -Finally, the buildMatchAndApplyFunctions function returns a tuple of two values: the two functions you just created. The constants you defined within those functions +
                                                                                                                                                                                        9. Finally, the buildMatchAndApplyFunctions function returns a tuple of two values: the two functions you just created. The constants you defined within those functions (pattern within matchFunction, and search and replace within applyFunction) stay with those functions, even after you return from buildMatchAndApplyFunctions. That's insanely cool. -
                                                                                                                                                                                        10. If this is incredibly confusing (and it should be, this is weird stuff), it may become clearer when you see how to use it. -

                                                                                                                                                                                          Example 17.10. plural4.py continued

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.10. plural4.py continued

                                                                                                                                                                                          
                                                                                                                                                                                           patterns = \
                                                                                                                                                                                             (
                                                                                                                                                                                               ('[sxz]$', '$', 'es'),
                                                                                                                                                                                               ('[^aeioudgkprt]h$', '$', 'es'),
                                                                                                                                                                                               ('(qu|[^aeiou])y$', 'y$', 'ies'),
                                                                                                                                                                                               ('$', '$', 's')
                                                                                                                                                                                          -  )             1
                                                                                                                                                                                          -rules = map(buildMatchAndApplyFunctions, patterns)  2
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - -
                                                                                                                                                                                          1 -Our pluralization rules are now defined as a series of strings (not functions). The first string is the regular expression + ) +rules = map(buildMatchAndApplyFunctions, patterns) +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. Our pluralization rules are now defined as a series of strings (not functions). The first string is the regular expression that you would use in re.search to see if this rule matches; the second and third are the search and replace expressions you would use in re.sub to actually apply the rule to turn a noun into its plural. -
                                                                                                                                                                                          2 -This line is magic. It takes the list of strings in patterns and turns them into a list of functions. How? By mapping the strings to the buildMatchAndApplyFunctions function, which just happens to take three strings as parameters and return a tuple of two functions. This means that rules ends up being exactly the same as the previous example: a list of tuples, where each tuple is a pair of functions, where +
                                                                                                                                                                                        11. This line is magic. It takes the list of strings in patterns and turns them into a list of functions. How? By mapping the strings to the buildMatchAndApplyFunctions function, which just happens to take three strings as parameters and return a tuple of two functions. This means that rules ends up being exactly the same as the previous example: a list of tuples, where each tuple is a pair of functions, where the first function is the match function that calls re.search, and the second function is the apply function that calls re.sub. -
                                                                                                                                                                                        12. I swear I am not making this up: rules ends up with exactly the same list of functions as the previous example. Unroll the rules definition, and you'll get this: -

                                                                                                                                                                                          Example 17.11. Unrolling the rules definition

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.11. Unrolling the rules definition

                                                                                                                                                                                          
                                                                                                                                                                                           rules = \
                                                                                                                                                                                             (
                                                                                                                                                                                               (
                                                                                                                                                                                          @@ -13980,55 +9055,37 @@ rules = \
                                                                                                                                                                                                lambda word: re.sub('$', 's', word)
                                                                                                                                                                                               )
                                                                                                                                                                                              )      
                                                                                                                                                                                          -

                                                                                                                                                                                          Example 17.12. plural4.py, finishing up

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.12. plural4.py, finishing up

                                                                                                                                                                                          
                                                                                                                                                                                           def plural(noun):                
                                                                                                                                                                                          -    for matchesRule, applyRule in rules:            1
                                                                                                                                                                                          +    for matchesRule, applyRule in rules:            
                                                                                                                                                                                                   if matchesRule(noun):    
                                                                                                                                                                                                       return applyRule(noun)                 
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - -
                                                                                                                                                                                          1 -Since the rules list is the same as the previous example, it should come as no surprise that the plural function hasn't changed. Remember, it's completely generic; it takes a list of rule functions and calls them in order. +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. Since the rules list is the same as the previous example, it should come as no surprise that the plural function hasn't changed. Remember, it's completely generic; it takes a list of rule functions and calls them in order. It doesn't care how the rules are defined. In stage 2, they were defined as seperate named functions. In stage 3, they were defined as anonymous lambda functions. Now in stage 4, they are built dynamically by mapping the buildMatchAndApplyFunctions function onto a list of raw strings. Doesn't matter; the plural function still works the same way. -

                                                                                                                                                                                          Just in case that wasn't mind-blowing enough, I must confess that there was a subtlety in the definition of buildMatchAndApplyFunctions that I skipped over. Let's go back and take another look. -

                                                                                                                                                                                          Example 17.13. Another look at buildMatchAndApplyFunctions

                                                                                                                                                                                          -def buildMatchAndApplyFunctions((pattern, search, replace)):   1
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - -
                                                                                                                                                                                          1 -Notice the double parentheses? This function doesn't actually take three parameters; it actually takes one parameter, a tuple +

                                                                                                                                                                                          Example 17.13. Another look at buildMatchAndApplyFunctions

                                                                                                                                                                                          
                                                                                                                                                                                          +def buildMatchAndApplyFunctions((pattern, search, replace)):   
                                                                                                                                                                                          +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. Notice the double parentheses? This function doesn't actually take three parameters; it actually takes one parameter, a tuple of three elements. But the tuple is expanded when the function is called, and the three elements of the tuple are each assigned to different variables: pattern, search, and replace. Confused yet? Let's see it in action. -
                                                                                                                                                                                          -

                                                                                                                                                                                          Example 17.14. Expanding tuples when calling functions

                                                                                                                                                                                          ->>> def foo((a, b, c)):
                                                                                                                                                                                          -...    print c
                                                                                                                                                                                          -...    print b
                                                                                                                                                                                          -...    print a
                                                                                                                                                                                          ->>> parameters = ('apple', 'bear', 'catnap')
                                                                                                                                                                                          ->>> foo(parameters) 1
                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.14. Expanding tuples when calling functions

                                                                                                                                                                                          +>>> def foo((a, b, c)):
                                                                                                                                                                                          +...    print c
                                                                                                                                                                                          +...    print b
                                                                                                                                                                                          +...    print a
                                                                                                                                                                                          +>>> parameters = ('apple', 'bear', 'catnap')
                                                                                                                                                                                          +>>> foo(parameters) 
                                                                                                                                                                                           catnap
                                                                                                                                                                                           bear
                                                                                                                                                                                           apple
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - -
                                                                                                                                                                                          1 -The proper way to call the function foo is with a tuple of three elements. When the function is called, the elements are assigned to different local variables within +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. The proper way to call the function foo is with a tuple of three elements. When the function is called, the elements are assigned to different local variables within foo. -

                                                                                                                                                                                          Now let's go back and see why this auto-tuple-expansion trick was necessary. patterns was a list of tuples, and each tuple had three elements. When you called map(buildMatchAndApplyFunctions, patterns), that means that buildMatchAndApplyFunctions is not getting called with three parameters. Using map to map a single list onto a function always calls the function with a single parameter: each element of the list. In the case of patterns, each element of the list is a tuple, so buildMatchAndApplyFunctions always gets called with the tuple, and you use the auto-tuple-expansion trick in the definition of buildMatchAndApplyFunctions to assign the elements of that tuple to named variables that you can work with.

                                                                                                                                                                                          17.6. plural.py, stage 5

                                                                                                                                                                                          @@ -14038,78 +9095,47 @@ apple

                                                                                                                                                                                          First, let's create a text file that contains the rules you want. No fancy data structures, just space- (or tab-)delimited strings in three columns. You'll call it rules.en; “en” stands for English. These are the rules for pluralizing English nouns. You could add other rule files for other languages later. -

                                                                                                                                                                                          Example 17.15. rules.en

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.15. rules.en

                                                                                                                                                                                          
                                                                                                                                                                                           [sxz]$$               es
                                                                                                                                                                                           [^aeioudgkprt]h$        $               es
                                                                                                                                                                                           [^aeiou]y$              y$              ies
                                                                                                                                                                                           $     $               s
                                                                                                                                                                                           

                                                                                                                                                                                          Now let's see how you can use this rules file. -

                                                                                                                                                                                          Example 17.16. plural5.py

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.16. plural5.py

                                                                                                                                                                                          
                                                                                                                                                                                           import re
                                                                                                                                                                                           import string               
                                                                                                                                                                                           
                                                                                                                                                                                           def buildRule((pattern, search, replace)):    
                                                                                                                                                                                          -    return lambda word: re.search(pattern, word) and re.sub(search, replace, word) 1
                                                                                                                                                                                          +    return lambda word: re.search(pattern, word) and re.sub(search, replace, word) 
                                                                                                                                                                                           
                                                                                                                                                                                          -def plural(noun, language='en'):           2
                                                                                                                                                                                          -    lines = file('rules.%s' % language).readlines()          3
                                                                                                                                                                                          -    patterns = map(string.split, lines)    4
                                                                                                                                                                                          -    rules = map(buildRule, patterns)       5
                                                                                                                                                                                          +def plural(noun, language='en'):           
                                                                                                                                                                                          +    lines = file('rules.%s' % language).readlines()          
                                                                                                                                                                                          +    patterns = map(string.split, lines)    
                                                                                                                                                                                          +    rules = map(buildRule, patterns)       
                                                                                                                                                                                               for rule in rules:  
                                                                                                                                                                                          -        result = rule(noun)                6
                                                                                                                                                                                          +        result = rule(noun)                
                                                                                                                                                                                                   if result: return result          
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                                                                                          1 -You're still using the closures technique here (building a function dynamically that uses variables defined outside the function), +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. You're still using the closures technique here (building a function dynamically that uses variables defined outside the function), but now you've combined the separate match and apply functions into one. (The reason for this change will become clear in the next section.) This will let you accomplish the same thing as having two functions, but you'll need to call it differently, as you'll see in a minute. -
                                                                                                                                                                                          2 -Our plural function now takes an optional second parameter, language, which defaults to en. -
                                                                                                                                                                                          3 -You use the language parameter to construct a filename, then open the file and read the contents into a list. If language is en, then you'll open the rules.en file, read the entire thing, break it up by carriage returns, and return a list. Each line of the file will be one element +
                                                                                                                                                                                        13. Our plural function now takes an optional second parameter, language, which defaults to en. +
                                                                                                                                                                                        14. You use the language parameter to construct a filename, then open the file and read the contents into a list. If language is en, then you'll open the rules.en file, read the entire thing, break it up by carriage returns, and return a list. Each line of the file will be one element in the list. -
                                                                                                                                                                                        15. 4 -As you saw, each line in the file really has three values, but they're separated by whitespace (tabs or spaces, it makes no +
                                                                                                                                                                                        16. As you saw, each line in the file really has three values, but they're separated by whitespace (tabs or spaces, it makes no difference). Mapping the string.split function onto this list will create a new list where each element is a tuple of three strings. So a line like [sxz]$ $ es will be broken up into the tuple ('[sxz]$', '$', 'es'). This means that patterns will end up as a list of tuples, just like you hard-coded it in stage 4. -
                                                                                                                                                                                        17. 5 -If patterns is a list of tuples, then rules will be a list of the functions created dynamically by each call to buildRule. Calling buildRule(('[sxz]$', '$', 'es')) returns a function that takes a single parameter, word. When this returned function is called, it will execute re.search('[sxz]$', word) and re.sub('$', 'es', word). -
                                                                                                                                                                                          6 -Because you're now building a combined match-and-apply function, you need to call it differently. Just call the function, +
                                                                                                                                                                                        18. If patterns is a list of tuples, then rules will be a list of the functions created dynamically by each call to buildRule. Calling buildRule(('[sxz]$', '$', 'es')) returns a function that takes a single parameter, word. When this returned function is called, it will execute re.search('[sxz]$', word) and re.sub('$', 'es', word). +
                                                                                                                                                                                        19. Because you're now building a combined match-and-apply function, you need to call it differently. Just call the function, and if it returns something, then that's the plural; if it returns nothing (None), then the rule didn't match and you need to try another rule. -
                                                                                                                                                                                        20. So the improvement here is that you've completely separated the pluralization rules into an external file. Not only can the file be maintained separately from the code, but you've set up a naming scheme where the same plural function can use different rule files, based on the language parameter.

                                                                                                                                                                                          The downside here is that you're reading that file every time you call the plural function. I thought I could get through this entire book without using the phrase “left as an exercise for the reader”, but here you go: building a caching mechanism for the language-specific rule files that auto-refreshes itself if the rule files change between calls is left as an exercise for the reader. Have fun.

                                                                                                                                                                                          17.7. plural.py, stage 6

                                                                                                                                                                                          Now you're ready to talk about generators. -

                                                                                                                                                                                          Example 17.17. plural6.py

                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.17. plural6.py

                                                                                                                                                                                          
                                                                                                                                                                                           import re
                                                                                                                                                                                           
                                                                                                                                                                                           def rules(language):           
                                                                                                                                                                                          @@ -14122,161 +9148,82 @@ def plural(noun, language='en'):
                                                                                                                                                                                                   result = applyRule(noun)      
                                                                                                                                                                                                   if result: return result      
                                                                                                                                                                                           

                                                                                                                                                                                          This uses a technique called generators, which I'm not even going to try to explain until you look at a simpler example first. -

                                                                                                                                                                                          Example 17.18. Introducing generators

                                                                                                                                                                                          ->>> def make_counter(x):
                                                                                                                                                                                          -...    print 'entering make_counter'
                                                                                                                                                                                          -...    while 1:
                                                                                                                                                                                          -...        yield x               1
                                                                                                                                                                                          -...        print 'incrementing x'
                                                                                                                                                                                          -...        x = x + 1
                                                                                                                                                                                          -...    
                                                                                                                                                                                          ->>> counter = make_counter(2) 2
                                                                                                                                                                                          ->>> counter 3
                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.18. Introducing generators

                                                                                                                                                                                          +>>> def make_counter(x):
                                                                                                                                                                                          +...    print 'entering make_counter'
                                                                                                                                                                                          +...    while 1:
                                                                                                                                                                                          +...        yield x               
                                                                                                                                                                                          +...        print 'incrementing x'
                                                                                                                                                                                          +...        x = x + 1
                                                                                                                                                                                          +...    
                                                                                                                                                                                          +>>> counter = make_counter(2) 
                                                                                                                                                                                          +>>> counter 
                                                                                                                                                                                           <generator object at 0x001C9C10>
                                                                                                                                                                                          ->>> counter.next()            4
                                                                                                                                                                                          -entering make_counter
                                                                                                                                                                                          +>>> counter.next()            
                                                                                                                                                                                          +entering make_counter
                                                                                                                                                                                           2
                                                                                                                                                                                          ->>> counter.next()            5
                                                                                                                                                                                          -incrementing x
                                                                                                                                                                                          +>>> counter.next()            
                                                                                                                                                                                          +incrementing x
                                                                                                                                                                                           3
                                                                                                                                                                                          ->>> counter.next()            6
                                                                                                                                                                                          -incrementing x
                                                                                                                                                                                          +>>> counter.next()            
                                                                                                                                                                                          +incrementing x
                                                                                                                                                                                           4
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                                                                                                                          1 -The presence of the yield keyword in make_counter means that this is not a normal function. It is a special kind of function which generates values one at a time. You can +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. The presence of the yield keyword in make_counter means that this is not a normal function. It is a special kind of function which generates values one at a time. You can think of it as a resumable function. Calling it will return a generator that can be used to generate successive values of x. -
                                                                                                                                                                                          2 -To create an instance of the make_counter generator, just call it like any other function. Note that this does not actually execute the function code. You can tell +
                                                                                                                                                                                        21. To create an instance of the make_counter generator, just call it like any other function. Note that this does not actually execute the function code. You can tell this because the first line of make_counter is a print statement, but nothing has been printed yet. -
                                                                                                                                                                                        22. 3 -The make_counter function returns a generator object. -
                                                                                                                                                                                          4 -The first time you call the next() method on the generator object, it executes the code in make_counter up to the first yield statement, and then returns the value that was yielded. In this case, that will be 2, because you originally created the generator by calling make_counter(2). -
                                                                                                                                                                                          5 -Repeatedly calling next() on the generator object resumes where you left off and continues until you hit the next yield statement. The next line of code waiting to be executed is the print statement that prints incrementing x, and then after that the x = x + 1 statement that actually increments it. Then you loop through the while loop again, and the first thing you do is yield x, which returns the current value of x (now 3). -
                                                                                                                                                                                          6 -The second time you call counter.next(), you do all the same things again, but this time x is now 4. And so forth. Since make_counter sets up an infinite loop, you could theoretically do this forever, and it would just keep incrementing x and spitting out values. But let's look at more productive uses of generators instead. -
                                                                                                                                                                                          -

                                                                                                                                                                                          Example 17.19. Using generators instead of recursion

                                                                                                                                                                                          +
                                                                                                                                                                                        23. The make_counter function returns a generator object. +
                                                                                                                                                                                        24. The first time you call the next() method on the generator object, it executes the code in make_counter up to the first yield statement, and then returns the value that was yielded. In this case, that will be 2, because you originally created the generator by calling make_counter(2). +
                                                                                                                                                                                        25. Repeatedly calling next() on the generator object resumes where you left off and continues until you hit the next yield statement. The next line of code waiting to be executed is the print statement that prints incrementing x, and then after that the x = x + 1 statement that actually increments it. Then you loop through the while loop again, and the first thing you do is yield x, which returns the current value of x (now 3). +
                                                                                                                                                                                        26. The second time you call counter.next(), you do all the same things again, but this time x is now 4. And so forth. Since make_counter sets up an infinite loop, you could theoretically do this forever, and it would just keep incrementing x and spitting out values. But let's look at more productive uses of generators instead. +

                                                                                                                                                                                          Example 17.19. Using generators instead of recursion

                                                                                                                                                                                          
                                                                                                                                                                                           def fibonacci(max):
                                                                                                                                                                                          -    a, b = 0, 1       1
                                                                                                                                                                                          +    a, b = 0, 1       
                                                                                                                                                                                               while a < max:
                                                                                                                                                                                          -        yield a       2
                                                                                                                                                                                          -        a, b = b, a+b 3
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - - - - - -
                                                                                                                                                                                          1 -The Fibonacci sequence is a sequence of numbers where each number is the sum of the two numbers before it. It starts with + yield a + a, b = b, a+b +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. The Fibonacci sequence is a sequence of numbers where each number is the sum of the two numbers before it. It starts with 0 and 1, goes up slowly at first, then more and more rapidly. To start the sequence, you need two variables: a starts at 0, and b starts at 1. -
                                                                                                                                                                                          2 -a is the current number in the sequence, so yield it. -
                                                                                                                                                                                          3 -b is the next number in the sequence, so assign that to a, but also calculate the next value (a+b) and assign that to b for later use. Note that this happens in parallel; if a is 3 and b is 5, then a, b = b, a+b will set a to 5 (the previous value of b) and b to 8 (the sum of the previous values of a and b). -
                                                                                                                                                                                          +
                                                                                                                                                                                        27. a is the current number in the sequence, so yield it. +
                                                                                                                                                                                        28. b is the next number in the sequence, so assign that to a, but also calculate the next value (a+b) and assign that to b for later use. Note that this happens in parallel; if a is 3 and b is 5, then a, b = b, a+b will set a to 5 (the previous value of b) and b to 8 (the sum of the previous values of a and b).

                                                                                                                                                                                          So you have a function that spits out successive Fibonacci numbers. Sure, you could do that with recursion, but this way is easier to read. Also, it works well with for loops. -

                                                                                                                                                                                          Example 17.20. Generators in for loops

                                                                                                                                                                                          ->>> for n in fibonacci(1000): 1
                                                                                                                                                                                          -...    print n,              2
                                                                                                                                                                                          +

                                                                                                                                                                                          Example 17.20. Generators in for loops

                                                                                                                                                                                          +>>> for n in fibonacci(1000): 
                                                                                                                                                                                          +...    print n,              
                                                                                                                                                                                           0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
                                                                                                                                                                                          -
                                                                                                                                                                                          - - - - - - - - - -
                                                                                                                                                                                          1 -You can use a generator like fibonacci in a for loop directly. The for loop will create the generator object and successively call the next() method to get values to assign to the for loop index variable (n). -
                                                                                                                                                                                          2 -Each time through the for loop, n gets a new value from the yield statement in fibonacci, and all you do is print it out. Once fibonacci runs out of numbers (a gets bigger than max, which in this case is 1000), then the for loop exits gracefully. -
                                                                                                                                                                                          +
                                                                                                                                                                                          +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. You can use a generator like fibonacci in a for loop directly. The for loop will create the generator object and successively call the next() method to get values to assign to the for loop index variable (n). +
                                                                                                                                                                                          2. Each time through the for loop, n gets a new value from the yield statement in fibonacci, and all you do is print it out. Once fibonacci runs out of numbers (a gets bigger than max, which in this case is 1000), then the for loop exits gracefully.

                                                                                                                                                                                            OK, let's go back to the plural function and see how you're using this. -

                                                                                                                                                                                            Example 17.21. Generators that generate dynamic functions

                                                                                                                                                                                            +

                                                                                                                                                                                            Example 17.21. Generators that generate dynamic functions

                                                                                                                                                                                            
                                                                                                                                                                                             def rules(language):           
                                                                                                                                                                                            -    for line in file('rules.%s' % language):      1
                                                                                                                                                                                            -        pattern, search, replace = line.split()   2
                                                                                                                                                                                            -        yield lambda word: re.search(pattern, word) and re.sub(search, replace, word) 3
                                                                                                                                                                                            +    for line in file('rules.%s' % language):      
                                                                                                                                                                                            +        pattern, search, replace = line.split()   
                                                                                                                                                                                            +        yield lambda word: re.search(pattern, word) and re.sub(search, replace, word) 
                                                                                                                                                                                             
                                                                                                                                                                                             def plural(noun, language='en'):      
                                                                                                                                                                                            -    for applyRule in rules(language):  4
                                                                                                                                                                                            +    for applyRule in rules(language):  
                                                                                                                                                                                                     result = applyRule(noun)      
                                                                                                                                                                                                     if result: return result      
                                                                                                                                                                                            -
                                                                                                                                                                                            - - - - - - - - - - - - - - - - - -
                                                                                                                                                                                            1 -for line in file(...) is a common idiom for reading lines from a file, one line at a time. It works because file actually returns a generator whose next() method returns the next line of the file. That is so insanely cool, I wet myself just thinking about it. -
                                                                                                                                                                                            2 -No magic here. Remember that the lines of the rules file have three values separated by whitespace, so line.split() returns a tuple of 3 values, and you assign those values to 3 local variables. -
                                                                                                                                                                                            3 -And then you yield. What do you yield? A function, built dynamically with lambda, that is actually a closure (it uses the local variables pattern, search, and replace as constants). In other words, rules is a generator that spits out rule functions. -
                                                                                                                                                                                            4 -Since rules is a generator, you can use it directly in a for loop. The first time through the for loop, you will call the rules function, which will open the rules file, read the first line out of it, dynamically build a function that matches and applies +
                                                                                                                                                                                            +
                                                                                                                                                                                              +
                                                                                                                                                                                            1. for line in file(...) is a common idiom for reading lines from a file, one line at a time. It works because file actually returns a generator whose next() method returns the next line of the file. That is so insanely cool, I wet myself just thinking about it. +
                                                                                                                                                                                            2. No magic here. Remember that the lines of the rules file have three values separated by whitespace, so line.split() returns a tuple of 3 values, and you assign those values to 3 local variables. +
                                                                                                                                                                                            3. And then you yield. What do you yield? A function, built dynamically with lambda, that is actually a closure (it uses the local variables pattern, search, and replace as constants). In other words, rules is a generator that spits out rule functions. +
                                                                                                                                                                                            4. Since rules is a generator, you can use it directly in a for loop. The first time through the for loop, you will call the rules function, which will open the rules file, read the first line out of it, dynamically build a function that matches and applies the first rule defined in the rules file, and yields the dynamically built function. The second time through the for loop, you will pick up where you left off in rules (which was in the middle of the for line in file(...) loop), read the second line of the rules file, dynamically build another function that matches and applies the second rule defined in the rules file, and yields it. And so forth. -

                                                                                                                                                                                            What have you gained over stage 5? In stage 5, you read the entire rules file and built a list of all the possible rules before you even tried the first one. Now with generators, you can do everything lazily: you open the first and read the first rule and create a function to try it, but if that works you don't ever read the rest of the file or create any other functions. -

                                                                                                                                                                                            +

                                                                                                                                                                                            Further reading

                                                                                                                                                                                            • PEP 255 defines generators. @@ -14287,7 +9234,7 @@ it, but if that works you don't ever read the rest of the file or create any oth

                                                                                                                                                                                              17.8. Summary

                                                                                                                                                                                              You talked about several different advanced techniques in this chapter. Not all of them are appropriate for every situation.

                                                                                                                                                                                              You should now be comfortable with all of these techniques: -

                                                                                                                                                                                              +
                                                                                                                                                                                              • Performing string substitution with regular expressions. @@ -14303,7 +9250,7 @@ it, but if that works you don't ever read the rest of the file or create any oth

                                                                                                                                                                                                Adding abstractions, building functions dynamically, building closures, and using generators can all make your code simpler, more readable, and more flexible. But they can also end up making it more difficult to debug later. It's up to you to find the right balance between simplicity and power. -

                                                                                                                                                                                                +

                                                                                                                                                                                                Chapter 18. Performance Tuning

                                                                                                                                                                                                Performance tuning is a many-splendored thing. Just because Python is an interpreted language doesn't mean you shouldn't worry about code optimization. But don't worry about it too much.

                                                                                                                                                                                                18.1. Diving in

                                                                                                                                                                                                @@ -14322,11 +9269,11 @@ for categorizing surnames in the United States census. It grouped similar-soundi misspelled, researchers had a chance of finding it. Soundex is still used today for much the same reason, although of course we use computerized database servers now. Most database servers include a Soundex function.

                                                                                                                                                                                                There are several subtle variations of the Soundex algorithm. This is the one used in this chapter: -

                                                                                                                                                                                                +
                                                                                                                                                                                                1. Keep the first letter of the name as-is.
                                                                                                                                                                                                2. Convert the remaining letters to digits, according to a specific table: -
                                                                                                                                                                                                  +
                                                                                                                                                                                                  • B, F, P, and V become 1.
                                                                                                                                                                                                  • C, G, J, K, Q, S, X, and Z become 2. @@ -14346,8 +9293,9 @@ we use computerized database servers now. Most database servers include a Sounde too long, so you discard the excess character, leaving P426.

                                                                                                                                                                                                    Another example: Woo becomes W99, which becomes W9, which becomes W, which gets padded with zeros to become W000.

                                                                                                                                                                                                    Here's a first attempt at a Soundex function: -

                                                                                                                                                                                                    Example 18.1. soundex/stage1/soundex1a.py

                                                                                                                                                                                                    -

                                                                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                                                                    +

                                                                                                                                                                                                    Example 18.1. soundex/stage1/soundex1a.py

                                                                                                                                                                                                    +

                                                                                                                                                                                                    If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                                                                    
                                                                                                                                                                                                     import string, re
                                                                                                                                                                                                     
                                                                                                                                                                                                     charToSoundex = {"A": "9",
                                                                                                                                                                                                    @@ -14420,7 +9368,7 @@ if __name__ == '__main__':
                                                                                                                                                                                                             statement = "soundex('%s')" % name
                                                                                                                                                                                                             t = Timer(statement, "from __main__ import soundex")
                                                                                                                                                                                                             print name.ljust(15), soundex(name), min(t.repeat())
                                                                                                                                                                                                    -
                                                                                                                                                                                                    +

                                                                                                                                                                                                    Further Reading on Soundex

                                                                                                                                                                                                    • Soundexing and Genealogy gives a chronology of the evolution of the Soundex and its regional variations. @@ -14438,48 +9386,26 @@ the first time, then turn off the service that's incessantly checking whether th side effects if run more than once? Don't forget that you're dealing with small fractions of a second, so small mistakes in your timing framework will irreparably skew your results.

                                                                                                                                                                                                      The Python community has a saying: “Python comes with batteries included.” Don't write your own timing framework. Python 2.3 comes with a perfectly good one called timeit. -

                                                                                                                                                                                                      Example 18.2. Introducing timeit

                                                                                                                                                                                                      -

                                                                                                                                                                                                      If you have not already done so, you can download this and other examples used in this book.

                                                                                                                                                                                                      ->>> import timeit
                                                                                                                                                                                                      ->>> t = timeit.Timer("soundex.soundex('Pilgrim')",
                                                                                                                                                                                                      -...    "import soundex")   1
                                                                                                                                                                                                      ->>> t.timeit()              2
                                                                                                                                                                                                      +

                                                                                                                                                                                                      Example 18.2. Introducing timeit

                                                                                                                                                                                                      +

                                                                                                                                                                                                      If you have not already done so, you can download this and other examples used in this book. +

                                                                                                                                                                                                      +>>> import timeit
                                                                                                                                                                                                      +>>> t = timeit.Timer("soundex.soundex('Pilgrim')",
                                                                                                                                                                                                      +...    "import soundex")   
                                                                                                                                                                                                      +>>> t.timeit()              
                                                                                                                                                                                                       8.21683733547
                                                                                                                                                                                                      ->>> t.repeat(3, 2000000)    3
                                                                                                                                                                                                      +>>> t.repeat(3, 2000000)    
                                                                                                                                                                                                       [16.48319309109, 16.46128984923, 16.44203948912]
                                                                                                                                                                                                      -
                                                                                                                                                                                                      - - - - - - - - - - - - - -
                                                                                                                                                                                                      1 -The timeit module defines one class, Timer, which takes two arguments. Both arguments are strings. The first argument is the statement you wish to time; in this case, +
                                                                                                                                                                                                      +
                                                                                                                                                                                                        +
                                                                                                                                                                                                      1. The timeit module defines one class, Timer, which takes two arguments. Both arguments are strings. The first argument is the statement you wish to time; in this case, you are timing a call to the Soundex function within the soundex with an argument of 'Pilgrim'. The second argument to the Timer class is the import statement that sets up the environment for the statement. Internally, timeit sets up an isolated virtual environment, manually executes the setup statement (importing the soundex module), then manually compiles and executes the timed statement (calling the Soundex function). -
                                                                                                                                                                                                      2 -Once you have the Timer object, the easiest thing to do is call timeit(), which calls your function 1 million times and returns the number of seconds it took to do it. -
                                                                                                                                                                                                      3 -The other major method of the Timer object is repeat(), which takes two optional arguments. The first argument is the number of times to repeat the entire test, and the second +
                                                                                                                                                                                                    • Once you have the Timer object, the easiest thing to do is call timeit(), which calls your function 1 million times and returns the number of seconds it took to do it. +
                                                                                                                                                                                                    • The other major method of the Timer object is repeat(), which takes two optional arguments. The first argument is the number of times to repeat the entire test, and the second argument is the number of times to call the timed statement within each test. Both arguments are optional, and they default to 3 and 1000000 respectively. The repeat() method returns a list of the times each test cycle took, in seconds. -
                                                                                                                                                                                                    • -
                                                                                                                                                                                                      - - - - - - -
                                                                                                                                                                                                      Tip
                                                                                                                                                                                                      You can use the timeit module on the command line to test an existing Python program, without modifying the code. See http://docs.python.org/lib/node396.html for documentation on the command-line flags. -
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +

                                                                                                                                                                                                      You can use the timeit module on the command line to test an existing Python program, without modifying the code. See http://docs.python.org/lib/node396.html for documentation on the command-line flags.

                                                                                                                                                                                                      Note that repeat() returns a list of times. The times will almost never be identical, due to slight variations in how much processor time the Python interpreter is getting (and those pesky background processes that you can't get rid of). Your first thought might be to say “Let's take the average and call that The True Number.” @@ -14487,31 +9413,24 @@ say “Let's take the average and call that The True Number.” or in the Python interpreter; they took longer because of those pesky background processes, or other factors outside of the Python interpreter that you can't fully eliminate. If the different timing results differ by more than a few percent, you still have too much variability to trust the results. Otherwise, take the minimum time and discard the rest.

                                                                                                                                                                                                      Python has a handy min function that takes a list and returns the smallest value: -

                                                                                                                                                                                                      ->>> min(t.repeat(3, 1000000))
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +>>> min(t.repeat(3, 1000000))
                                                                                                                                                                                                       8.22203948912
                                                                                                                                                                                                      -
                                                                                                                                                                                                      - - - - - - -
                                                                                                                                                                                                      Tip
                                                                                                                                                                                                      The timeit module only works if you already know what piece of code you need to optimize. If you have a larger Python program and don't know where your performance problems are, check out the hotshot module.
                                                                                                                                                                                                      -

                                                                                                                                                                                                      18.3. Optimizing Regular Expressions

                                                                                                                                                                                                      +
                                                                                                                                                                                                      +

                                                                                                                                                                                                      The timeit module only works if you already know what piece of code you need to optimize. If you have a larger Python program and don't know where your performance problems are, check out the hotshot module.

                                                                                                                                                                                                      18.3. Optimizing Regular Expressions

                                                                                                                                                                                                      The first thing the Soundex function checks is whether the input is a non-empty string of letters. What's the best way to do this?

                                                                                                                                                                                                      If you answered “regular expressions”, go sit in the corner and contemplate your bad instincts. Regular expressions are almost never the right answer; they should be avoided whenever possible. Not only for performance reasons, but simply because they're difficult to debug and maintain. Also for performance reasons.

                                                                                                                                                                                                      This code fragment from soundex/stage1/soundex1a.py checks whether the function argument source is a word made entirely of letters, with at least one letter (not the empty string): -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           allChars = string.uppercase + string.lowercase
                                                                                                                                                                                                           if not re.search('^[%s]+$' % allChars, source):
                                                                                                                                                                                                               return "0000"
                                                                                                                                                                                                       

                                                                                                                                                                                                      How does soundex1a.py perform? For convenience, the __main__ section of the script contains this code that calls the timeit module, sets up a timing test with three different names, tests each name three times, and displays the minimum time for each: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                       if __name__ == '__main__':
                                                                                                                                                                                                           from timeit import Timer
                                                                                                                                                                                                           names = ('Woo', 'Pilgrim', 'Flingjingwaller')
                                                                                                                                                                                                      @@ -14520,9 +9439,9 @@ if __name__ == '__main__':
                                                                                                                                                                                                               t = Timer(statement, "from __main__ import soundex")
                                                                                                                                                                                                               print name.ljust(15), soundex(name), min(t.repeat())
                                                                                                                                                                                                       

                                                                                                                                                                                                      So how does soundex1a.py perform with this regular expression? -

                                                                                                                                                                                                      -C:\samples\soundex\stage1>python soundex1a.py
                                                                                                                                                                                                      -Woo             W000 19.3356647283
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage1>python soundex1a.py
                                                                                                                                                                                                      +Woo             W000 19.3356647283
                                                                                                                                                                                                       Pilgrim         P426 24.0772053431
                                                                                                                                                                                                       Flingjingwaller F452 35.0463220884
                                                                                                                                                                                                       

                                                                                                                                                                                                      As you might expect, the algorithm takes significantly longer when called with longer names. There will be a few things we @@ -14532,41 +9451,41 @@ that it will never run in constant time. of different cases.

                                                                                                                                                                                                      So what about that regular expression? Well, it's inefficient. Since the expression is testing for ranges of characters (A-Z in uppercase, and a-z in lowercase), we can use a shorthand regular expression syntax. Here is soundex/stage1/soundex1b.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           if not re.search('^[A-Za-z]+$', source):
                                                                                                                                                                                                               return "0000"
                                                                                                                                                                                                       

                                                                                                                                                                                                      timeit says soundex1b.py is slightly faster than soundex1a.py, but nothing to get terribly excited about: -

                                                                                                                                                                                                      -C:\samples\soundex\stage1>python soundex1b.py
                                                                                                                                                                                                      -Woo             W000 17.1361133887
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage1>python soundex1b.py
                                                                                                                                                                                                      +Woo             W000 17.1361133887
                                                                                                                                                                                                       Pilgrim         P426 21.8201693232
                                                                                                                                                                                                       Flingjingwaller F452 32.7262294509
                                                                                                                                                                                                       

                                                                                                                                                                                                      We saw in Section 15.3, “Refactoring” that regular expressions can be compiled and reused for faster results. Since this regular expression never changes across function calls, we can compile it once and use the compiled version. Here is soundex/stage1/soundex1c.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                       isOnlyChars = re.compile('^[A-Za-z]+$').search
                                                                                                                                                                                                       def soundex(source):
                                                                                                                                                                                                           if not isOnlyChars(source):
                                                                                                                                                                                                               return "0000"
                                                                                                                                                                                                       

                                                                                                                                                                                                      Using a compiled regular expression in soundex1c.py is significantly faster: -

                                                                                                                                                                                                      -C:\samples\soundex\stage1>python soundex1c.py
                                                                                                                                                                                                      -Woo             W000 14.5348347346
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage1>python soundex1c.py
                                                                                                                                                                                                      +Woo             W000 14.5348347346
                                                                                                                                                                                                       Pilgrim         P426 19.2784703084
                                                                                                                                                                                                       Flingjingwaller F452 30.0893873383
                                                                                                                                                                                                       

                                                                                                                                                                                                      But is this the wrong path? The logic here is simple: the input source needs to be non-empty, and it needs to be composed entirely of letters. Wouldn't it be faster to write a loop checking each character, and do away with regular expressions altogether?

                                                                                                                                                                                                      Here is soundex/stage1/soundex1d.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           if not source:
                                                                                                                                                                                                               return "0000"
                                                                                                                                                                                                           for c in source:
                                                                                                                                                                                                               if not ('A' <= c <= 'Z') and not ('a' <= c <= 'z'):
                                                                                                                                                                                                                   return "0000"
                                                                                                                                                                                                       

                                                                                                                                                                                                      It turns out that this technique in soundex1d.py is not faster than using a compiled regular expression (although it is faster than using a non-compiled regular expression): -

                                                                                                                                                                                                      -C:\samples\soundex\stage1>python soundex1d.py
                                                                                                                                                                                                      -Woo             W000 15.4065058548
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage1>python soundex1d.py
                                                                                                                                                                                                      +Woo             W000 15.4065058548
                                                                                                                                                                                                       Pilgrim         P426 22.2753567842
                                                                                                                                                                                                       Flingjingwaller F452 37.5845122774
                                                                                                                                                                                                       

                                                                                                                                                                                                      Why isn't soundex1d.py faster? The answer lies in the interpreted nature of Python. The regular expression engine is written in C, and compiled to run natively on your computer. On the other hand, this @@ -14575,16 +9494,16 @@ Regular expressions are never the right answer... except when they are.

                                                                                                                                                                                                      It turns out that Python offers an obscure string method. You can be excused for not knowing about it, since it's never been mentioned in this book. The method is called isalpha(), and it checks whether a string contains only letters.

                                                                                                                                                                                                      This is soundex/stage1/soundex1e.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           if (not source) and (not source.isalpha()):
                                                                                                                                                                                                               return "0000"
                                                                                                                                                                                                       

                                                                                                                                                                                                      How much did we gain by using this specific method in soundex1e.py? Quite a bit. -

                                                                                                                                                                                                      -C:\samples\soundex\stage1>python soundex1e.py
                                                                                                                                                                                                      -Woo             W000 13.5069504644
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage1>python soundex1e.py
                                                                                                                                                                                                      +Woo             W000 13.5069504644
                                                                                                                                                                                                       Pilgrim         P426 18.2199394057
                                                                                                                                                                                                       Flingjingwaller F452 28.9975225902
                                                                                                                                                                                                      -

                                                                                                                                                                                                      Example 18.3. Best Result So Far: soundex/stage1/soundex1e.py

                                                                                                                                                                                                      +

                                                                                                                                                                                                      Example 18.3. Best Result So Far: soundex/stage1/soundex1e.py

                                                                                                                                                                                                      
                                                                                                                                                                                                       import string, re
                                                                                                                                                                                                       
                                                                                                                                                                                                       charToSoundex = {"A": "9",
                                                                                                                                                                                                      @@ -14643,7 +9562,7 @@ if __name__ == '__main__':
                                                                                                                                                                                                          do this?
                                                                                                                                                                                                       

                                                                                                                                                                                                      The most obvious solution is to define a dictionary with individual characters as keys and their corresponding digits as values, and do dictionary lookups on each character. This is what we have in soundex/stage1/soundex1c.py (the current best result so far): -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                       charToSoundex = {"A": "9",
                                                                                                                                                                                                                        "B": "1",
                                                                                                                                                                                                                        "C": "2",
                                                                                                                                                                                                      @@ -14679,9 +9598,9 @@ def soundex(source):
                                                                                                                                                                                                               s = s.upper()
                                                                                                                                                                                                               digits += charToSoundex[s]
                                                                                                                                                                                                       

                                                                                                                                                                                                      You timed soundex1c.py already; this is how it performs: -

                                                                                                                                                                                                      -C:\samples\soundex\stage1>python soundex1c.py
                                                                                                                                                                                                      -Woo             W000 14.5341678901
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage1>python soundex1c.py
                                                                                                                                                                                                      +Woo             W000 14.5341678901
                                                                                                                                                                                                       Pilgrim         P426 19.2650071448
                                                                                                                                                                                                       Flingjingwaller F452 30.1003563302
                                                                                                                                                                                                       

                                                                                                                                                                                                      This code is straightforward, but is it the best solution? Calling upper() on each individual character seems inefficient; it would probably be better to call upper() once on the entire string. @@ -14689,32 +9608,32 @@ Flingjingwaller F452 30.1003563302

                                                                                                                                                                                                      Python is good at lists, though. It can treat a string as a list of characters automatically. And lists are easy to combine into strings again, using the string method join().

                                                                                                                                                                                                      Here is soundex/stage2/soundex2a.py, which converts letters to digits by using ↦ and lambda: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                       def soundex(source):
                                                                                                                                                                                                           # ...
                                                                                                                                                                                                           source = source.upper()
                                                                                                                                                                                                           digits = source[0] + "".join(map(lambda c: charToSoundex[c], source[1:]))
                                                                                                                                                                                                       

                                                                                                                                                                                                      Surprisingly, soundex2a.py is not faster: -

                                                                                                                                                                                                      -C:\samples\soundex\stage2>python soundex2a.py
                                                                                                                                                                                                      -Woo             W000 15.0097526362
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage2>python soundex2a.py
                                                                                                                                                                                                      +Woo             W000 15.0097526362
                                                                                                                                                                                                       Pilgrim         P426 19.254806407
                                                                                                                                                                                                       Flingjingwaller F452 29.3790847719
                                                                                                                                                                                                       

                                                                                                                                                                                                      The overhead of the anonymous lambda function kills any performance you gain by dealing with the string as a list of characters.

                                                                                                                                                                                                      soundex/stage2/soundex2b.py uses a list comprehension instead of ↦ and lambda: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           source = source.upper()
                                                                                                                                                                                                           digits = source[0] + "".join([charToSoundex[c] for c in source[1:]])
                                                                                                                                                                                                       

                                                                                                                                                                                                      Using a list comprehension in soundex2b.py is faster than using ↦ and lambda in soundex2a.py, but still not faster than the original code (incrementally building a string in soundex1c.py): -

                                                                                                                                                                                                      -C:\samples\soundex\stage2>python soundex2b.py
                                                                                                                                                                                                      -Woo             W000 13.4221324219
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage2>python soundex2b.py
                                                                                                                                                                                                      +Woo             W000 13.4221324219
                                                                                                                                                                                                       Pilgrim         P426 16.4901234654
                                                                                                                                                                                                       Flingjingwaller F452 25.8186157738
                                                                                                                                                                                                       

                                                                                                                                                                                                      It's time for a radically different approach. Dictionary lookups are a general purpose tool. Dictionary keys can be any length string (or many other data types), but in this case we are only dealing with single-character keys and single-character values. It turns out that Python has a specialized function for handling exactly this situation: the string.maketrans function.

                                                                                                                                                                                                      This is soundex/stage2/soundex2c.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                       allChar = string.uppercase + string.lowercase
                                                                                                                                                                                                       charToSoundex = string.maketrans(allChar, "91239129922455912623919292" * 2)
                                                                                                                                                                                                       def soundex(source):
                                                                                                                                                                                                      @@ -14725,13 +9644,13 @@ is the string ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,
                                                                                                                                                                                                       to 1, C maps to 2, and so forth. But it's not a dictionary; it's a specialized data structure that you can access using the
                                                                                                                                                                                                       string method translate, which translates each character into the corresponding digit, according to the matrix defined by string.maketrans.
                                                                                                                                                                                                       

                                                                                                                                                                                                      timeit shows that soundex2c.py is significantly faster than defining a dictionary and looping through the input and building the output incrementally: -

                                                                                                                                                                                                      -C:\samples\soundex\stage2>python soundex2c.py
                                                                                                                                                                                                      -Woo             W000 11.437645008
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage2>python soundex2c.py
                                                                                                                                                                                                      +Woo             W000 11.437645008
                                                                                                                                                                                                       Pilgrim         P426 13.2825062962
                                                                                                                                                                                                       Flingjingwaller F452 18.5570110168
                                                                                                                                                                                                       

                                                                                                                                                                                                      You're not going to get much better than that. Python has a specialized function that does exactly what you want to do; use it and move on. -

                                                                                                                                                                                                      Example 18.4. Best Result So Far: soundex/stage2/soundex2c.py

                                                                                                                                                                                                      +

                                                                                                                                                                                                      Example 18.4. Best Result So Far: soundex/stage2/soundex2c.py

                                                                                                                                                                                                      
                                                                                                                                                                                                       import string, re
                                                                                                                                                                                                       
                                                                                                                                                                                                       allChar = string.uppercase + string.lowercase
                                                                                                                                                                                                      @@ -14761,21 +9680,21 @@ if __name__ == '__main__':
                                                                                                                                                                                                       

                                                                                                                                                                                                      18.5. Optimizing List Operations

                                                                                                                                                                                                      The third step in the Soundex algorithm is eliminating consecutive duplicate digits. What's the best way to do this?

                                                                                                                                                                                                      Here's the code we have so far, in soundex/stage2/soundex2c.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           digits2 = digits[0]
                                                                                                                                                                                                           for d in digits[1:]:
                                                                                                                                                                                                               if digits2[-1] != d:
                                                                                                                                                                                                                   digits2 += d
                                                                                                                                                                                                       

                                                                                                                                                                                                      Here are the performance results for soundex2c.py: -

                                                                                                                                                                                                      -C:\samples\soundex\stage2>python soundex2c.py
                                                                                                                                                                                                      -Woo             W000 12.6070768771
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage2>python soundex2c.py
                                                                                                                                                                                                      +Woo             W000 12.6070768771
                                                                                                                                                                                                       Pilgrim         P426 14.4033353401
                                                                                                                                                                                                       Flingjingwaller F452 19.7774882003
                                                                                                                                                                                                       

                                                                                                                                                                                                      The first thing to consider is whether it's efficient to check digits[-1] each time through the loop. Are list indexes expensive? Would we be better off maintaining the last digit in a separate variable, and checking that instead?

                                                                                                                                                                                                      To answer this question, here is soundex/stage3/soundex3a.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           digits2 = ''
                                                                                                                                                                                                           last_digit = ''
                                                                                                                                                                                                           for d in digits:
                                                                                                                                                                                                      @@ -14783,9 +9702,9 @@ variable, and checking that instead?
                                                                                                                                                                                                                   digits2 += d
                                                                                                                                                                                                                   last_digit = d
                                                                                                                                                                                                       

                                                                                                                                                                                                      soundex3a.py does not run any faster than soundex2c.py, and may even be slightly slower (although it's not enough of a difference to say for sure): -

                                                                                                                                                                                                      -C:\samples\soundex\stage3>python soundex3a.py
                                                                                                                                                                                                      -Woo             W000 11.5346048171
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage3>python soundex3a.py
                                                                                                                                                                                                      +Woo             W000 11.5346048171
                                                                                                                                                                                                       Pilgrim         P426 13.3950636184
                                                                                                                                                                                                       Flingjingwaller F452 18.6108927252
                                                                                                                                                                                                       

                                                                                                                                                                                                      Why isn't soundex3a.py faster? It turns out that list indexes in Python are extremely efficient. Repeatedly accessing digits2[-1] is no problem at all. On the other hand, manually maintaining the last seen digit in a separate variable means we have two variable assignments for each digit we're storing, which wipes out any small gains we might have gotten from eliminating @@ -14796,19 +9715,19 @@ in the list, and that's not easy to do with a straightforward list comprehension

                                                                                                                                                                                                      However, it is possible to create a list of index numbers using the built-in range() function, and use those index numbers to progressively search through the list and pull out each character that is different from the previous character. That will give you a list of characters, and you can use the string method join() to reconstruct a string from that.

                                                                                                                                                                                                      Here is soundex/stage3/soundex3b.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           digits2 = "".join([digits[i] for i in range(len(digits))
                                                                                                                                                                                                            if i == 0 or digits[i-1] != digits[i]])
                                                                                                                                                                                                       

                                                                                                                                                                                                      Is this faster? In a word, no. -

                                                                                                                                                                                                      -C:\samples\soundex\stage3>python soundex3b.py
                                                                                                                                                                                                      -Woo             W000 14.2245271396
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage3>python soundex3b.py
                                                                                                                                                                                                      +Woo             W000 14.2245271396
                                                                                                                                                                                                       Pilgrim         P426 17.8337165757
                                                                                                                                                                                                       Flingjingwaller F452 25.9954005327
                                                                                                                                                                                                       

                                                                                                                                                                                                      It's possible that the techniques so far as have been “string-centric”. Python can convert a string into a list of characters with a single command: list('abc') returns ['a', 'b', 'c']. Furthermore, lists can be modified in place very quickly. Instead of incrementally building a new list (or string) out of the source string, why not move elements around within a single list?

                                                                                                                                                                                                      Here is soundex/stage3/soundex3c.py, which modifies a list in place to remove consecutive duplicate elements: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           digits = list(source[0].upper() + source[1:].translate(charToSoundex))
                                                                                                                                                                                                           i=0
                                                                                                                                                                                                           for item in digits:
                                                                                                                                                                                                      @@ -14818,13 +9737,13 @@ within a single list?
                                                                                                                                                                                                           del digits[i+1:]
                                                                                                                                                                                                           digits2 = "".join(digits)
                                                                                                                                                                                                       

                                                                                                                                                                                                      Is this faster than soundex3a.py or soundex3b.py? No, in fact it's the slowest method yet: -

                                                                                                                                                                                                      -C:\samples\soundex\stage3>python soundex3c.py
                                                                                                                                                                                                      -Woo             W000 14.1662554878
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage3>python soundex3c.py
                                                                                                                                                                                                      +Woo             W000 14.1662554878
                                                                                                                                                                                                       Pilgrim         P426 16.0397885765
                                                                                                                                                                                                       Flingjingwaller F452 22.1789341942
                                                                                                                                                                                                       

                                                                                                                                                                                                      We haven't made any progress here at all, except to try and rule out several “clever” techniques. The fastest code we've seen so far was the original, most straightforward method (soundex2c.py). Sometimes it doesn't pay to be clever. -

                                                                                                                                                                                                      Example 18.5. Best Result So Far: soundex/stage2/soundex2c.py

                                                                                                                                                                                                      +

                                                                                                                                                                                                      Example 18.5. Best Result So Far: soundex/stage2/soundex2c.py

                                                                                                                                                                                                      
                                                                                                                                                                                                       import string, re
                                                                                                                                                                                                       
                                                                                                                                                                                                       allChar = string.uppercase + string.lowercase
                                                                                                                                                                                                      @@ -14855,36 +9774,36 @@ if __name__ == '__main__':
                                                                                                                                                                                                       

                                                                                                                                                                                                      The final step of the Soundex algorithm is padding short results with zeros, and truncating long results. What is the best way to do this?

                                                                                                                                                                                                      This is what we have so far, taken from soundex/stage2/soundex2c.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           digits3 = re.sub('9', '', digits2)
                                                                                                                                                                                                           while len(digits3) < 4:
                                                                                                                                                                                                               digits3 += "0"
                                                                                                                                                                                                           return digits3[:4]
                                                                                                                                                                                                       

                                                                                                                                                                                                      These are the results for soundex2c.py: -

                                                                                                                                                                                                      -C:\samples\soundex\stage2>python soundex2c.py
                                                                                                                                                                                                      -Woo             W000 12.6070768771
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage2>python soundex2c.py
                                                                                                                                                                                                      +Woo             W000 12.6070768771
                                                                                                                                                                                                       Pilgrim         P426 14.4033353401
                                                                                                                                                                                                       Flingjingwaller F452 19.7774882003
                                                                                                                                                                                                       

                                                                                                                                                                                                      The first thing to consider is replacing that regular expression with a loop. This code is from soundex/stage4/soundex4a.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           digits3 = ''
                                                                                                                                                                                                           for d in digits2:
                                                                                                                                                                                                               if d != '9':
                                                                                                                                                                                                                   digits3 += d
                                                                                                                                                                                                       

                                                                                                                                                                                                      Is soundex4a.py faster? Yes it is: -

                                                                                                                                                                                                      -C:\samples\soundex\stage4>python soundex4a.py
                                                                                                                                                                                                      -Woo             W000 6.62865531792
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage4>python soundex4a.py
                                                                                                                                                                                                      +Woo             W000 6.62865531792
                                                                                                                                                                                                       Pilgrim         P426 9.02247576158
                                                                                                                                                                                                       Flingjingwaller F452 13.6328416042
                                                                                                                                                                                                       

                                                                                                                                                                                                      But wait a minute. A loop to remove characters from a string? We can use a simple string method for that. Here's soundex/stage4/soundex4b.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           digits3 = digits2.replace('9', '')
                                                                                                                                                                                                       

                                                                                                                                                                                                      Is soundex4b.py faster? That's an interesting question. It depends on the input: -

                                                                                                                                                                                                      -C:\samples\soundex\stage4>python soundex4b.py
                                                                                                                                                                                                      -Woo             W000 6.75477414029
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage4>python soundex4b.py
                                                                                                                                                                                                      +Woo             W000 6.75477414029
                                                                                                                                                                                                       Pilgrim         P426 7.56652144337
                                                                                                                                                                                                       Flingjingwaller F452 10.8727729362
                                                                                                                                                                                                       

                                                                                                                                                                                                      The string method in soundex4b.py is faster than the loop for most names, but it's actually slightly slower than soundex4a.py in the trivial case (of a very short name). Performance optimizations aren't always uniform; tuning that makes one case @@ -14892,26 +9811,26 @@ faster can sometimes make other cases slower. In this case, the majority of case leave it at that, but the principle is an important one to remember.

                                                                                                                                                                                                      Last but not least, let's examine the final two steps of the algorithm: padding short results with zeros, and truncating long results to four characters. The code you see in soundex4b.py does just that, but it's horribly inefficient. Take a look at soundex/stage4/soundex4c.py to see why: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           digits3 += '000'
                                                                                                                                                                                                           return digits3[:4]
                                                                                                                                                                                                       

                                                                                                                                                                                                      Why do we need a while loop to pad out the result? We know in advance that we're going to truncate the result to four characters, and we know that we already have at least one character (the initial letter, which is passed unchanged from the original source variable). That means we can simply add three zeros to the output, then truncate it. Don't get stuck in a rut over the exact wording of the problem; looking at the problem slightly differently can lead to a simpler solution.

                                                                                                                                                                                                      How much speed do we gain in soundex4c.py by dropping the while loop? It's significant: -

                                                                                                                                                                                                      -C:\samples\soundex\stage4>python soundex4c.py
                                                                                                                                                                                                      -Woo             W000 4.89129791636
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage4>python soundex4c.py
                                                                                                                                                                                                      +Woo             W000 4.89129791636
                                                                                                                                                                                                       Pilgrim         P426 7.30642134685
                                                                                                                                                                                                       Flingjingwaller F452 10.689832367
                                                                                                                                                                                                       

                                                                                                                                                                                                      Finally, there is still one more thing you can do to these three lines of code to make them faster: you can combine them into one line. Take a look at soundex/stage4/soundex4d.py: -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      
                                                                                                                                                                                                           return (digits2.replace('9', '') + '000')[:4]
                                                                                                                                                                                                       

                                                                                                                                                                                                      Putting all this code on one line in soundex4d.py is barely faster than soundex4c.py: -

                                                                                                                                                                                                      -C:\samples\soundex\stage4>python soundex4d.py
                                                                                                                                                                                                      -Woo             W000 4.93624105857
                                                                                                                                                                                                      +
                                                                                                                                                                                                      +C:\samples\soundex\stage4>python soundex4d.py
                                                                                                                                                                                                      +Woo             W000 4.93624105857
                                                                                                                                                                                                       Pilgrim         P426 7.19747593619
                                                                                                                                                                                                       Flingjingwaller F452 10.5490700634
                                                                                                                                                                                                       

                                                                                                                                                                                                      It is also significantly less readable, and for not much performance gain. Is that worth it? I hope you have good comments. @@ -14919,7 +9838,7 @@ Performance isn't everything. Your optimization efforts must always be balanced and maintainability.

                                                                                                                                                                                                      18.7. Summary

                                                                                                                                                                                                      This chapter has illustrated several important aspects of performance tuning in Python, and performance tuning in general. -

                                                                                                                                                                                                      +
                                                                                                                                                                                                      • If you need to choose between regular expressions and writing a loop, choose regular expressions. The regular expression engine is compiled in C and runs natively on your computer; your loop is written in Python and runs through the Python interpreter. diff --git a/index.html b/index.html index dd3152c..ad1aef4 100644 --- a/index.html +++ b/index.html @@ -20,7 +20,7 @@ li:last-child:before{content:"A. \00a0 \00a0"}
                                                                                                                                                                                                      • Your first Python program
                                                                                                                                                                                                      • Native datatypes
                                                                                                                                                                                                      • -
                                                                                                                                                                                                      • +
                                                                                                                                                                                                      • Regular expressions
                                                                                                                                                                                                      • diff --git a/regular-expressions.html b/regular-expressions.html new file mode 100644 index 0000000..b720678 --- /dev/null +++ b/regular-expressions.html @@ -0,0 +1,434 @@ + + + + +Regular expressions - Dive into Python 3 + + + + + +

                                                                                                                                                                                                        skip to main content +

                                                                                                                                                                                                        +

                                                                                                                                                                                                        Regular expressions

                                                                                                                                                                                                        +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.
                                                                                                                                                                                                        Jamie Zawinski +

                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. Diving in +
                                                                                                                                                                                                        2. Case study: street addresses +
                                                                                                                                                                                                        3. Case study: Roman numerals +
                                                                                                                                                                                                            +
                                                                                                                                                                                                          1. Checking for thousands +
                                                                                                                                                                                                          2. Checking for hundreds +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        4. Using the {n,m} Syntax +
                                                                                                                                                                                                            +
                                                                                                                                                                                                          1. Checking for tens and ones +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        5. Verbose regular expressions +
                                                                                                                                                                                                        6. Case study: parsing phone numbers +
                                                                                                                                                                                                        7. Summary +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Diving in

                                                                                                                                                                                                        +

                                                                                                                                                                                                        Regular expressions are a powerful and standardized way of searching, replacing, and parsing text with complex patterns of +characters. If you've used regular expressions in other languages (like Perl), the syntax will be very familiar, and you get by just reading the summary of the re module to get an overview of the available functions and their arguments. +

                                                                                                                                                                                                        Strings have methods for searching and replacing — index(), find(), split(), count(), replace(), &c. — but they are limited to the simplest of cases. For example, the index() method looks for a single, hard-coded substring, and the search is always case-sensitive. To do case-insensitive searches of a string s, you must call s.lower() or s.upper() and make sure your search strings are the appropriate case to match. The replace() and split() methods have the same limitations. +

                                                                                                                                                                                                        If your goal can be accomplished with string functions, you should use them. They're fast and simple and easy to read, and there's a lot to be said for fast, simple, readable code. But if you find yourself using a lot of different string functions with if statements to handle special cases, or if you're combining them with split() and join() and list comprehensions in weird unreadable ways, you may need to move up to regular expressions. +

                                                                                                                                                                                                        Although the regular expression syntax is tight and unlike normal code, the result can end up being more readable than a hand-rolled solution that uses a long chain of string functions. There are even ways of embedding comments within regular expressions, so you can include fine-grained documentation within them. +

                                                                                                                                                                                                        Case study: street addresses

                                                                                                                                                                                                        +

                                                                                                                                                                                                        This series of examples was inspired by a real-life problem I had in my day job several years ago, when I needed to scrub and standardize street addresses exported from a legacy system before importing them into a newer system. (See, I don't just make this stuff up; it's actually useful.) This example shows how I approached the problem. +

                                                                                                                                                                                                        +>>> s = '100 NORTH MAIN ROAD'
                                                                                                                                                                                                        +>>> s.replace('ROAD', 'RD.')                
                                                                                                                                                                                                        +'100 NORTH MAIN RD.'
                                                                                                                                                                                                        +>>> s = '100 NORTH BROAD ROAD'
                                                                                                                                                                                                        +>>> s.replace('ROAD', 'RD.')                
                                                                                                                                                                                                        +'100 NORTH BRD. RD.'
                                                                                                                                                                                                        +>>> s[:-4] + s[-4:].replace('ROAD', 'RD.')  
                                                                                                                                                                                                        +'100 NORTH BROAD RD.'
                                                                                                                                                                                                        +>>> import re                               
                                                                                                                                                                                                        +>>> re.sub('ROAD$', 'RD.', s)               
                                                                                                                                                                                                        +'100 NORTH BROAD RD.'
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. My goal is to standardize a street address so that 'ROAD' is always abbreviated as 'RD.'. At first glance, I thought this was simple enough that I could just use the string method replace(). After all, all the data was already uppercase, so case mismatches would not be a problem. And the search string, 'ROAD', was a constant. And in this deceptively simple example, s.replace() does indeed work. +
                                                                                                                                                                                                        2. Life, unfortunately, is full of counterexamples, and I quickly discovered this one. The problem here is that 'ROAD' appears twice in the address, once as part of the street name 'BROAD' and once as its own word. The replace() method sees these two occurrences and blindly replaces both of them; meanwhile, I see my addresses getting destroyed. +
                                                                                                                                                                                                        3. To solve the problem of addresses with more than one 'ROAD' substring, you could resort to something like this: only search and replace 'ROAD' in the last four characters of the address (s[-4:]), and leave the string alone (s[:-4]). But you can see that this is already getting unwieldy. For example, the pattern is dependent on the length of the string you're replacing. (If you were replacing 'STREET' with 'ST.', you would need to use s[:-6] and s[-6:].replace(...).) Would you like to come back in six months and debug this? I know I wouldn't. +
                                                                                                                                                                                                        4. It's time to move up to regular expressions. In Python, all functionality related to regular expressions is contained in the re module. +
                                                                                                                                                                                                        5. Take a look at the first parameter: 'ROAD$'. This is a simple regular expression that matches 'ROAD' only when it occurs at the end of a string. The $ means “end of the string.” (There is a corresponding character, the caret ^, which means “beginning of the string.”) Using the re.sub function, you search the string s for the regular expression 'ROAD$' and replace it with 'RD.'. This matches the ROAD at the end of the string s, but does not match the ROAD that's part of the word BROAD, because that's in the middle of s. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Continuing with my story of scrubbing addresses, I soon discovered that the previous example, matching 'ROAD' at the end of the address, was not good enough, because not all addresses included a street designation at all. Some addresses simply ended with the street name. I got away with it most of the time, but if the street name was 'BROAD', then the regular expression would match 'ROAD' at the end of the string as part of the word 'BROAD', which is not what I wanted. +

                                                                                                                                                                                                        +>>> s = '100 BROAD'
                                                                                                                                                                                                        +>>> re.sub('ROAD$', 'RD.', s)
                                                                                                                                                                                                        +'100 BRD.'
                                                                                                                                                                                                        +>>> re.sub('\\bROAD$', 'RD.', s)   
                                                                                                                                                                                                        +'100 BROAD'
                                                                                                                                                                                                        +>>> re.sub(r'\bROAD$', 'RD.', s)   
                                                                                                                                                                                                        +'100 BROAD'
                                                                                                                                                                                                        +>>> s = '100 BROAD ROAD APT. 3'
                                                                                                                                                                                                        +>>> re.sub(r'\bROAD$', 'RD.', s)   
                                                                                                                                                                                                        +'100 BROAD ROAD APT. 3'
                                                                                                                                                                                                        +>>> re.sub(r'\bROAD\b', 'RD.', s)  
                                                                                                                                                                                                        +'100 BROAD RD. APT 3'
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. What I really wanted was to match 'ROAD' when it was at the end of the string and it was its own word (and not a part of some larger word). To express this in a regular expression, you use \b, which means “a word boundary must occur right here.” In Python, this is complicated by the fact that the '\' character in a string must itself be escaped. This is sometimes referred to as the backslash plague, and it is one reason why regular expressions are easier in Perl than in Python. On the down side, Perl mixes regular expressions with other syntax, so if you have a bug, it may be hard to tell whether it's a bug in syntax or a bug in your regular expression. +
                                                                                                                                                                                                        2. To work around the backslash plague, you can use what is called a raw string [FIXME reference to strings chapter], by prefixing the string with the letter r. This tells Python that nothing in this string should be escaped; '\t' is a tab character, but r'\t' is really the backslash character \ followed by the letter t. I recommend always using raw strings when dealing with regular expressions; otherwise, things get too confusing too quickly (and regular expressions are confusing enough already). +
                                                                                                                                                                                                        3. *sigh* Unfortunately, I soon found more cases that contradicted my logic. In this case, the street address contained the word 'ROAD' as a whole word by itself, but it wasn't at the end, because the address had an apartment number after the street designation. Because 'ROAD' isn't at the very end of the string, it doesn't match, so the entire call to re.sub ends up replacing nothing at all, and you get the original string back, which is not what you want. +
                                                                                                                                                                                                        4. To solve this problem, I removed the $ character and added another \b. Now the regular expression reads “match 'ROAD' when it's a whole word by itself anywhere in the string,” whether at the end, the beginning, or somewhere in the middle. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Case study: Roman numerals

                                                                                                                                                                                                        +

                                                                                                                                                                                                        You've most likely seen Roman numerals, even if you didn't recognize them. You may have seen them in copyrights of old movies and television shows (“Copyright MCMXLVI” instead of “Copyright 1946”), or on the dedication walls of libraries or universities (“established MDCCCLXXXVIII” instead of “established 1888”). You may also have seen them in outlines and bibliographical references. It's a system of representing numbers that really does date back to the ancient Roman empire (hence the name). +

                                                                                                                                                                                                        In Roman numerals, there are seven characters that are repeated and combined in various ways to represent numbers. +

                                                                                                                                                                                                          +
                                                                                                                                                                                                        • I = 1 +
                                                                                                                                                                                                        • V = 5 +
                                                                                                                                                                                                        • X = 10 +
                                                                                                                                                                                                        • L = 50 +
                                                                                                                                                                                                        • C = 100 +
                                                                                                                                                                                                        • D = 500 +
                                                                                                                                                                                                        • M = 1000 +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        The following are some general rules for constructing Roman numerals: +

                                                                                                                                                                                                          +
                                                                                                                                                                                                        • Characters are additive. I is 1, II is 2, and III is 3. VI is 6 (literally, “5 and 1”), VII is 7, and VIII is 8. +
                                                                                                                                                                                                        • The tens characters (I, X, C, and M) can be repeated up to three times. At 4, you need to subtract from the next highest fives character. You can't represent 4 as IIII; instead, it is represented as IV (“1 less than 5”). The number 40 is written as XL (10 less than 50), 41 as XLI, 42 as XLII, 43 as XLIII, and then 44 as XLIV (10 less than 50, then 1 less than 5). +
                                                                                                                                                                                                        • Similarly, at 9, you need to subtract from the next highest tens character: 8 is VIII, but 9 is IX (1 less than 10), not VIIII (since the I character can not be repeated four times). The number 90 is XC, 900 is CM. +
                                                                                                                                                                                                        • The fives characters can not be repeated. The number 10 is always represented as X, never as VV. The number 100 is always C, never LL. +
                                                                                                                                                                                                        • Roman numerals are always written highest to lowest, and read left to right, so the order the of characters matters very much. DC is 600; CD is a completely different number (400, 100 less than 500). CI is 101; IC is not even a valid Roman numeral (because you can't subtract 1 directly from 100; you would need to write it as XCIX, for 10 less than 100, then 1 less than 10). +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Checking for thousands

                                                                                                                                                                                                        +

                                                                                                                                                                                                        What would it take to validate that an arbitrary string is a valid Roman numeral? Let's take it one digit at a time. Since Roman numerals are always written highest to lowest, let's start with the highest: the thousands place. For numbers 1000 and higher, the thousands are represented by a series of M characters. +

                                                                                                                                                                                                        +>>> import re
                                                                                                                                                                                                        +>>> pattern = '^M?M?M?$'        
                                                                                                                                                                                                        +>>> re.search(pattern, 'M')     
                                                                                                                                                                                                        +<SRE_Match object at 0106FB58>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MM')    
                                                                                                                                                                                                        +<SRE_Match object at 0106C290>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMM')   
                                                                                                                                                                                                        +<SRE_Match object at 0106AA38>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMMM')  
                                                                                                                                                                                                        +>>> re.search(pattern, '')      
                                                                                                                                                                                                        +<SRE_Match object at 0106F4A8>
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. This pattern has three parts. ^ matches what follows only at the beginning of the string. If this were not specified, the pattern would match no matter where the M characters were, which is not what you want. You want to make sure that the M characters, if they're there, are at the beginning of the string. M? optionally matches a single M character. Since this is repeated three times, you're matching anywhere from zero to three M characters in a row. And $ matches the end of the string. When combined with the ^ character at the beginning, this means that the pattern must match the entire string, with no other characters before or after the M characters. +
                                                                                                                                                                                                        2. The essence of the re module is the search() function, that takes a regular expression (pattern) and a string ('M') to try to match against the regular expression. If a match is found, search() returns an object which has various methods to describe the match; if no match is found, search() returns None, the Python null value. All you care about at the moment is whether the pattern matches, which you can tell by just looking at the return value of search(). 'M' matches this regular expression, because the first optional M matches and the second and third optional M characters are ignored. +
                                                                                                                                                                                                        3. 'MM' matches because the first and second optional M characters match and the third M is ignored. +
                                                                                                                                                                                                        4. 'MMM' matches because all three M characters match. +
                                                                                                                                                                                                        5. 'MMMM' does not match. All three M characters match, but then the regular expression insists on the string ending (because of the $ character), and the string doesn't end yet (because of the fourth M). So search() returns None. +
                                                                                                                                                                                                        6. Interestingly, an empty string also matches this regular expression, since all the M characters are optional. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Checking for hundreds

                                                                                                                                                                                                        +

                                                                                                                                                                                                        The hundreds place is more difficult than the thousands, because there are several mutually exclusive ways it could be expressed, depending on its value. +

                                                                                                                                                                                                          +
                                                                                                                                                                                                        • 100 = C +
                                                                                                                                                                                                        • 200 = CC +
                                                                                                                                                                                                        • 300 = CCC +
                                                                                                                                                                                                        • 400 = CD +
                                                                                                                                                                                                        • 500 = D +
                                                                                                                                                                                                        • 600 = DC +
                                                                                                                                                                                                        • 700 = DCC +
                                                                                                                                                                                                        • 800 = DCCC +
                                                                                                                                                                                                        • 900 = CM +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        So there are four possible patterns: +

                                                                                                                                                                                                          +
                                                                                                                                                                                                        • CM +
                                                                                                                                                                                                        • CD +
                                                                                                                                                                                                        • Zero to three C characters (zero if the hundreds place is 0) +
                                                                                                                                                                                                        • D, followed by zero to three C characters +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        The last two patterns can be combined: +

                                                                                                                                                                                                          +
                                                                                                                                                                                                        • an optional D, followed by zero to three C characters +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        This example shows how to validate the hundreds place of a Roman numeral. +

                                                                                                                                                                                                        +>>> import re
                                                                                                                                                                                                        +>>> pattern = '^M?M?M?(CM|CD|D?C?C?C?)$'  
                                                                                                                                                                                                        +>>> re.search(pattern, 'MCM')             
                                                                                                                                                                                                        +<SRE_Match object at 01070390>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MD')              
                                                                                                                                                                                                        +<SRE_Match object at 01073A50>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMMCCC')          
                                                                                                                                                                                                        +<SRE_Match object at 010748A8>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MCMC')            
                                                                                                                                                                                                        +>>> re.search(pattern, '')                
                                                                                                                                                                                                        +<SRE_Match object at 01071D98>
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. This pattern starts out the same as the previous one, checking for the beginning of the string (^), then the thousands place (M?M?M?). Then it has the new part, in parentheses, which defines a set of three mutually exclusive patterns, separated by vertical bars: CM, CD, and D?C?C?C? (which is an optional D followed by zero to three optional C characters). The regular expression parser checks for each of these patterns in order (from left to right), takes the first one that matches, and ignores the rest. +
                                                                                                                                                                                                        2. 'MCM' matches because the first M matches, the second and third M characters are ignored, and the CM matches (so the CD and D?C?C?C? patterns are never even considered). MCM is the Roman numeral representation of 1900. +
                                                                                                                                                                                                        3. 'MD' matches because the first M matches, the second and third M characters are ignored, and the D?C?C?C? pattern matches D (each of the three C characters are optional and are ignored). MD is the Roman numeral representation of 1500. +
                                                                                                                                                                                                        4. 'MMMCCC' matches because all three M characters match, and the D?C?C?C? pattern matches CCC (the D is optional and is ignored). MMMCCC is the Roman numeral representation of 3300. +
                                                                                                                                                                                                        5. 'MCMC' does not match. The first M matches, the second and third M characters are ignored, and the CM matches, but then the $ does not match because you're not at the end of the string yet (you still have an unmatched C character). The C does not match as part of the D?C?C?C? pattern, because the mutually exclusive CM pattern has already matched. +
                                                                                                                                                                                                        6. Interestingly, an empty string still matches this pattern, because all the M characters are optional and ignored, and the empty string matches the D?C?C?C? pattern where all the characters are optional and ignored. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Whew! See how quickly regular expressions can get nasty? And you've only covered the thousands and hundreds places of Roman numerals. But if you followed all that, the tens and ones places are easy, because they're exactly the same pattern. But let's look at another way to express the pattern. +

                                                                                                                                                                                                        Using the {n,m} Syntax

                                                                                                                                                                                                        +

                                                                                                                                                                                                        In the previous section, you were dealing with a pattern where the same character could be repeated up to three times. There is another way to express this in regular expressions, which some people find more readable. First look at the method we already used in the previous example. +

                                                                                                                                                                                                        +>>> import re
                                                                                                                                                                                                        +>>> pattern = '^M?M?M?$'
                                                                                                                                                                                                        +>>> re.search(pattern, 'M')     
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EE090>
                                                                                                                                                                                                        +>>> pattern = '^M?M?M?$'
                                                                                                                                                                                                        +>>> re.search(pattern, 'MM')    
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> pattern = '^M?M?M?$'
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMM')   
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EE090>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMMM')  
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. This matches the start of the string, and then the first optional M, but not the second and third M (but that's okay because they're optional), and then the end of the string. +
                                                                                                                                                                                                        2. This matches the start of the string, and then the first and second optional M, but not the third M (but that's okay because it's optional), and then the end of the string. +
                                                                                                                                                                                                        3. This matches the start of the string, and then all three optional M, and then the end of the string. +
                                                                                                                                                                                                        4. This matches the start of the string, and then all three optional M, but then does not match the the end of the string (because there is still one unmatched M), so the pattern does not match and returns None. +
                                                                                                                                                                                                        +
                                                                                                                                                                                                        +>>> pattern = '^M{0,3}$'        
                                                                                                                                                                                                        +>>> re.search(pattern, 'M')     
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MM')    
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EE090>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMM')   
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEDA8>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMMM')  
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. This pattern says: “Match the start of the string, then anywhere from zero to three M characters, then the end of the string.” The 0 and 3 can be any numbers; if you want to match at least one but no more than three M characters, you could say M{1,3}. +
                                                                                                                                                                                                        2. This matches the start of the string, then one M out of a possible three, then the end of the string. +
                                                                                                                                                                                                        3. This matches the start of the string, then two M out of a possible three, then the end of the string. +
                                                                                                                                                                                                        4. This matches the start of the string, then three M out of a possible three, then the end of the string. +
                                                                                                                                                                                                        5. This matches the start of the string, then three M out of a possible three, but then does not match the end of the string. The regular expression allows for up to only three M characters before the end of the string, but you have four, so the pattern does not match and returns None. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Checking for tens and ones

                                                                                                                                                                                                        +

                                                                                                                                                                                                        Now let's expand the Roman numeral regular expression to cover the tens and ones place. This example shows the check for tens. +

                                                                                                                                                                                                        +>>> pattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)$'
                                                                                                                                                                                                        +>>> re.search(pattern, 'MCMXL')     
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MCML')      
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MCMLX')     
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MCMLXXX')   
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MCMLXXXX')  
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. This matches the start of the string, then the first optional M, then CM, then XL, then the end of the string. Remember, the (A|B|C) syntax means “match exactly one of A, B, or C”. You match XL, so you ignore the XC and L?X?X?X? choices, and then move on to the end of the string. MCML is the Roman numeral representation of 1940. +
                                                                                                                                                                                                        2. This matches the start of the string, then the first optional M, then CM, then L?X?X?X?. Of the L?X?X?X?, it matches the L and skips all three optional X characters. Then you move to the end of the string. MCML is the Roman numeral representation of 1950. +
                                                                                                                                                                                                        3. This matches the start of the string, then the first optional M, then CM, then the optional L and the first optional X, skips the second and third optional X, then the end of the string. MCMLX is the Roman numeral representation of 1960. +
                                                                                                                                                                                                        4. This matches the start of the string, then the first optional M, then CM, then the optional L and all three optional X characters, then the end of the string. MCMLXXX is the Roman numeral representation of 1980. +
                                                                                                                                                                                                        5. This matches the start of the string, then the first optional M, then CM, then the optional L and all three optional X characters, then fails to match the end of the string because there is still one more X unaccounted for. So the entire pattern fails to match, and returns None. MCMLXXXX is not a valid Roman numeral. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        The expression for the ones place follows the same pattern. I'll spare you the details and show you the end result. +

                                                                                                                                                                                                        +>>> pattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'
                                                                                                                                                                                                        +

                                                                                                                                                                                                        So what does that look like using this alternate {n,m} syntax? This example shows the new syntax. +

                                                                                                                                                                                                        +>>> pattern = '^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
                                                                                                                                                                                                        +>>> re.search(pattern, 'MDLV')              
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMDCLXVI')          
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMMMDCCCLXXXVIII')  
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'I')                 
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. This matches the start of the string, then one of a possible four M characters, then D?C{0,3}. Of that, it matches the optional D and zero of three possible C characters. Moving on, it matches L?X{0,3} by matching the optional L and zero of three possible X characters. Then it matches V?I{0,3} by matching the optional V and zero of three possible I characters, and finally the end of the string. MDLV is the Roman numeral representation of 1555. +
                                                                                                                                                                                                        2. This matches the start of the string, then two of a possible four M characters, then the D?C{0,3} with a D and one of three possible C characters; then L?X{0,3} with an L and one of three possible X characters; then V?I{0,3} with a V and one of three possible I characters; then the end of the string. MMDCLXVI is the Roman numeral representation of 2666. +
                                                                                                                                                                                                        3. This matches the start of the string, then four out of four M characters, then D?C{0,3} with a D and three out of three C characters; then L?X{0,3} with an L and three out of three X characters; then V?I{0,3} with a V and three out of three I characters; then the end of the string. MMMMDCCCLXXXVIII is the Roman numeral representation of 3888, and it's the longest Roman numeral you can write without extended syntax. +
                                                                                                                                                                                                        4. Watch closely. (I feel like a magician. “Watch closely, kids, I'm going to pull a rabbit out of my hat.”) This matches the start of the string, then zero out of four M, then matches D?C{0,3} by skipping the optional D and matching zero out of three C, then matches L?X{0,3} by skipping the optional L and matching zero out of three X, then matches V?I{0,3} by skipping the optional V and matching one out of three I. Then the end of the string. Whoa. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        If you followed all that and understood it on the first try, you're doing better than I did. Now imagine trying to understand someone else's regular expressions, in the middle of a critical function of a large program. Or even imagine coming back to your own regular expressions a few months later. I've done it, and it's not a pretty sight. +

                                                                                                                                                                                                        Now let's explore an alternate syntax that can help keep your expressions maintainable. +

                                                                                                                                                                                                        Verbose Regular Expressions

                                                                                                                                                                                                        +

                                                                                                                                                                                                        So far you've just been dealing with what I'll call “compact” regular expressions. As you've seen, they are difficult to read, and even if you figure out what one does, that's no guarantee that you'll be able to understand it six months later. What you really need is inline documentation. +

                                                                                                                                                                                                        Python allows you to do this with something called verbose regular expressions. A verbose regular expression is different from a compact regular expression in two ways: +

                                                                                                                                                                                                          +
                                                                                                                                                                                                        • Whitespace is ignored. Spaces, tabs, and carriage returns are not matched as spaces, tabs, and carriage returns. They're not matched at all. (If you want to match a space in a verbose regular expression, you'll need to escape it by putting a backslash in front of it.) +
                                                                                                                                                                                                        • Comments are ignored. A comment in a verbose regular expression is just like a comment in Python code: it starts with a # character and goes until the end of the line. In this case it's a comment within a multi-line string instead of within your source code, but it works the same way. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        This will be more clear with an example. Let's revisit the compact regular expression you've been working with, and make it a verbose regular expression. This example shows how. +

                                                                                                                                                                                                        +>>> pattern = """
                                                                                                                                                                                                        +    ^ # beginning of string
                                                                                                                                                                                                        +    M{0,4}              # thousands - 0 to 4 M's
                                                                                                                                                                                                        +    (CM|CD|D?C{0,3})    # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),
                                                                                                                                                                                                        +      #            or 500-800 (D, followed by 0 to 3 C's)
                                                                                                                                                                                                        +    (XC|XL|L?X{0,3})    # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),
                                                                                                                                                                                                        +      #        or 50-80 (L, followed by 0 to 3 X's)
                                                                                                                                                                                                        +    (IX|IV|V?I{0,3})    # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
                                                                                                                                                                                                        +      #        or 5-8 (V, followed by 0 to 3 I's)
                                                                                                                                                                                                        +    $ # end of string
                                                                                                                                                                                                        +    """
                                                                                                                                                                                                        +>>> re.search(pattern, 'M', re.VERBOSE)                 
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MCMLXXXIX', re.VERBOSE)         
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'MMMMDCCCLXXXVIII', re.VERBOSE)  
                                                                                                                                                                                                        +<_sre.SRE_Match object at 0x008EEB48>
                                                                                                                                                                                                        +>>> re.search(pattern, 'M')                             
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. The most important thing to remember when using verbose regular expressions is that you need to pass an extra argument when working with them: re.VERBOSE is a constant defined in the re module that signals that the pattern should be treated as a verbose regular expression. As you can see, this pattern has quite a bit of whitespace (all of which is ignored), and several comments (all of which are ignored). Once you ignore the whitespace and the comments, this is exactly the same regular expression as you saw in the previous section, but it's a lot more readable. +
                                                                                                                                                                                                        2. This matches the start of the string, then one of a possible four M, then CM, then L and three of a possible three X, then IX, then the end of the string. +
                                                                                                                                                                                                        3. This matches the start of the string, then four of a possible four M, then D and three of a possible three C, then L and three of a possible three X, then V and three of a possible three I, then the end of the string. +
                                                                                                                                                                                                        4. This does not match. Why? Because it doesn't have the re.VERBOSE flag, so the re.search function is treating the pattern as a compact regular expression, with significant whitespace and literal hash marks. Python can't auto-detect whether a regular expression is verbose or not. Python assumes every regular expression is compact unless you explicitly state that it is verbose. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Case study: parsing phone numbers

                                                                                                                                                                                                        +

                                                                                                                                                                                                        So far you've concentrated on matching whole patterns. Either the pattern matches, or it doesn't. But regular expressions are much more powerful than that. When a regular expression does match, you can pick out specific pieces of it. You can find out what matched where. +

                                                                                                                                                                                                        This example came from another real-world problem I encountered, again from a previous day job. The problem: parsing an American phone number. The client wanted to be able to enter the number free-form (in a single field), but then wanted to store the area code, trunk, number, and optionally an extension separately in the company's database. I scoured the Web and found many examples of regular expressions that purported to do this, but none of them were permissive enough. +

                                                                                                                                                                                                        Here are the phone numbers I needed to be able to accept: +

                                                                                                                                                                                                          +
                                                                                                                                                                                                        • 800-555-1212 +
                                                                                                                                                                                                        • 800 555 1212 +
                                                                                                                                                                                                        • 800.555.1212 +
                                                                                                                                                                                                        • (800) 555-1212 +
                                                                                                                                                                                                        • 1-800-555-1212 +
                                                                                                                                                                                                        • 800-555-1212-1234 +
                                                                                                                                                                                                        • 800-555-1212x1234 +
                                                                                                                                                                                                        • 800-555-1212 ext. 1234 +
                                                                                                                                                                                                        • work 1-(800) 555.1212 #1234 +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Quite a variety! In each of these cases, I need to know that the area code was 800, the trunk was 555, and the rest of the phone number was 1212. For those with an extension, I need to know that the extension was 1234. +

                                                                                                                                                                                                        Let's work through developing a solution for phone number parsing. This example shows the first step. +

                                                                                                                                                                                                        +>>> phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})$')  
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212').groups()             
                                                                                                                                                                                                        +('800', '555', '1212')
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212-1234')                 
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. Always read regular expressions from left to right. This one matches the beginning of the string, and then (\d{3}). What's \d{3}? Well, the {3} means “match exactly three numeric digits”; it's a variation on the {n,m} syntax you saw earlier. \d means “any numeric digit” (0 through 9). Putting it in parentheses means “match exactly three numeric digits, and then remember them as a group that I can ask for later”. Then match a literal hyphen. Then match another group of exactly three digits. Then another literal hyphen. Then another group of exactly four digits. Then match the end of the string. +
                                                                                                                                                                                                        2. To get access to the groups that the regular expression parser remembered along the way, use the groups() method on the object that the search() method returns. It will return a tuple of however many groups were defined in the regular expression. In this case, you defined three groups, one with three digits, one with three digits, and one with four digits. +
                                                                                                                                                                                                        3. This regular expression is not the final answer, because it doesn't handle a phone number with an extension on the end. For that, you'll need to expand the regular expression. +
                                                                                                                                                                                                        +
                                                                                                                                                                                                        +>>> phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})-(\d+)$')  
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212-1234').groups()              
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +>>> phonePattern.search('800 555 1212 1234')                       
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212')                            
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. This regular expression is almost identical to the previous one. Just as before, you match the beginning of the string, then a remembered group of three digits, then a hyphen, then a remembered group of three digits, then a hyphen, then a remembered group of four digits. What's new is that you then match another hyphen, and a remembered group of one or more digits, then the end of the string. +
                                                                                                                                                                                                        2. The groups() method now returns a tuple of four elements, since the regular expression now defines four groups to remember. +
                                                                                                                                                                                                        3. Unfortunately, this regular expression is not the final answer either, because it assumes that the different parts of the phone number are separated by hyphens. What if they're separated by spaces, or commas, or dots? You need a more general solution to match several different types of separators. +
                                                                                                                                                                                                        4. Oops! Not only does this regular expression not do everything you want, it's actually a step backwards, because now you can't parse phone numbers without an extension. That's not what you wanted at all; if the extension is there, you want to know what it is, but if it's not there, you still want to know what the different parts of the main number are. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        The next example shows the regular expression to handle separators between the different parts of the phone number. +

                                                                                                                                                                                                        +>>> phonePattern = re.compile(r'^(\d{3})\D+(\d{3})\D+(\d{4})\D+(\d+)$')  
                                                                                                                                                                                                        +>>> phonePattern.search('800 555 1212 1234').groups()  
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212-1234').groups()  
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +>>> phonePattern.search('80055512121234')              
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212')                
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. Hang on to your hat. You're matching the beginning of the string, then a group of three digits, then \D+. What the heck is that? Well, \D matches any character except a numeric digit, and + means “1 or more”. So \D+ matches one or more characters that are not digits. This is what you're using instead of a literal hyphen, to try to match different separators. +
                                                                                                                                                                                                        2. Using \D+ instead of - means you can now match phone numbers where the parts are separated by spaces instead of hyphens. +
                                                                                                                                                                                                        3. Of course, phone numbers separated by hyphens still work too. +
                                                                                                                                                                                                        4. Unfortunately, this is still not the final answer, because it assumes that there is a separator at all. What if the phone number is entered without any spaces or hyphens at all? +
                                                                                                                                                                                                        5. Oops! This still hasn't fixed the problem of requiring extensions. Now you have two problems, but you can solve both of them with the same technique. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        The next example shows the regular expression for handling phone numbers without separators. +

                                                                                                                                                                                                        +>>> phonePattern = re.compile(r'^(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')  
                                                                                                                                                                                                        +>>> phonePattern.search('80055512121234').groups()      
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +>>> phonePattern.search('800.555.1212 x1234').groups()  
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212').groups()        
                                                                                                                                                                                                        +('800', '555', '1212', '')
                                                                                                                                                                                                        +>>> phonePattern.search('(800)5551212 x1234')           
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. The only change you've made since that last step is changing all the + to *. Instead of \D+ between the parts of the phone number, you now match on \D*. Remember that + means “1 or more”? Well, * means “zero or more”. So now you should be able to parse phone numbers even when there is no separator character at all. +
                                                                                                                                                                                                        2. Lo and behold, it actually works. Why? You matched the beginning of the string, then a remembered group of three digits (800), then zero non-numeric characters, then a remembered group of three digits (555), then zero non-numeric characters, then a remembered group of four digits (1212), then zero non-numeric characters, then a remembered group of an arbitrary number of digits (1234), then the end of the string. +
                                                                                                                                                                                                        3. Other variations work now too: dots instead of hyphens, and both a space and an x before the extension. +
                                                                                                                                                                                                        4. Finally, you've solved the other long-standing problem: extensions are optional again. If no extension is found, the groups() method still returns a tuple of four elements, but the fourth element is just an empty string. +
                                                                                                                                                                                                        5. I hate to be the bearer of bad news, but you're not finished yet. What's the problem here? There's an extra character before the area code, but the regular expression assumes that the area code is the first thing at the beginning of the string. No problem, you can use the same technique of “zero or more non-numeric characters” to skip over the leading characters before the area code. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        The next example shows how to handle leading characters in phone numbers. +

                                                                                                                                                                                                        +>>> phonePattern = re.compile(r'^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')  
                                                                                                                                                                                                        +>>> phonePattern.search('(800)5551212 ext. 1234').groups()                  
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212').groups()                            
                                                                                                                                                                                                        +('800', '555', '1212', '')
                                                                                                                                                                                                        +>>> phonePattern.search('work 1-(800) 555.1212 #1234')                      
                                                                                                                                                                                                        +>>> 
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. This is the same as in the previous example, except now you're matching \D*, zero or more non-numeric characters, before the first remembered group (the area code). Notice that you're not remembering these non-numeric characters (they're not in parentheses). If you find them, you'll just skip over them and then start remembering the area code whenever you get to it. +
                                                                                                                                                                                                        2. You can successfully parse the phone number, even with the leading left parenthesis before the area code. (The right parenthesis after the area code is already handled; it's treated as a non-numeric separator and matched by the \D* after the first remembered group.) +
                                                                                                                                                                                                        3. Just a sanity check to make sure you haven't broken anything that used to work. Since the leading characters are entirely optional, this matches the beginning of the string, then zero non-numeric characters, then a remembered group of three digits (800), then one non-numeric character (the hyphen), then a remembered group of three digits (555), then one non-numeric character (the hyphen), then a remembered group of four digits (1212), then zero non-numeric characters, then a remembered group of zero digits, then the end of the string. +
                                                                                                                                                                                                        4. This is where regular expressions make me want to gouge my eyes out with a blunt object. Why doesn't this phone number match? Because there's a 1 before the area code, but you assumed that all the leading characters before the area code were non-numeric characters (\D*). Aargh. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Let's back up for a second. So far the regular expressions have all matched from the beginning of the string. But now you see that there may be an indeterminate amount of stuff at the beginning of the string that you want to ignore. Rather than trying to match it all just so you can skip over it, let's take a different approach: don't explicitly match the beginning of the string at all. This approach is shown in the next example. +

                                                                                                                                                                                                        +>>> phonePattern = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')  
                                                                                                                                                                                                        +>>> phonePattern.search('work 1-(800) 555.1212 #1234').groups()         
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212')                                 
                                                                                                                                                                                                        +('800', '555', '1212', '')
                                                                                                                                                                                                        +>>> phonePattern.search('80055512121234')                               
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. Note the lack of ^ in this regular expression. You are not matching the beginning of the string anymore. There's nothing that says you need to match the entire input with your regular expression. The regular expression engine will do the hard work of figuring out where the input string starts to match, and go from there. +
                                                                                                                                                                                                        2. Now you can successfully parse a phone number that includes leading characters and a leading digit, plus any number of any kind of separators around each part of the phone number. +
                                                                                                                                                                                                        3. Sanity check. this still works. +
                                                                                                                                                                                                        4. That still works too. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        See how quickly a regular expression can get out of control? Take a quick glance at any of the previous iterations. Can you tell the difference between one and the next? +

                                                                                                                                                                                                        While you still understand the final answer (and it is the final answer; if you've discovered a case it doesn't handle, I don't want to know about it), let's write it out as a verbose regular expression, before you forget why you made the choices you made. +

                                                                                                                                                                                                        +>>> phonePattern = re.compile(r'''
                                                                                                                                                                                                        +                # don't match beginning of string, number can start anywhere
                                                                                                                                                                                                        +    (\d{3})     # area code is 3 digits (e.g. '800')
                                                                                                                                                                                                        +    \D*         # optional separator is any number of non-digits
                                                                                                                                                                                                        +    (\d{3})     # trunk is 3 digits (e.g. '555')
                                                                                                                                                                                                        +    \D*         # optional separator
                                                                                                                                                                                                        +    (\d{4})     # rest of number is 4 digits (e.g. '1212')
                                                                                                                                                                                                        +    \D*         # optional separator
                                                                                                                                                                                                        +    (\d*)       # extension is optional and can be any number of digits
                                                                                                                                                                                                        +    $           # end of string
                                                                                                                                                                                                        +    ''', re.VERBOSE)
                                                                                                                                                                                                        +>>> phonePattern.search('work 1-(800) 555.1212 #1234').groups()  
                                                                                                                                                                                                        +('800', '555', '1212', '1234')
                                                                                                                                                                                                        +>>> phonePattern.search('800-555-1212')                          
                                                                                                                                                                                                        +('800', '555', '1212', '')
                                                                                                                                                                                                        +
                                                                                                                                                                                                          +
                                                                                                                                                                                                        1. Other than being spread out over multiple lines, this is exactly the same regular expression as the last step, so it's no surprise that it parses the same inputs. +
                                                                                                                                                                                                        2. Final sanity check. Yes, this still works. You're done. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Summary

                                                                                                                                                                                                        +

                                                                                                                                                                                                        This is just the tiniest tip of the iceberg of what regular expressions can do. In other words, even though you're completely overwhelmed by them now, believe me, you ain't seen nothing yet. +

                                                                                                                                                                                                        You should now be familiar with the following techniques: +

                                                                                                                                                                                                          +
                                                                                                                                                                                                        • ^ matches the beginning of a string. +
                                                                                                                                                                                                        • $ matches the end of a string. +
                                                                                                                                                                                                        • \b matches a word boundary. +
                                                                                                                                                                                                        • \d matches any numeric digit. +
                                                                                                                                                                                                        • \D matches any non-numeric character. +
                                                                                                                                                                                                        • x? matches an optional x character (in other words, it matches an x zero or one times). +
                                                                                                                                                                                                        • x* matches x zero or more times. +
                                                                                                                                                                                                        • x+ matches x one or more times. +
                                                                                                                                                                                                        • x{n,m} matches an x character at least n times, but not more than m times. +
                                                                                                                                                                                                        • (a|b|c) matches either a or b or c. +
                                                                                                                                                                                                        • (x) in general is a remembered group. You can get the value of what matched by using the groups() method of the object returned by re.search. +
                                                                                                                                                                                                        +

                                                                                                                                                                                                        Regular expressions are extremely powerful, but they are not the correct solution for every problem. You should learn enough about them to know when they are appropriate, when they will solve your problems, and when they will cause more problems than they solve. +

                                                                                                                                                                                                        © 2001–4, 2009 ark Pilgrim, CC-BY-SA-3.0 + + diff --git a/table-of-contents.html b/table-of-contents.html index c23add3..a7c1e3c 100644 --- a/table-of-contents.html +++ b/table-of-contents.html @@ -50,19 +50,9 @@ ul li ol{margin:0;padding:0 0 0 2.5em}

                                                                                                                                                                                                      • Diving in
                                                                                                                                                                                                      • Booleans
                                                                                                                                                                                                      • Numbers -
                                                                                                                                                                                                      • Lists
                                                                                                                                                                                                      • Sets -
                                                                                                                                                                                                      • Dictionaries +
                                                                                                                                                                                                      • Dictionaries
                                                                                                                                                                                                      • None
                                                                                                                                                                                                      • Further reading
                                                                                                                                                                                                @@ -82,6 +72,23 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
                                                                                                                                                                                              • Byte streams
                                                                                                                                                                                              • Summary
                                                                                                                                                                                          +
                                                                                                                                                                                        29. Regular expressions +
                                                                                                                                                                                            +
                                                                                                                                                                                          1. Diving in +
                                                                                                                                                                                          2. Case study: street addresses +
                                                                                                                                                                                          3. Case study: Roman numerals +
                                                                                                                                                                                              +
                                                                                                                                                                                            1. Checking for thousands +
                                                                                                                                                                                            2. Checking for hundreds +
                                                                                                                                                                                            +
                                                                                                                                                                                          4. Using the {n,m} Syntax +
                                                                                                                                                                                              +
                                                                                                                                                                                            1. Checking for tens and ones +
                                                                                                                                                                                            +
                                                                                                                                                                                          5. Verbose regular expressions +
                                                                                                                                                                                          6. Case study: parsing phone numbers +
                                                                                                                                                                                          7. Summary +
                                                                                                                                                                                        30. The power of introspection
                                                                                                                                                                                          1. Diving in @@ -114,35 +121,39 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
                                                                                                                                                                                          2. ...mention why from module import * is only allowed at module level
                                                                                                                                                                                        -
                                                                                                                                                                                      4. Exceptions +
                                                                                                                                                                                      5. Unit testing
                                                                                                                                                                                          -
                                                                                                                                                                                        1. ... +
                                                                                                                                                                                        2. Introduction to Roman numerals +
                                                                                                                                                                                        3. Diving in +
                                                                                                                                                                                        4. Introducing romantest.py +
                                                                                                                                                                                        5. Testing for success +
                                                                                                                                                                                        6. Testing for failure +
                                                                                                                                                                                        7. Testing for sanity +
                                                                                                                                                                                        +
                                                                                                                                                                                      6. Test-first programming +
                                                                                                                                                                                          +
                                                                                                                                                                                        1. roman.py, stage 1 +
                                                                                                                                                                                        2. roman.py, stage 2 +
                                                                                                                                                                                        3. roman.py, stage 3 +
                                                                                                                                                                                        4. roman.py, stage 4 +
                                                                                                                                                                                        5. roman.py, stage 5 +
                                                                                                                                                                                        +
                                                                                                                                                                                      7. Refactoring your code +
                                                                                                                                                                                          +
                                                                                                                                                                                        1. Handling bugs +
                                                                                                                                                                                        2. Handling changing requirements +
                                                                                                                                                                                        3. The art of refactoring +
                                                                                                                                                                                        4. Postscript +
                                                                                                                                                                                        5. Summary
                                                                                                                                                                                      8. Files
                                                                                                                                                                                        1. File objects
                                                                                                                                                                                        2. Reading files
                                                                                                                                                                                        3. Close your files... or don't -
                                                                                                                                                                                        4. Handling I/O errors +
                                                                                                                                                                                        5. Handling errors (exceptions)
                                                                                                                                                                                        6. Writing to files
                                                                                                                                                                                        -
                                                                                                                                                                                      9. Regular expressions -
                                                                                                                                                                                          -
                                                                                                                                                                                        1. Diving in -
                                                                                                                                                                                        2. Case study: street addresses -
                                                                                                                                                                                        3. Case study: Roman numerals -
                                                                                                                                                                                            -
                                                                                                                                                                                          1. Checking for thousands -
                                                                                                                                                                                          2. Checking for hundreds -
                                                                                                                                                                                          -
                                                                                                                                                                                        4. Using the {n,m} syntax -
                                                                                                                                                                                            -
                                                                                                                                                                                          1. Checking for tens and ones -
                                                                                                                                                                                          -
                                                                                                                                                                                        5. Verbose regular expressions -
                                                                                                                                                                                        6. Case study: parsing phone numbers -
                                                                                                                                                                                        7. Summary -
                                                                                                                                                                                      10. HTML processing
                                                                                                                                                                                        1. Diving in @@ -186,31 +197,6 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
                                                                                                                                                                                        2. Putting it all together
                                                                                                                                                                                        3. Summary
                                                                                                                                                                                        -
                                                                                                                                                                                      11. Unit testing -
                                                                                                                                                                                          -
                                                                                                                                                                                        1. Introduction to Roman numerals -
                                                                                                                                                                                        2. Diving in -
                                                                                                                                                                                        3. Introducing romantest.py -
                                                                                                                                                                                        4. Testing for success -
                                                                                                                                                                                        5. Testing for failure -
                                                                                                                                                                                        6. Testing for sanity -
                                                                                                                                                                                        -
                                                                                                                                                                                      12. Test-first programming -
                                                                                                                                                                                          -
                                                                                                                                                                                        1. roman.py, stage 1 -
                                                                                                                                                                                        2. roman.py, stage 2 -
                                                                                                                                                                                        3. roman.py, stage 3 -
                                                                                                                                                                                        4. roman.py, stage 4 -
                                                                                                                                                                                        5. roman.py, stage 5 -
                                                                                                                                                                                        -
                                                                                                                                                                                      13. Refactoring your code -
                                                                                                                                                                                          -
                                                                                                                                                                                        1. Handling bugs -
                                                                                                                                                                                        2. Handling changing requirements -
                                                                                                                                                                                        3. The art of refactoring -
                                                                                                                                                                                        4. Postscript -
                                                                                                                                                                                        5. Summary -
                                                                                                                                                                                      14. Dynamic functions
                                                                                                                                                                                        1. Diving in