Already the industry standard for Python users, "ProgrammingPython" fromO'Reilly just got even better. This third edition has been updated toreflect current best practices andthe abundance of changes introduced by the latest version of thelanguage, Python 2.5.
Whether you're a novice or an advancedpractitioner, you'll find thisrefreshed book more than lives up to its reputation. "ProgrammingPython," 3rd Edition, teaches you the rightway to code. It explains Python language syntax and programmingtechniques in a clear and concisemanner, with numerous examples that illustrate both correct usage andcommon idioms. By reading thiscomprehensive guide, you'll learn how to apply Python in real-worldproblem domains such as: GUI programmingInternet scriptingParallel processingDatabase managementNetworked applications
"Programming Python," Third Edition coverseach of thesetarget domainsgradually, beginning with in-depth discussions of core concepts andthen progressing toward completeprograms. Large examples do appear, but only after you've learnedenough to understand their techniques andcode.
Along the way, you'll also learn how to use the Python language inrealistically scaled programs--concepts such as Object-Oriented Programming (OOP) and code reuseare recurring side themes throughout thistext. If you're interested in Python programming, then this O'Reillyclassic needs to be within arm's reach. Thewealth of practical advice, snippets of code, and patterns of programdesign can all be put into use on adaily basis--making your life easier and more productive.
Reviews of the second edition:
.".".about as comprehensive as any book can be.""--Dr. Dobb's Journal
""If the language had manuals, they would undoubtedlybe the texts from O'Reilly...'Learning Python' and 'Programming Python'are definitive treatments.""--SD Times
Author(s): Mark Lutz
Edition: 3
Publisher: O'Reilly Media
Year: 2006
Language: English
Pages: 1552
Table of Contents
Foreword
How Time Flies!
Coming Attractions
Concluding Remarks
Foreword to the Second Edition (2001)
Foreword from the First Edition (1996)
Preface
“And Now for Something Completely Different . . . Again”
About This Book
About This Edition
Python Changes
Example Changes
Topic Changes
Focus Unchanged
This Book’s Motivation
This Book’s Structure
This Edition’s Design
It’s Been Updated for Python 2.4 (and 2.5)
It’s Been Reorganized
It Covers New Topics
It’s Still Mostly Platform-Neutral
C integration code platform issues
It’s Still Focused for a More Advanced Audience
It’s Still Example-Oriented
Using the Book’s Examples
The Book Examples Tree
The Book Examples Distribution Package
Running Examples: The Short Story
Running Examples: The Details
Demos and gadgets
Setup requirements
Web-based examples
Top-level programs
Conventions Used in This Book
Safari® Enabled
Where to Look for Updates
Contacting O’Reilly
Using Code Examples
Acknowledgments
Part I
Introducing Python
“And Now for Something Completely Different”
Python Philosophy 101
The Life of Python
Signs of the Python Times
Recent Growth (As of 2005, at Least)
The Compulsory Features List
What’s Python Good For?
What’s Python Not Good For?
Truth in Advertising
A Sneak Preview
“Programming Python: The Short Story”
The Task
Step 1: Representing Records
Using Lists
A database list
Field labels
Using Dictionaries
Other ways to make dictionaries
Lists of dictionaries
Nested structures
Dictionaries of dictionaries
Step 2: Storing Records Persistently
Using Formatted Files
Test data script
Data format script
Utility scripts
Using Pickle Files
Using Per-Record Pickle Files
Using Shelves
Step 3: Stepping Up to OOP
Using Classes
Adding Behavior
Adding Inheritance
Refactoring Code
Augmenting methods
Display format
Constructor customization
Alternative classes
Adding Persistence
Other Database Options
Step 4: Adding Console Interaction
A Console Shelve Interface
Step 5: Adding a GUI
GUI Basics
Using OOP for GUIs
Getting Input from a User
A GUI Shelve Interface
Coding the GUI
Using the GUI
Future directions
Step 6: Adding a Web Interface
CGI Basics
Running a Web Server
Using Query Strings and urllib
Formatting Reply Text
A Web-Based Shelve Interface
Coding the web site
Using the web site
Future directions
The End of the Demo
Part II
System Tools
“The os.path to Knowledge”
Why Python Here?
The Next Five Chapters
System Scripting Overview
Python System Modules
Module Documentation Sources
Paging Documentation Strings
Introducing String Methods
File Operation Basics
Using Programs in Two Ways
Python Library Manuals
Commercially Published References
Introducing the sys Module
Platforms and Versions
The Module Search Path
The Loaded Modules Table
Exception Details
Other sys Module Exports
Introducing the os Module
The Big os Lists
Administrative Tools
Portability Constants
Basic os.path Tools
Running Shell Commands from Scripts
What’s a shell command?
Running shell commands
Communicating with shell commands
Shell command limitations
Other os Module Exports
Script Execution Context
Current Working Directory
CWD, Files, and Import Paths
CWD and Command Lines
Command-Line Arguments
Shell Environment Variables
Fetching Shell Variables
Changing Shell Variables
Shell Variable Details
Standard Streams
Redirecting Streams to Files and Programs
Redirecting streams to files
Chaining programs with pipes
Coding alternatives
Redirected streams and user interaction
Reading keyboard input
Redirecting Streams to Python Objects
The StringIO Module
Capturing the stderr Stream
Redirection Syntax in Print Statements
Other Redirection Options
File and Directory Tools
“Erase Your Hard Drive in Five Easy Steps!”
File Tools
Built-In File Objects
Output files
Input files
Reading lines with file iterators
Other file object modes
Binary datafiles
End-of-line translations on Windows
Parsing packed binary data with the struct module
File Tools in the os Module
Open mode flags
Other os file tools
File Scanners
Directory Tools
Walking One Directory
Running shell listing commands with os.popen
The glob module
The os.listdir call
Splitting and joining listing results
Walking Directory Trees
The os.path.walk visitor
The os.walk generator
Recursive os.listdir traversals
Rolling Your Own find Module
Parallel System Tools
“Telling the Monkeys What to Do”
Forking Processes
The fork/exec Combination
os.exec call formats
Spawned child program
Threads
The thread Module
Synchronizing access to global objects
Waiting for spawned thread exits
The threading Module
The Queue Module
The Global Interpreter Lock and Threads
The thread switch interval
Atomic operations
C API thread considerations
Program Exits
os Module Exits
Exit Status Codes
Process Exit Status
Thread Exits
Interprocess Communication
Pipes
Anonymous Pipe Basics
Bidirectional IPC with Pipes
Deadlocks, flushes, and unbuffered streams
Named Pipes (Fifos)
Signals
Other Ways to Start Programs
The os.spawn Calls
Launching Programs on Windows
Using the DOS start command
Using start in Python scripts
The os.startfile call
Other Program Launch Options
A Portable Program-Launch Framework
Other System Tools
System Examples: Utilities
“Splits and Joins and Alien Invasions”
Splitting and Joining Files
Splitting Files Portably
Operation modes
Binary file access
Manually closing files
Joining Files Portably
Reading by blocks or files
Sorting filenames
Usage Variations
Generating Forward-Link Web Pages
Page Template File
Page Generator Script
A Regression Test Script
Packing and Unpacking Files
Packing Files “++”
Application Hierarchy Superclasses
StreamApp: adding stream redirection
App: the root class
Why use classes here?
Automated Program Launchers
Launcher Module Clients
Launching Programs Without Environment Settings
Launching Web Browsers Portably
Launching browsers with command lines
Launching browsers with function calls
Viewing multimedia in browsers
A Portable Media File Player Tool
The Python webbrowser module
The Python mimetypes module
System Examples: Directories
“The Greps of Wrath”
Fixing DOS Line Ends
Converting Line Ends in One File
Slinging bytes and verifying results
Nonintrusive conversions
Slicing strings out of bounds
Binary file mode revisited
Converting Line Ends in One Directory
Converting Line Ends in an Entire Tree
The view from the top
Fixing DOS Filenames
Rewriting with os.path.walk
Searching Directory Trees
Greps and Globs in Shells and Python
Cleaning up bytecode files
A Python Tree Searcher
Visitor: Walking Trees Generically
Editing Files in Directory Trees
Global Replacements in Directory Trees
Collecting Matched Files in Trees
Suppressing status messages
Recoding Fixers with Visitors
Fixing File Permissions in Trees
Changing Unix Executable Path Lines
Summary: Counting Source Lines Four Ways
Copying Directory Trees
A Python Tree Copy Script
Recoding Copies with a Visitor-Based Class
Deleting Directory Trees
Recoding Deletions for Generality
Comparing Directory Trees
Finding Directory Differences
Finding Tree Differences
Running the Script
Verifying CD backups
Reporting Differences
Part III
Graphical User Interfaces
“Here’s Looking at You, Kid”
GUI Programming Topics
Python GUI Development Options
Tkinter Overview
Tkinter Pragmatics
Tkinter Documentation
Tkinter Extensions
Tkinter Structure
Climbing the GUI Learning Curve
“Hello World” in Four Lines (or Less)
Tkinter Coding Basics
Making Widgets
Geometry Managers
Running GUI Programs
Avoiding DOS consoles on Windows
Tkinter Coding Alternatives
Widget Resizing Basics
Configuring Widget Options and Window Titles
One More for Old Times’ Sake
Packing Widgets Without Saving Them
Adding Buttons and Callbacks
Widget Resizing Revisited: Expansion
Adding User-Defined Callback Handlers
Lambda Callback Handlers
Passing in values with default arguments
Passing in values with enclosing scope references
Enclosing scopes versus defaults
Bound Method Callback Handlers
Callable Class Object Callback Handlers
Other Tkinter Callback Protocols
Binding Events
Adding Multiple Widgets
Widget Resizing Revisited: Clipping
Attaching Widgets to Frames
Layout: Packing Order and Side Attachments
The Packer’s Expand and Fill Revisited
Using Anchor to Position Instead of Stretch
Customizing Widgets with Classes
Reusable GUI Components with Classes
Attaching Class Components
Extending Class Components
Standalone Container Classes
The End of the Tutorial
Python/Tkinter for Tcl/Tk Converts
A Tkinter Tour, Part 1
“Widgets and Gadgets and GUIs, Oh My!”
This Chapter’s Topics
Configuring Widget Appearance
Top-Level Windows
Toplevel and Tk Widgets
Top-Level Window Protocols
Dialogs
Standard (Common) Dialogs
A “smart” and reusable Quit button
A dialog demo launcher bar
Printing dialog results (and passing callback data with lambdas)
Letting users select colors on the fly
Other standard dialog calls
The Old-Style Dialog Module
Custom Dialogs
Making custom dialogs modal
Other ways to be modal
Binding Events
Message and Entry
Message
Entry
Programming Entry widgets
Laying out input forms
Going modal again
Tkinter “variables”
Checkbutton, Radiobutton, and Scale
Checkbuttons
Check buttons and variables
Radio Buttons
Radio buttons and variables
Radio buttons without variables
Hold onto your variables
Scales (Sliders)
Scales and variables
Running GUI Code Three Ways
Attaching Frames
Independent Windows
Running Programs
Cross-program communication
Coding for reusability
Images
Fun with Buttons and Pictures
Viewing and Processing Images with PIL
PIL Basics
Displaying Other Image Types with PIL
Displaying all images in a directory
Creating Image Thumbnails with PIL
Performance: saving thumbnail files
Layout: gridding and fixed-size widgets
Scrolling and canvases
A Tkinter Tour, Part 2
“On Today’s Menu: Spam, Spam, and Spam”
Menus
Top-Level Window Menus
Frame- and Menubutton-Based Menus
Using Menubuttons and Optionmenus
Windows with Both Menus and Toolbars
Automating menu construction
Listboxes and Scrollbars
Programming Listboxes
Programming Scroll Bars
Packing Scroll Bars
Text
Programming the Text Widget
Text is a Python string
String positions
Adding Text-Editing Operations
Using the clipboard
Composition versus inheritance
It’s called “Simple” for a reason
Advanced Text and Tag Operations
Canvas
Basic Canvas Operations
Programming the Canvas Widget
Coordinates
Object construction
Object identifiers and operations
Canvas object tags
Scrolling Canvases
Scrollable Canvases and Image Thumbnails
Scrolling images too: PyPhoto (ahead)
Using Canvas Events
Binding events on specific items
Grids
Grid Basics
grid Versus pack
Combining grid and pack
Making Gridded Widgets Expandable
Resizing in grids
Spanning columns and rows
Laying Out Larger Tables with grid
Time Tools, Threads, and Animation
Using Threads with GUIs
Using the after Method
Hiding and redrawing widgets and windows
Simple Animation Techniques
Using time.sleep loops
Using widget.after events
Using multiple time.sleep loop threads
Other Animation Concepts: Threads and Toolkits
The End of the Tour
The PyDemos and PyGadgets Launchers
PyDemos Launcher Bar
PyGadgets Launcher Bar
GUI Coding Techniques
“Building a Better Mouse Trap”
GuiMixin: Common Tool Mixin Classes
GuiMaker: Automating Menus and Toolbars
Subclass Protocols
GuiMaker Classes
GuiMaker Self-Test
BigGui: A Client Demo Program
ShellGui: GUIs for Command-Line Tools
A Generic Shell-Tools Display
Application-Specific Tool Set Classes
Adding GUI Frontends to Command Lines
GuiStreams: Redirecting Streams to Widgets
Using Redirection for the Packing Scripts
Reloading Callback Handlers Dynamically
Wrapping Up Top-Level Window Interfaces
GUIs, Threads, and Queues
Placing Callbacks on Queues
More Ways to Add GUIs to Non-GUI Code
Popping up GUI Windows on Demand
Adding a GUI As a Separate Program: Sockets
Adding a GUI As a Separate Program: Pipes
Complete GUI Programs
“Python, Open Source, and Camaros”
Examples in Other Chapters
This Chapter’s Strategy
PyEdit: A Text Editor Program/Object
Running PyEdit
Menus and toolbars
Dialogs
Running program code
New features in version 2.0
PyEdit Source Code
PyPhoto: An Image Viewer and Resizer
Running PyPhoto
PyPhoto Source Code
PyView: An Image and Notes Slideshow
Running PyView
PyView Source Code
PyDraw: Painting and Moving Graphics
Running PyDraw
PyDraw Source Code
PyClock: An Analog/Digital Clock Widget
A Quick Geometry Lesson
Running PyClock
PyClock Source Code
PyToe: A Tic-Tac-Toe Game Widget
Running PyToe
PyToe Source Code (Book Examples Distribution)
Where to Go from Here
Part IV
Network Scripting
“Tune In, Log On, and Drop Out”
Internet Scripting Topics
What we will cover
What we won’t cover
Running examples in this part of the book
Plumbing the Internet
The Socket Layer
Machine identifiers
The Protocol Layer
Port number rules
Clients and servers
Protocol structures
Python’s Internet Library Modules
Socket Programming
Socket Basics
Server socket calls
Client socket calls
Running socket programs locally
Running socket programs remotely
Socket pragmatics
Spawning clients in parallel
Talking to reserved ports
Handling Multiple Clients
Forking Servers
Running the forking server
Forking processes
Exiting from children
Killing the zombies
Preventing zombies with signal handlers
Threading Servers
Standard Library Server Classes
Third-Party Server Tools: Twisted
Multiplexing Servers with select
A select-based echo server
Running the select server
Choosing a Server Scheme
A Simple Python File Server
Running the File Server and Clients
Adding a User-Interface Frontend
Using Frames and command lines
Using grids and function calls
Using a reusable form-layout class
Client-Side Scripting
“Socket to Me!”
FTP: Transferring Files over the Net
Fetching Files with ftplib
Using urllib to FTP Files
FTP get and put Utilities
Download utility
Upload utility
Playing the Monty Python theme song
Adding user interfaces
Downloading Web Sites (Mirrors)
Uploading Web Sites
Refactoring Uploads and Downloads for Reuse
Refactoring with functions
Refactoring with classes
Uploads and Deletes with Subdirectories
Uploading local trees
Deleting remote trees
Processing Internet Email
POP: Fetching Email
Mail Configuration Module
POP Mail Reader Script
Fetching Messages
Fetching Email at the Interactive Prompt
SMTP: Sending Email
SMTP Mail Sender Script
Sending Messages
More Ways to Abuse the Net
Back to the Big Internet Picture
Sending Email from the Interactive Prompt
email: Parsing and Composing Mails
Message Objects
Basic email Interfaces in Action
pymail: A Console-Based Email Client
Running the pymail Console Client
The mailtools Utility Package
Initialization File
MailTool Class
MailSender Class
MailFetcher Class
General usage
Inbox synchronization tools
MailParser Class
Self-Test Script
Updating the pymail Console Client
NNTP: Accessing Newsgroups
HTTP: Accessing Web Sites
Module urllib Revisited
Other urllib Interfaces
Other Client-Side Scripting Options
The PyMailGUI Client
“Use the Source, Luke”
Source Code Modules
Why PyMailGUI?
Running PyMailGUI
Presentation Strategy
New in This Edition
A PyMailGUI Demo
Getting Started
Loading Mail
Threading Model
Load Server Interface
Offline Processing with Save and Open
Sending Email and Attachments
Viewing Email and Attachments
Email Replies and Forwards
Deleting Email
POP Message Numbers and Synchronization
Multiple Windows and Status Messages
PyMailGUI Implementation
Code Reuse
Code Structure
PyMailGui2: The Main Module
SharedNames: Program-Wide Globals
ListWindows: Message List Windows
ViewWindows: Message View Windows
messagecache: Message Cache Manager
popuputil: General-Purpose GUI Pop Ups
wraplines: Line Split Tools
mailconfig: User Configurations
PyMailGuiHelp: User Help Text
Ideas for Improvement
Server-Side Scripting
“Oh What a Tangled Web We Weave”
What’s a Server-Side CGI Script?
The Script Behind the Curtain
Writing CGI Scripts in Python
Running Server-Side Examples
Web Server Options
Running a Local Web Server
The Server-Side Examples Root Page
Viewing Server-Side Examples and Output
Climbing the CGI Learning Curve
A First Web Page
HTML basics
Internet addresses (URLs)
Using minimal URLs
HTML file permission constraints
A First CGI Script
Installing CGI scripts
Finding Python on remote servers
Adding Pictures and Generating Tables
Table tags
Adding User Interaction
Submission page
More on form tags
Response script
Passing parameters in URLs
Testing outside browsers with the module urllib
Using Tables to Lay Out Forms
Converting strings in CGI scripts
Debugging CGI scripts
Adding Common Input Devices
Changing Input Layouts
Keeping display and logic separate
Passing Parameters in Hardcoded URLs
Passing Parameters in Hidden Form Fields
Saving State Information in CGI Scripts
URL Query Parameters
Hidden Form Input Fields
HTTP “Cookies”
Creating a cookie
Receiving a cookie
Using cookies in CGI scripts
Handling cookies with the module urllib2
Server-Side Databases
Extensions to the CGI Model
Combining Techniques
The Hello World Selector
Checking for Missing and Invalid Inputs
Refactoring Code for Maintainability
Step 1: Sharing Objects Between Pages—A New Input Form
Step 2: A Reusable Form Mock-Up Utility
Step 3: Putting It All Together—A New Reply Script
More on HTML and URL Escapes
URL Escape Code Conventions
Python HTML and URL Escape Tools
Escaping HTML Code
Escaping URLs
Escaping URLs Embedded in HTML Code
HTML and URL conflicts: &
Avoiding conflicts
Transferring Files to Clients and Servers
Displaying Arbitrary Server Files on the Client
Handling private files and errors
Uploading Client Files to the Server
Handling client path formats
More Than One Way to Push Bits over the Net
The PyMailCGI Server
“Things to Do When Visiting Chicago”
The PyMailCGI Web Site
Implementation Overview
New in This Edition
Presentation Overview
Running This Chapter’s Examples
The Root Page
Configuring PyMailCGI
Sending Mail by SMTP
The Message Composition Page
The Send Mail Script
Error Pages
Common Look-and-Feel
Using the Send Mail Script Outside a Browser
Reading POP Email
The POP Password Page
The Mail Selection List Page
Passing State Information in URL Link Parameters
Security Protocols
Reading mail with direct URLs
The Message View Page
Passing State Information in HTML Hidden Input Fields
Escaping Mail Text and Passwords in HTML
Processing Fetched Mail
Reply and Forward
Delete
Deletions and POP Message Numbers
Inbox synchronization error potential
Passing header text in hidden input fields (PyMailCGI_2.1)
Server-side files for headers
Delete on load
Utility Modules
External Components and Configuration
POP Mail Interface
POP Password Encryption
Manual data encryption: rotor (defunct)
Manual data encryption: PyCrypto
Secure HTTP transmissions
Secure cookies
The secret.py module
Rolling your own encryptor
Common Utilities Module
CGI Script Trade-Offs
Other Approaches
Advanced Internet Topics
“Surfing on the Shoulders of Giants”
Zope: A Web Application Framework
Zope Overview
Zope hierarchy model
Zope scripting
Zope components
Zope Object Publishing
A Zope External Method
Calling through the Web
Calling from other objects
A Simple Zope Interactive Web Site
HTMLgen: Web Pages from Objects
A Brief HTMLgen Tutorial
Jython: Python for Java
A Quick Introduction to Jython
Why Jython?
A Simple Jython Example
Interface Automation Tricks
Writing Java Applets in Jython
Jython Trade-Offs
Picking Your Python
Grail: A Python-Based Web Browser
A Simple Grail Applet Example
XML Processing Tools
A Brief Introduction to XML Parsing
Windows Web Scripting Extensions
Active Scripting: Client-Side Embedding
Active Scripting basics
Embedding Python in HTML
Active Server Pages: Server-Side Embedding
A short ASP example
The COM Connection
A brief introduction to COM
Python COM clients
Python COM servers
The bigger COM picture: DCOM
Python Server Pages
PSP in Webware and mod_python
Rolling Your Own Servers in Python
Standard Library Socket Servers
Standard Library Web Servers
Third-Party Solutions
And Other Cool Stuff
Part V
Databases and Persistence
“Give Me an Order of Persistence, but Hold the Pickles”
Persistence Options in Python
DBM Files
Using DBM Files
Pickled Objects
Using Object Pickling
Picking in Action
Pickler Protocols and cPickle
Shelve Files
Using Shelves
Storing Built-In Object Types in Shelves
Storing Class Instances in Shelves
Changing Classes of Objects Stored in Shelves
Shelve Constraints
Keys must be strings
Objects are unique only within a key
Updates must treat shelves as fetch-modify-store mappings
Concurrent updates are not directly supported
Underlying DBM format portability
Pickled Class Constraints
Other Shelve Limitations
The ZODB Object-Oriented Database
A ZODB Tutorial
Installing ZODB
The ZEO distributed object server
Creating a ZODB database
Fetching and changing
Using Classes with ZODB
A ZODB People Database
ZODB Resources
SQL Database Interfaces
SQL Interface Overview
An SQL Database API Tutorial
The MySQL system
Installation
Getting started
Making databases and tables
Adding records
Running queries
Running updates
Building Record Dictionaries
Using table descriptions
Record dictionaries
Automating with scripts and modules
Tying the Pieces Together
Loading Database Tables from Files
Loading with SQL and Python
Python versus SQL
SQL Utility Scripts
Table load scripts
Table display script
Using the scripts
SQL Resources
PyForm: A Persistent Object Viewer
Processing Shelves with Code
Adding a Graphical Interface
PyForm GUI Implementation
PyForm Table Wrappers
PyForm Creation and View Utility Scripts
Creating and browsing custom databases
Data as Code
Browsing Other Kinds of Objects with PyForm
Browsing Other Kinds of Databases with PyForm
PyForm Limitations
Data Structures
“Roses Are Red, Violets Are Blue; Lists Are Mutable, and So Is Set Foo”
Implementing Stacks
A Stack Module
A Stack Class
Customization: Performance Monitors
Optimization: Tuple Tree Stacks
Optimization: In-Place List Modifications
Timing the Improvements
Results under Python 1.5.2
Results under Python 2.4
Implementing Sets
Set Functions
Supporting multiple operands
Set Classes
Optimization: Moving Sets to Dictionaries
Timing the results under Python 2.4
Timing results under Python 1.5.2: version skew
Using the Python profiler
Optimizing fastset by Coding Techniques (or Not)
Adding Relational Algebra to Sets (External)
Subclassing Built-In Types
Binary Search Trees
Graph Searching
Moving Graphs to Classes
Reversing Sequences
Permuting Sequences
Sorting Sequences
Adding Comparison Functions
Data Structures Versus Python Built-Ins
PyTree: A Generic Tree Object Viewer
Running PyTree
PyTree Source Code
Tree-independent GUI implementation
Tree wrappers and test widgets
PyTree Does Parse Trees Too
Text and Language
“See Jack Hack. Hack, Jack, Hack”
Strategies for Parsing Text in Python
String Method Utilities
Templating with Replacements and Formats
Parsing with Splits and Joins
Summing Columns in a File
Parsing and Unparsing Rule Strings
More on the holmes Expert System Shell
Regular Expression Pattern Matching
First Examples
Using the re Module
Module functions
Compiled pattern objects
Match objects
Regular expression patterns
Basic Patterns
Scanning C Header Files for Patterns
A File Pattern Search Utility
Advanced Language Tools
Handcoded Parsers
The Expression Grammar
The Parser’s Code
Adding a Parse Tree Interpreter
Parse Tree Structure
Exploring Parse Trees with PyTree
Parsers Versus Python
PyCalc: A Calculator Program/Object
A Simple Calculator GUI
Building the GUI
Running code strings
Extending and attaching
PyCalc—A Real Calculator GUI
Running PyCalc
Evaluating expressions with stacks
PyCalc source code
Using PyCalc as a component
Adding new buttons in new components
Part VI
Extending Python
“I Am Lost at C”
Integration Modes
Presentation Notes
C Extensions Overview
A Simple C Extension Module
Extension Module Details
Compilation and Linking
Dynamic binding
Static binding
Static versus dynamic binding
Compiling with the Distutils System
Anatomy of a C Extension Module
Data Conversions
Python to C: using Python argument lists
Python to C: using Python return values
C to Python: returning values to Python
Common conversion codes
Error Handling
Raising Python exceptions in C
Detecting errors that occur in Python
Reference Counts
Other Extension Tasks: Threads
The SWIG Integration Code Generator
A Simple SWIG Example
SWIG Details
Wrapping C Environment Calls
Adding Wrapper Classes to Flat Libraries
But Don’t Do That Either—SWIG
A C Extension Module String Stack
But Don’t Do That Either—SWIG
A C Extension Type String Stack
Anatomy of a C Extension Type
Compiling and Running
Timing the C Implementations
Older Timing Results
But Don’t Do That Either—SWIG
Wrapping C++ Classes with SWIG
A Simple C++ Extension Class
Wrapping the C++ Class with SWIG
Using the C++ Class in Python
Using the low-level extension module
Subclassing the C++ class in Python
Exploring the wrappers interactively
Other Extending Tools
Embedding Python
“Add Python. Mix Well. Repeat.”
C Embedding API Overview
What Is Embedded Code?
Basic Embedding Techniques
Running Simple Code Strings
Compiling and running
Running Code Strings with Results and Namespaces
Calling Python Objects
Running Strings in Dictionaries
Precompiling Strings to Bytecode
Registering Callback Handler Objects
Using Python Classes in C
A High-Level Embedding API: ppembed
Running Objects with ppembed
Running Code Strings with ppembed
Running Customizable Validations
Running function-based validations
Other validation components
ppembed Implementation
Other Integration Examples (External)
Other Integration Topics
Jython: Java Integration
IronPython: C#/.NET Integration
COM Integration on Windows
CORBA Integration
Other Languages
Network-Based Integration Protocols
Integration Versus Optimization
Framework roles
Extension module roles
Picking an integration technology
Part VII
Conclusion: Python and the Development Cycle
“That’s the End of the Book, Now Here’s the Meaning of Life”
“Something’s Wrong with the Way We Program Computers”
The “Gilligan Factor”
Doing the Right Thing
The Static Language Build Cycle
Artificial Complexities
One Language Does Not Fit All
Enter Python
But What About That Bottleneck?
Python Provides Immediate Turnaround
Python Is “Executable Pseudocode”
Python Is OOP Done Right
Python Fosters Hybrid Applications
On Sinking the Titanic
So What’s “Python: The Sequel”?
In the Final Analysis . . .
Postscript to the Second Edition (2000)
Integration Isn’t Everything
The End of the Java Wars
We’re Not Off That Island Yet
Postscript to the Third Edition (2006)
Proof of Concept
Integration Today
Quality Counts
Index