• Main Page
  • Table of content
  • Copyright
  • Foreword
  • Preface
    • The Design of the Book
    • The Implementation of the Book
    • A Note About Licenses
    • Audience
    • Organization
    • Further Reading
    • Conventions Used in This Book
    • How to Contact Us
    • Acknowledgments
  • Chapter 1. Python Shortcuts
    • 1.1 Introduction
    • 1.2 Swapping Values WithoutUsing a Temporary Variable
    • 1.3 Constructing a Dictionary Without Excessive Quoting
    • 1.4 Getting a Value from a Dictionary
    • 1.5 Adding an Entry to a Dictionary
    • 1.6 Associating Multiple Values with Each Key in a Dictionary
    • 1.7 Dispatching Using a Dictionary
    • 1.8 Collecting a Bunch of Named Items
    • 1.9 Finding the Intersection of Two Dictionaries
    • 1.10 Assigning and Testing with One Statement
    • 1.11 Using List Comprehensions Instead of map and filter
    • 1.12 Unzipping Simple List-Like Objects
    • 1.13 Flattening a Nested Sequence
    • 1.14 Looping in Parallel over Index and Sequence Items
    • 1.15 Looping Through Multiple Lists
    • 1.16 Spanning a Range Defined by Floats
    • 1.17 Transposing Two-Dimensional Arrays
    • 1.18 Creating Lists of Lists Without Sharing References
  • Chapter 2. Searching and Sorting
    • 2.1 Introduction
    • 2.2 Sorting a Dictionary
    • 2.3 Processing Selected Pairs of Structured Data Efficiently
    • 2.4 Sorting While Guaranteeing Sort Stability
    • 2.5 Sorting by One Field, Then by Another
    • 2.6 Looking for Items in a Sorted Sequence Using Binary Search
    • 2.7 Sorting a List of Objects by an Attribute of the Objects
    • 2.8 Sorting by Item or by Attribute
    • 2.9 Selecting Random Elements from a List Without Repetition
    • 2.10 Performing Frequent Membership Tests on a Sequence
    • 2.11 Finding the Deep Index of an Item in an Embedded Sequence
    • 2.12 Showing Off Quicksort in Three Lines
    • 2.13 Sorting Objects Using SQL's ORDER BY Syntax
  • Chapter 3. Text
    • 3.1 Introduction
    • 3.2 Processing a String One Character at a Time
    • 3.3 Testing if an Object Is String-Like
    • 3.4 Aligning Strings
    • 3.5 Trimming Space from the Ends of a String
    • 3.6 Combining Strings
    • 3.7 Checking Whether a String Contains a Set of Characters
    • 3.8 Filtering a String for a Set of Characters
    • 3.9 Controlling Case
    • 3.10 Reversing a String by Words or Characters
    • 3.11 Accessing Substrings
    • 3.12 Changing the Indentation of a Multiline String
    • 3.13 Testing Whether a String Represents an Integer
    • 3.14 Expanding and Compressing Tabs
    • 3.15 Replacing Multiple Patterns in a Single Pass
    • 3.16 Converting Between Different Naming Conventions
    • 3.17 Converting Between Characters and Values
    • 3.18 Converting Between Unicode and Plain Strings
    • 3.19 Printing Unicode Characters to Standard Output
    • 3.20 Dispatching Based on Pattern Matches
    • 3.21 Evaluating Code Inside Strings
    • 3.22 Replacing Python Code with the Results of Executing That Code
    • 3.23 Module: Yet Another Python Templating Utility (YAPTU)
    • 3.24 Module: Roman Numerals
  • Chapter 4. Files
    • 4.1 Introduction
    • 4.2 Reading from a File
    • 4.3 Writing to a File
    • 4.4 Searching and Replacing Text in a File
    • 4.5 Reading a Particular Line from a File
    • 4.6 Retrieving a Line at Random from a File of Unknown Size
    • 4.7 Counting Lines in a File
    • 4.8 Processing Every Word in a File
    • 4.9 Reading a Text File by Paragraphs
    • 4.10 Reading Lines with Continuation Characters
    • 4.11 Reading Data from ZIP Files
    • 4.12 Reading INI Configuration Files
    • 4.13 Sending Binary Data to Standard Output Under Windows
    • 4.14 Using Random-Access Input/Output
    • 4.15 Updating a Random-Access File
    • 4.16 Splitting a Path into All of Its Parts
    • 4.17 Treating Pathnames as Objects
    • 4.18 Creating Directories Including Necessary Parent Directories
    • 4.19 Walking Directory Trees
    • 4.20 Swapping One File Extension for Another Throughout a Directory Tree
    • 4.21 Finding a File Given an Arbitrary Search Path
    • 4.22 Finding a File on the Python Search Path
    • 4.23 Dynamically Changing the Python Search Path
    • 4.24 Computing Directory Sizes in a Cross-Platform Way
    • 4.25 File Locking Using a Cross-Platform API
    • 4.26 Versioning Filenames
    • 4.27 Module: Versioned Backups
  • Chapter 5. Object-Oriented Programming
    • 5.1 Introduction
    • 5.2 Overriding a Built-In Method
    • 5.3 Getting All Members of a Class Hierarchy
    • 5.4 Calling a Superclass _ _init_ _ Method if It Exists
    • 5.5 Calling a Superclass Implementation of a Method
    • 5.6 Implementing Properties
    • 5.7 Implementing Static Methods
    • 5.8 Implementing Class Methods
    • 5.9 Delegating Automatically as an Alternative to Inheritance
    • 5.10 Decorating an Object with Print-Like Methods
    • 5.11 Checking if an Object Has Necessary Attributes
    • 5.12 Making a Fast Copy of an Object
    • 5.13 Adding Methods to a Class at Runtime
    • 5.14 Modifying the Class Hierarchy of an Instance
    • 5.15 Keeping References to Bound Methods Without Inhibiting Garbage Collection
    • 5.16 Defining Constants
    • 5.17 Managing Options
    • 5.18 Implementing a Set Class
    • 5.19 Implementing a Ring Buffer
    • 5.20 Implementing a Collection
    • 5.21 Delegating Messages to Multiple Objects
    • 5.22 Implementing the Singleton Design Pattern
    • 5.23 Avoiding the Singleton Design Pattern with the Borg Idiom
    • 5.24 Implementing the Null Object Design Pattern
  • Chapter 6. Threads, Processes, and Synchronization
    • 6.1 Introduction
    • 6.2 Storing Per-Thread Information
    • 6.3 Terminating a Thread
    • 6.4 Allowing Multithreaded Read Access While Maintaining a Write Lock
    • 6.5 Running Functions in the Future
    • 6.6 Synchronizing All Methods in an Object
    • 6.7 Capturing the Output and Error Streams from a Unix Shell Command
    • 6.8 Forking a Daemon Process on Unix
    • 6.9 Determining if Another Instance of a Script Is Already Running in Windows
    • 6.10 Processing Windows Messages Using MsgWaitForMultipleObjects
  • Chapter 7. System Administration
    • 7.1 Introduction
    • 7.2 Running a Command Repeatedly
    • 7.3 Generating Random Passwords
    • 7.4 Generating Non-Totally Random Passwords
    • 7.5 Checking the Status of a Unix Network Interface
    • 7.6 Calculating Apache Hits per IP Address
    • 7.7 Calculating the Rate of Client Cache Hits on Apache
    • 7.8 Manipulating the Environment on Windows NT/2000/XP
    • 7.9 Checking and Modifying the Set of Tasks Windows Automatically Runs at Logon
    • 7.10 Examining the Microsoft Windows Registry for a List of Name Server Addresses
    • 7.11 Getting Information About the Current User on Windows NT/2000
    • 7.12 Getting the Windows Service Name from Its Long Name
    • 7.13 Manipulating Windows Services
    • 7.14 Impersonating Principals on Windows
    • 7.15 Changing a Windows NT Password Using ADSI
    • 7.16 Working with Windows Scripting Host (WSH) from Python
    • 7.17 Displaying Decoded Hotkeys for Shortcuts in Windows
  • Chapter 8. Databases and Persistence
    • 8.1 Introduction
    • 8.2 Serializing Data Using the marshal Module
    • 8.3 Serializing Data Using the pickle and cPickle Modules
    • 8.4 Using the cPickle Module on Classes and Instances
    • 8.5 Mutating Objects with shelve
    • 8.6 Accesssing a MySQL Database
    • 8.7 Storing a BLOB in a MySQL Database
    • 8.8 Storing a BLOB in a PostgreSQL Database
    • 8.9 Generating a Dictionary Mapping from Field Names to Column Numbers
    • 8.10 Using dtuple for Flexible Access to Query Results
    • 8.11 Pretty-Printing the Contents of Database Cursors
    • 8.12 Establishing Database Connections Lazily
    • 8.13 Accessing a JDBC Database from a Jython Servlet
    • 8.14 Module: jet2sql桟reating a SQL DDL from an Access Database
  • Chapter 9. User Interfaces
    • 9.1 Introduction
    • 9.2 Avoiding lambda in Writing Callback Functions
    • 9.3 Creating Menus with Tkinter
    • 9.4 Creating Dialog Boxes with Tkinter
    • 9.5 Supporting Multiple Values per Row in a Tkinter Listbox
    • 9.6 Embedding Inline GIFs Using Tkinter
    • 9.7 Combining Tkinter and Asynchronous I/O with Threads
    • 9.8 Using a wxPython Notebook with Panels
    • 9.9 Giving the User Unobtrusive Feedback During Data Entry with Qt
    • 9.10 Building GUI Solutions Independent of the Specific GUI Toolkit
    • 9.11 Creating Color Scales
    • 9.12 Using Publish/Subscribe Broadcasting to Loosen the Coupling Between GUI and Business Logic Systems
    • 9.13 Module: Building GTK GUIs Interactively
  • Chapter 10. Network Programming
    • 10.1 Introduction
    • 10.2 Writing a TCP Client
    • 10.3 Writing a TCP Server
    • 10.4 Passing Messages with Socket Datagrams
    • 10.5 Finding Your Own Name and Address
    • 10.6 Converting IP Addresses
    • 10.7 Grabbing a Document from the Web
    • 10.8 Being an FTP Client
    • 10.9 Sending HTML Mail
    • 10.10 Sending Multipart MIME Email
    • 10.11 Bundling Files in a MIME Message
    • 10.12 Unpacking a Multipart MIME Message
    • 10.13 Module: PyHeartBeat桪etecting Inactive Computers
    • 10.14 Module: Interactive POP3 Mailbox Inspector
    • 10.15 Module: Watching for New IMAP Mail Using a GUI
  • Chapter 11. Web Programming
    • 11.1 Introduction
    • 11.2 Testing Whether CGI Is Working
    • 11.3 Writing a CGI Script
    • 11.4 Using a Simple Dictionary for CGI Parameters
    • 11.5 Handling URLs Within a CGI Script
    • 11.6 Resuming the HTTP Download of a File
    • 11.7 Stripping Dangerous Tags and Javascript from HTML
    • 11.8 Running a Servlet with Jython
    • 11.9 Accessing Netscape Cookie Information
    • 11.10 Finding an Internet Explorer Cookie
    • 11.11 Module: Fetching Latitude/Longitude Data from the Web
  • Chapter 12. Processing XML
    • 12.1 Introduction
    • 12.2 Checking XML Well-Formedness
    • 12.3 Counting Tags in a Document
    • 12.4 Extracting Text from an XML Document
    • 12.5 Transforming an XML Document Using XSLT
    • 12.6 Transforming an XML Document Using Python
    • 12.7 Parsing an XML File with xml.parsers.expat
    • 12.8 Converting Ad-Hoc Text into XML Markup
    • 12.9 Normalizing an XML Document
    • 12.10 Controlling XSLT Stylesheet Loading
    • 12.11 Autodetecting XML Encoding
    • 12.12 Module: XML Lexing (Shallow Parsing)
    • 12.13 Module: Converting a List of Equal-Length Lists into XML
  • Chapter 13. Distributed Programming
    • 13.1 Introduction
    • 13.2 Making an XML-RPC Method Call
    • 13.3 Serving XML-RPC Requests
    • 13.4 Using XML-RPC with Medusa
    • 13.5 Writing a Web Service That Supports Both XML-RPC and SOAP
    • 13.6 Implementing a CORBA Client and Server
    • 13.7 Performing Remote Logins Using telnetlib
    • 13.8 Using Publish/Subscribe in a Distributed Middleware Architecture
    • 13.9 Using Request/Reply in a Distributed Middleware Architecture
  • Chapter 14. Debugging and Testing
    • 14.1 Introduction
    • 14.2 Reloading All Loaded Modules
    • 14.3 Tracing Expressions and Comments in Debug Mode
    • 14.4 Wrapping Tracebacks in HTML
    • 14.5 Getting More Information from Tracebacks
    • 14.6 Starting the Debugger Automatically After an Uncaught Exception
    • 14.7 Logging and Tracing Across Platforms
    • 14.8 Determining the Name of the Current Function
    • 14.9 Introspecting the Call Stack with Older Versions of Python
    • 14.10 Debugging the Garbage-Collection Process
    • 14.11 Tracking Instances of Particular Classes
  • Chapter 15. Programs About Programs
    • 15.1 Introduction
    • 15.2 Colorizing Python Source Using the Built-in Tokenizer
    • 15.3 Importing a Dynamically Generated Module
    • 15.4 Importing from a Module Whose Name Is Determined at Runtime
    • 15.5 Importing Modules with Automatic End-of-Line Conversions
    • 15.6 Simulating Enumerations in Python
    • 15.7 Modifying Methods in Place
    • 15.8 Associating Parameters with a Function (Currying)
    • 15.9 Composing Functions
    • 15.10 Adding Functionality to a Class
    • 15.11 Adding a Method to a Class Instance at Runtime
    • 15.12 Defining a Custom Metaclass to Control Class Behavior
    • 15.13 Module: Allowing the Python Profiler to Profile C Modules
  • Chapter 16. Extending and Embedding
    • 16.1 Introduction
    • 16.2 Implementing a Simple Extension Type
    • 16.3 Translating a Python Sequence into a C Array with the PySequence_Fast Protocol
    • 16.4 Accessing a Python Sequence Item-by-Item with the Iterator Protocol
    • 16.5 Returning None from a Python-Callable C Function
    • 16.6 Coding the Methods of a Python Class in C
    • 16.7 Implementing C Function Callbacks to a Python Function
    • 16.8 Debugging Dynamically Loaded C Extensions with gdb
    • 16.9 Debugging Memory Problems
    • 16.10 Using SWIG-Generated Modules in a Multithreaded Environment
  • Chapter 17. Algorithms
    • 17.1 Introduction
    • 17.2 Testing if a Variable Is Defined
    • 17.3 Evaluating Predicate Tests Across Sequences
    • 17.4 Removing Duplicates from a Sequence
    • 17.5 Removing Duplicates from a Sequence While Maintaining Sequence Order
    • 17.6 Simulating the Ternary Operator in Python
    • 17.7 Counting Items and Sorting by Incidence (Histograms)
    • 17.8 Memoizing (Caching) the Return Values of Functions
    • 17.9 Looking Up Words by Sound Similarity
    • 17.10 Computing Factorials with lambda
    • 17.11 Generating the Fibonacci Sequence
    • 17.12 Wrapping an Unbounded Iterator to Restrict Its Output
    • 17.13 Operating on Iterators
    • 17.14 Rolling Dice
    • 17.15 Implementing a First-In First-Out Container
    • 17.16 Modeling a Priority Queue
    • 17.17 Converting Numbers to Rationals via Farey Fractions
    • 17.18 Evaluating a Polynomial
    • 17.19 Module: Finding the Convex Hull of a Set of 2D Points
    • 17.20 Module: Parsing a String into a Date/Time Object Portably
  • Chapter 18. List of Contributors
    • 18.1 A
    • 18.2 B
    • 18.3 C
    • 18.4 D
    • 18.5 F
    • 18.6 G
    • 18.7 H
    • 18.8 J
    • 18.9 K
    • 18.10 L
    • 18.11 M
    • 18.12 N
    • 18.13 P
    • 18.14 Q
    • 18.15 R
    • 18.16 S
    • 18.17 T
    • 18.18 U
    • 18.19 V
    • 18.20 W
    • 18.21 Y
    • 18.22 Z
  • Colophon
  • Index
    • Index SYMBOL
    • Index A
    • Index B
    • Index C
    • Index D
    • Index E
    • Index F
    • Index G
    • Index H
    • Index I
    • Index J
    • Index K
    • Index L
    • Index M
    • Index N
    • Index O
    • Index P
    • Index Q
    • Index R
    • Index S
    • Index T
    • Index U
    • Index V
    • Index W
    • Index X
    • Index Y
    • Index Z