Python Cookbook

Book description

The Python Cookbook is a collection of problems, solutions, and practical examples for Python programmers, written by Python programmers. Over the past year, members of the Python community have contributed material to an online repository of Python recipes hosted by ActiveState. This book contains the best of those recipes, accompanied by overviews and background material by key Python figures. The recipes in the Python Cookbook range from simple tasks, such as working with dictionaries and list comprehensions, to entire modules that demonstrate templating systems and network monitoring. This book contains over 200 recipes on the following topics:

  • Searching and sorting

  • Manipulating text

  • Working with files and the filesystem

  • Object-oriented programming

  • Dealing with threads and processes

  • System administration

  • Interacting with databases

  • Creating user interfaces

  • Network and web programming

  • Processing XML

  • Distributed programming

  • Debugging and testing

  • Extending Python

This book is a treasure trove of useful code for all Python programmers, from novices to advanced practitioners, with contributions from such Python luminaries as Guido Van Rossum, David Ascher, Tim Peters, Paul Prescod, Mark Hammond, and Alex Martelli, as well as over 100 other Python programmers. The recipes highlight Python best practices and can be used directly in day-to-day programming tasks, as a source of ideas, or as a way to learn more about Python. The recipes in the Python Cookbook were edited by David Ascher, who is on the board of the Python Software Foundation and is the co-author of Learning Python, and Alex Martelli, who is known for his numerous and exhaustive postings on the Python mailing list. The book contains a foreword by Guido van Rossum, the creator of Python.

Table of contents

  1. Python Cookbook
    1. Foreword
    2. Preface
      1. The Design of the Book
      2. The Implementation of the Book
      3. A Note About Licenses
      4. Audience
      5. Organization
      6. Further Reading
      7. Conventions Used in This Book
      8. How to Contact Us
      9. Acknowledgments
        1. David Ascher
        2. Alex Martelli
    3. 1. Python Shortcuts
      1. Introduction
      2. Swapping Values WithoutUsing a Temporary Variable
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Constructing a Dictionary Without Excessive Quoting
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Getting a Value from a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Adding an Entry to a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Associating Multiple Values with Each Key in a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Dispatching Using a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Collecting a Bunch of Named Items
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Finding the Intersection of Two Dictionaries
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Assigning and Testing with One Statement
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Using List Comprehensions Instead of map and filter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Unzipping Simple List-Like Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Flattening a Nested Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Looping in Parallel over Index and Sequence Items
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Looping Through Multiple Lists
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Spanning a Range Defined by Floats
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Transposing Two-Dimensional Arrays
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Creating Lists of Lists Without Sharing References
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    4. 2. Searching and Sorting
      1. Introduction
        1. Searching and Sorting FAQ
      2. Sorting a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Processing Selected Pairs of Structured Data Efficiently
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Sorting While Guaranteeing Sort Stability
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Sorting by One Field, Then by Another
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Looking for Items in a Sorted Sequence Using Binary Search
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Sorting a List of Objects by an Attribute of the Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Sorting by Item or by Attribute
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Selecting Random Elements from a List Without Repetition
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Performing Frequent Membership Tests on a Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Finding the Deep Index of an Item in an Embedded Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Showing Off Quicksort in Three Lines
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Sorting Objects Using SQL’s ORDER BY Syntax
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    5. 3. Text
      1. Introduction
        1. What Is Text?
        2. Basic Textual Operations
        3. Sources of Text
        4. String Basics
      2. Processing a String One Character at a Time
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Testing if an Object Is String-Like
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Aligning Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Trimming Space from the Ends of a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Combining Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Checking Whether a String Contains a Set of Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Filtering a String for a Set of Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Controlling Case
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Reversing a String by Words or Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Accessing Substrings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Changing the Indentation of a Multiline String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Testing Whether a String Represents an Integer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Expanding and Compressing Tabs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Replacing Multiple Patterns in a Single Pass
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Converting Between Different Naming Conventions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Converting Between Characters and Values
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Converting Between Unicode and Plain Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Printing Unicode Characters to Standard Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Dispatching Based on Pattern Matches
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Evaluating Code Inside Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Replacing Python Code with the Results of Executing That Code
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. Module: Yet Another Python Templating Utility (YAPTU)
        1. See Also
      24. Module: Roman Numerals
        1. See Also
    6. 4. Files
      1. Introduction
        1. File Basics
        2. Portability and Flexibility
      2. Reading from a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Writing to a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Searching and Replacing Text in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Reading a Particular Line from a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Retrieving a Line at Random from a File of Unknown Size
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Counting Lines in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Processing Every Word in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Reading a Text File by Paragraphs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Reading Lines with Continuation Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Reading Data from ZIP Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Reading INI Configuration Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Sending Binary Data to Standard Output Under Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Using Random-Access Input/Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Updating a Random-Access File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Splitting a Path into All of Its Parts
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Treating Pathnames as Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Creating Directories Including Necessary Parent Directories
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Walking Directory Trees
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Swapping One File Extension for Another Throughout a Directory Tree
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Finding a File Given an Arbitrary Search Path
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Finding a File on the Python Search Path
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. Dynamically Changing the Python Search Path
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. Computing Directory Sizes in a Cross-Platform Way
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      25. File Locking Using a Cross-Platform API
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      26. Versioning Filenames
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      27. Module: Versioned Backups
        1. See Also
    7. 5. Object-Oriented Programming
      1. Introduction
      2. Overriding a Built-In Method
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Getting All Members of a Class Hierarchy
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Calling a Superclass _ _init_ _ Method if It Exists
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Calling a Superclass Implementation of a Method
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Implementing Properties
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Implementing Static Methods
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Implementing Class Methods
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Delegating Automatically as an Alternative to Inheritance
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Decorating an Object with Print-Like Methods
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Checking if an Object Has Necessary Attributes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Making a Fast Copy of an Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Adding Methods to a Class at Runtime
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Modifying the Class Hierarchy of an Instance
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Keeping References to Bound Methods Without Inhibiting Garbage Collection
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Defining Constants
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Managing Options
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Implementing a Set Class
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Implementing a Ring Buffer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Implementing a Collection
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Delegating Messages to Multiple Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Implementing the Singleton Design Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. Avoiding the Singleton Design Pattern with the Borg Idiom
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. Implementing the Null Object Design Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    8. 6. Threads, Processes, and Synchronization
      1. Introduction
      2. Storing Per-Thread Information
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Terminating a Thread
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Allowing Multithreaded Read Access While Maintaining a Write Lock
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Running Functions in the Future
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Synchronizing All Methods in an Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Capturing the Output and Error Streams from a Unix Shell Command
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Forking a Daemon Process on Unix
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Determining if Another Instance of a Script Is Already Running in Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Processing Windows Messages Using MsgWaitForMultipleObjects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    9. 7. System Administration
      1. Introduction
      2. Running a Command Repeatedly
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Generating Random Passwords
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Generating Non-Totally Random Passwords
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Checking the Status of a Unix Network Interface
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Calculating Apache Hits per IP Address
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Calculating the Rate of Client Cache Hits on Apache
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Manipulating the Environment on Windows NT/2000/XP
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Checking and Modifying the Set of Tasks Windows Automatically Runs at Logon
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Examining the Microsoft Windows Registry for a List of Name Server Addresses
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Getting Information About the Current User on Windows NT/2000
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Getting the Windows Service Name from Its Long Name
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Manipulating Windows Services
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Impersonating Principals on Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Changing a Windows NT Password Using ADSI
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Working with Windows Scripting Host (WSH) from Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Displaying Decoded Hotkeys for Shortcuts in Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    10. 8. Databases and Persistence
      1. Introduction
      2. Serializing Data Using the marshal Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Serializing Data Using the pickle and cPickle Modules
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Using the cPickle Module on Classes and Instances
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Mutating Objects with shelve
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Accessing a MySQL Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Storing a BLOB in a MySQL Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Storing a BLOB in a PostgreSQL Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Generating a Dictionary Mapping from Field Names to Column Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Using dtuple for Flexible Access to Query Results
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Pretty-Printing the Contents of Database Cursors
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Establishing Database Connections Lazily
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Accessing a JDBC Database from a Jython Servlet
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Module: jet2sql—Creating a SQL DDL from an Access Database
    11. 9. User Interfaces
      1. Introduction
      2. Avoiding lambda in Writing Callback Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Creating Menus with Tkinter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Creating Dialog Boxes with Tkinter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Supporting Multiple Values per Row in a Tkinter Listbox
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Embedding Inline GIFs Using Tkinter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Combining Tkinter and Asynchronous I/O with Threads
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Using a wxPython Notebook with Panels
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Giving the User Unobtrusive Feedback During Data Entry with Qt
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Building GUI Solutions Independent of the Specific GUI Toolkit
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Creating Color Scales
        1. Problem
        2. Solution
        3. Discussion
      12. Using Publish/Subscribe Broadcasting to Loosen the Coupling Between GUI and Business Logic Systems
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Module: Building GTK GUIs Interactively
        1. See Also
    12. 10. Network Programming
      1. Introduction
      2. Writing a TCP Client
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Writing a TCP Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Passing Messages with Socket Datagrams
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Finding Your Own Name and Address
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Converting IP Addresses
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Grabbing a Document from the Web
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Being an FTP Client
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Sending HTML Mail
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Sending Multipart MIME Email
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Bundling Files in a MIME Message
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Unpacking a Multipart MIME Message
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Module: PyHeartBeat—Detecting Inactive Computers
        1. See Also
      14. Module: Interactive POP3 Mailbox Inspector
        1. See Also
      15. Module: Watching for New IMAP Mail Using a GUI
        1. See Also
    13. 11. Web Programming
      1. Introduction
      2. Testing Whether CGI Is Working
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Writing a CGI Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Using a Simple Dictionary for CGI Parameters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Handling URLs Within a CGI Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Resuming the HTTP Download of a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Stripping Dangerous Tags and Javascript from HTML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Running a Servlet with Jython
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Accessing Netscape Cookie Information
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Finding an Internet Explorer Cookie
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Module: Fetching Latitude/Longitude Data from the Web
        1. See Also
    14. 12. Processing XML
      1. Introduction
      2. Checking XML Well-Formedness
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Counting Tags in a Document
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Extracting Text from an XML Document
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Transforming an XML Document Using XSLT
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Transforming an XML Document Using Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Parsing an XML File with xml.parsers.expat
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Converting Ad-Hoc Text into XML Markup
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Normalizing an XML Document
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Controlling XSLT Stylesheet Loading
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Autodetecting XML Encoding
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Module: XML Lexing (Shallow Parsing)
        1. See Also
      13. Module: Converting a List of Equal-Length Lists into XML
        1. See Also
    15. 13. Distributed Programming
      1. Introduction
      2. Making an XML-RPC Method Call
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Serving XML-RPC Requests
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Using XML-RPC with Medusa
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Writing a Web Service That Supports Both XML-RPC and SOAP
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Implementing a CORBA Client and Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Performing Remote Logins Using telnetlib
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Using Publish/Subscribe in a Distributed Middleware Architecture
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Using Request/Reply in a Distributed Middleware Architecture
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    16. 14. Debugging and Testing
      1. Introduction
      2. Reloading All Loaded Modules
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Tracing Expressions and Comments in Debug Mode
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Wrapping Tracebacks in HTML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Getting More Information from Tracebacks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Starting the Debugger Automatically After an Uncaught Exception
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Logging and Tracing Across Platforms
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Determining the Name of the Current Function
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Introspecting the Call Stack with Older Versions of Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Debugging the Garbage-Collection Process
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Tracking Instances of Particular Classes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    17. 15. Programs About Programs
      1. Introduction
        1. Lexing
        2. Parsing
        3. PLY and SPARK
        4. Using Python Itself as a Little Language
        5. Introspection
      2. Colorizing Python Source Using the Built-in Tokenizer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Importing a Dynamically Generated Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Importing from a Module Whose Name Is Determined at Runtime
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Importing Modules with Automatic End-of-Line Conversions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Simulating Enumerations in Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Modifying Methods in Place
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Associating Parameters with a Function (Currying)
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Composing Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Adding Functionality to a Class
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Adding a Method to a Class Instance at Runtime
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Defining a Custom Metaclass to Control Class Behavior
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Module: Allowing the Python Profiler to Profile C Modules
        1. See Also
    18. 16. Extending and Embedding
      1. Introduction
      2. Implementing a Simple Extension Type
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Translating a Python Sequence into a C Array with the PySequence_Fast Protocol
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Accessing a Python Sequence Item-by-Item with the Iterator Protocol
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Returning None from a Python-Callable C Function
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Coding the Methods of a Python Class in C
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Implementing C Function Callbacks to a Python Function
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Debugging Dynamically Loaded C Extensions with gdb
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Debugging Memory Problems
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Using SWIG-Generated Modules in a Multithreaded Environment
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    19. 17. Algorithms
      1. Introduction
      2. Testing if a Variable Is Defined
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Evaluating Predicate Tests Across Sequences
        1. Problem
        2. Solution
        3. Discussion
      4. Removing Duplicates from a Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Removing Duplicates from a Sequence While Maintaining Sequence Order
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Simulating the Ternary Operator in Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Counting Items and Sorting by Incidence (Histograms)
        1. Problem
        2. Solution
        3. Discussion
      8. Memoizing (Caching) the Return Values of Functions
        1. Problem
        2. Solution
        3. Discussion
      9. Looking Up Words by Sound Similarity
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Computing Factorials with lambda
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Generating the Fibonacci Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Wrapping an Unbounded Iterator to Restrict Its Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Operating on Iterators
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Rolling Dice
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Implementing a First-In First-Out Container
        1. Problem
        2. Solution
        3. Discussion
      16. Modeling a Priority Queue
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Converting Numbers to Rationals via Farey Fractions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Evaluating a Polynomial
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Module: Finding the Convex Hull of a Set of 2D Points
        1. See Also
      20. Module: Parsing a String into a Date/Time Object Portably
        1. See Also
    20. 18. List of Contributors
      1. A
      2. B
      3. C
      4. D
      5. F
      6. G
      7. H
      8. J
      9. K
      10. L
      11. M
      12. N
      13. P
      14. Q
      15. R
      16. S
      17. T
      18. U
      19. V
      20. W
      21. Y
      22. Z
    21. Index
    22. Colophon

Product information

  • Title: Python Cookbook
  • Author(s):
  • Release date: July 2002
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596001674