Cover image for Python Cookbook, 2nd Edition

Book description

Portable, powerful, and a breeze to use, Python is the popular open source object-oriented programming language used for both standalone programs and scripting applications. It is now being used by an increasing number of major organizations, including NASA and Google. Updated for Python 2.4, The Python Cookbook, 2nd Edition offers a wealth of useful code for all Python programmers, not just advanced practitioners. Like its predecessor, the new edition provides solutions to problems that Python programmers face everyday. It now includes over 200 recipes that range from simple tasks, such as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and building a templating system. This revised version also includes new chapters on topics such as time, money, and metaprogramming. Here's a list of additional topics covered:

  • Manipulating text

  • Searching and sorting

  • 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

Another advantage of The Python Cookbook, 2nd Edition is its trio of authors--three well-known Python programming experts, who are highly visible on email lists and in newsgroups, and speak often at Python conferences. With scores of practical examples and pertinent background information, The Python Cookbook, 2nd Edition is the one source you need if you're looking to build efficient, flexible, scalable, and well-integrated systems.

Table of Contents

  1. Python Cookbook, 2nd Edition
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. The Design of the Book
      2. The Implementation of the Book
      3. Using the Code from This Book
      4. Audience
      5. Organization
      6. Further Reading
      7. Conventions Used in This Book
      8. How to Contact Us
      9. Safari® Enabled
      10. Acknowledgments
        1. David Ascher
        2. Alex Martelli
        3. Anna Martelli Ravenscroft
    4. 1. Text
      1. Introduction
        1. What Is Text?
        2. Basic Textual Operations
        3. Sources of Text
        4. String Basics
      2. 1.1. Processing a String One Character at a Time
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 1.2. Converting Between Characters and Numeric Codes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 1.3. Testing Whether an Object Is String-like
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 1.4. Aligning Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 1.5. Trimming Space from the Ends of a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 1.6. Combining Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 1.7. Reversing a String by Words or Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 1.8. Checking Whether a String Contains a Set of Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 1.9. Simplifying Usage of Strings' translate Method
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 1.10. Filtering a String for a Set of Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 1.11. Checking Whether a String Is Text or Binary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 1.12. Controlling Case
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 1.13. Accessing Substrings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 1.14. Changing the Indentation of a Multiline String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 1.15. Expanding and Compressing Tabs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 1.16. Interpolating Variables in a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 1.17. Interpolating Variables in a Stringin Python 2.4
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 1.18. Replacing Multiple Patterns in a Single Pass
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 1.19. Checking a String for Any of Multiple Endings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. 1.20. Handling International Text with Unicode
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. 1.21. Converting Between Unicode and Plain Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. 1.22. Printing Unicode Charactersto Standard Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. 1.23. Encoding Unicode Data for XML and HTML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      25. 1.24. Making Some Strings Case-Insensitive
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      26. 1.25. Converting HTML Documents to Texton a Unix Terminal
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    5. 2. Files
      1. Introduction
        1. File Basics
        2. Portability and Flexibility
      2. 2.1. Reading from a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 2.2. Writing to a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 2.3. Searching and Replacing Text in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 2.4. Reading a Specific Line from a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 2.5. Counting Lines in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 2.6. Processing Every Word in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 2.7. Using Random-Access Input/Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 2.8. Updating a Random-Access File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 2.9. Reading Data from zip Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 2.10. Handling a zip File Inside a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 2.11. Archiving a Tree of Files into a Compressed tar File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 2.12. Sending Binary Data to Standard Output Under Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 2.13. Using a C++-like iostream Syntax
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 2.14. Rewinding an Input File to the Beginning
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 2.15. Adapting a File-like Object to a True File Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 2.16. Walking Directory Trees
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 2.17. Swapping One File Extension for Another Throughout a Directory Tree
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 2.18. Finding a File Given a Search Path
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 2.19. Finding Files Given a Search Path and a Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. 2.20. Finding a File on the Python Search Path
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. 2.21. Dynamically Changing the PythonSearch Path
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. 2.22. Computing the Relative Path from One Directory to Another
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. 2.23. Reading an Unbuffered Character in a Cross-Platform Way
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      25. 2.24. Counting Pages of PDF Documents on Mac OS X
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      26. 2.25. Changing File Attributes on Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      27. 2.26. Extracting Text from OpenOffice.org Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      28. 2.27. Extracting Text from Microsoft Word Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      29. 2.28. File Locking Using a Cross-Platform API
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      30. 2.29. Versioning Filenames
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      31. 2.30. Calculating CRC-64 Cyclic Redundancy Checks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    6. 3. Time and Money
      1. Introduction
        1. The time Module
        2. Time and Date Objects
        3. Decimal
      2. 3.1. Calculating Yesterday and Tomorrow
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 3.2. Finding Last Friday
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 3.3. Calculating Time Periods in a Date Range
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 3.4. Summing Durations of Songs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 3.5. Calculating the Number of Weekdays Between Two Dates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 3.6. Looking up Holidays Automatically
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 3.7. Fuzzy Parsing of Dates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 3.8. Checking Whether Daylight Saving Time Is Currently in Effect
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 3.9. Converting Time Zones
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 3.10. Running a Command Repeatedly
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 3.11. Scheduling Commands
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 3.12. Doing Decimal Arithmetic
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 3.13. Formatting Decimals as Currency
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 3.14. Using Python as a Simple Adding Machine
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 3.15. Checking a Credit Card Checksum
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 3.16. Watching Foreign Exchange Rates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    7. 4. Python Shortcuts
      1. Introduction
      2. 4.1. Copying an Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 4.2. Constructing Lists with List Comprehensions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 4.3. Returning an Element of a List If It Exists
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 4.4. Looping over Items and Their Indices in a Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 4.5. Creating Lists of Lists Without Sharing References
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 4.6. Flattening a Nested Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 4.7. Removing or Reordering Columnsin a List of Rows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 4.8. Transposing Two-Dimensional Arrays
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 4.9. Getting a Value from a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 4.10. Adding an Entry to a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 4.11. Building a Dictionary Without Excessive Quoting
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 4.12. Building a Dict from a List of Alternating Keys and Values
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 4.13. Extracting a Subset of a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 4.14. Inverting a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 4.15. Associating Multiple Values with Each Key in a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 4.16. Using a Dictionary to Dispatch Methods or Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 4.17. Finding Unions and Intersections of Dictionaries
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 4.18. Collecting a Bunch of Named Items
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 4.19. Assigning and Testing with One Statement
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. 4.20. Using printf in Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. 4.21. Randomly Picking Items with Given Probabilities
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. 4.22. Handling Exceptions Within an Expression
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. 4.23. Ensuring a Name Is Defined in a Given Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    8. 5. Searching and Sorting
      1. Introduction
        1. A Short History of Python Sorting
        2. Current Sorting
      2. 5.1. Sorting a Dictionary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 5.2. Sorting a List of Strings Case-Insensitively
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 5.3. Sorting a List of Objects by an Attribute of the Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 5.4. Sorting Keys or Indices Basedon the Corresponding Values
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 5.5. Sorting Strings with Embedded Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 5.6. Processing All of a List's Items in Random Order
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 5.7. Keeping a Sequence Ordered as Items Are Added
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 5.8. Getting the First Few Smallest Items of a Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 5.9. Looking for Items in a Sorted Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 5.10. Selecting the nth Smallest Element of a Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 5.11. Showing off quicksort in Three Lines
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 5.12. Performing Frequent Membership Tests on a Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 5.13. Finding Subsequences
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 5.14. Enriching the Dictionary Type with Ratings Functionality
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 5.15. Sorting Names and Separating Them by Initials
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    9. 6. Object-Oriented Programming
      1. Introduction
      2. 6.1. Converting Among Temperature Scales
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 6.2. Defining Constants
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 6.3. Restricting Attribute Setting
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 6.4. Chaining Dictionary Lookups
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 6.5. Delegating Automatically as an Alternative to Inheritance
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 6.6. Delegating Special Methods in Proxies
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 6.7. Implementing Tuples with Named Items
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 6.8. Avoiding Boilerplate Accessors for Properties
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 6.9. Making a Fast Copy of an Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 6.10. Keeping References to Bound Methods Without Inhibiting Garbage Collection
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 6.11. Implementing a Ring Buffer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 6.12. Checking an Instance for Any State Changes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 6.13. Checking Whether an Object Has Necessary Attributes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 6.14. Implementing the State Design Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 6.15. Implementing the "Singleton" Design Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 6.16. Avoiding the "Singleton" Design Pattern with the Borg Idiom
        1. Problem
        2. Solution
        3. Discussion
          1. Borg in action
          2. Borg, Singleton, or neither?
          3. Borg odds and ends
        4. See Also
      18. 6.17. Implementing the Null Object Design Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 6.18. Automatically Initializing Instance Variables from _ _init_ _ Arguments
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 6.19. Calling a Superclass _ _init_ _ Method If It Exists
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. 6.20. Using Cooperative Supercalls Concisely and Safely
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    10. 7. Persistence and Databases
      1. Introduction
      2. 7.1. Serializing Data Using the marshal Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 7.2. Serializing Data Using the pickle and cPickle Modules
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 7.3. Using Compression with Pickling
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 7.4. Using the cPickle Module on Classes and Instances
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 7.5. Holding Bound Methods in a Picklable Way
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 7.6. Pickling Code Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 7.7. Mutating Objects with shelve
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 7.8. Using the Berkeley DB Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 7.9. Accessing a MySQL Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 7.10. Storing a BLOB in a MySQL Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 7.11. Storing a BLOB in a PostgreSQL Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 7.12. Storing a BLOB in a SQLite Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 7.13. Generating a Dictionary Mapping Field Names to Column Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 7.14. Using dtuple for Flexible Accessto Query Results
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 7.15. Pretty-Printing the Contents of Database Cursors
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 7.16. Using a Single Parameter-Passing Style Across Various DB API Modules
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 7.17. Using Microsoft Jet via ADO
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 7.18. Accessing a JDBC Database from a Jython Servlet
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 7.19. Using ODBC to Get Excel Data with Jython
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    11. 8. Debugging and Testing
      1. Introduction
      2. 8.1. Disabling Execution of Some Conditionals and Loops
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 8.2. Measuring Memory Usage on Linux
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 8.3. Debugging the Garbage-Collection Process
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 8.4. Trapping and Recording Exceptions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 8.5. Tracing Expressions and Comments in Debug Mode
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 8.6. Getting More Information from Tracebacks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 8.7. Starting the Debugger Automatically After an Uncaught Exception
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 8.8. Running Unit Tests Most Simply
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 8.9. Running Unit Tests Automatically
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 8.10. Using doctest with unittest in Python 2.4
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 8.11. Checking Values Against Intervals in Unit Testing
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    12. 9. Processes, Threads, and Synchronization
      1. Introduction
      2. 9.1. Synchronizing All Methods in an Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 9.2. Terminating a Thread
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 9.3. Using a Queue.Queue as a Priority Queue
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 9.4. Working with a Thread Pool
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 9.5. Executing a Function in Parallel on Multiple Argument Sets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 9.6. Coordinating Threads by Simple Message Passing
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 9.7. Storing Per-Thread Information
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 9.8. Multitasking Cooperatively Without Threads
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 9.9. Determining Whether Another Instanceof a Script Is Already Running in Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 9.10. Processing Windows Messages Using MsgWaitForMultipleObjects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 9.11. Driving an External Process with popen
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 9.12. Capturing the Output and Error Streams from a Unix Shell Command
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 9.13. Forking a Daemon Process on Unix
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    13. 10. System Administration
      1. Introduction
      2. 10.1. Generating Random Passwords
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 10.2. Generating Easily Remembered Somewhat-Random Passwords
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 10.3. Authenticating Users by Means of a POP Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 10.4. Calculating Apache Hits per IP Address
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 10.5. Calculating the Rate of Client Cache Hits on Apache
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 10.6. Spawning an Editor from a Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 10.7. Backing Up Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 10.8. Selectively Copying a Mailbox File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 10.9. Building a Whitelist of Email Addresses From a Mailbox
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 10.10. Blocking Duplicate Mails
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 10.11. Checking Your Windows Sound System
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 10.12. Registering or Unregistering a DLL on Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 10.13. Checking and Modifying the Set of Tasks Windows Automatically Runs at Login
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 10.14. Creating a Share on Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 10.15. Connecting to an Already Running Instance of Internet Explorer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 10.16. Reading Microsoft Outlook Contacts
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 10.17. Gathering Detailed System Informationon Mac OS X
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    14. 11. User Interfaces
      1. Introduction
      2. 11.1. Showing a Progress Indicator on a Text Console
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 11.2. Avoiding lambda in Writing Callback Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 11.3. Using Default Values and Bounds with tkSimpleDialog Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 11.4. Adding Drag and Drop Reordering to a Tkinter Listbox
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 11.5. Entering Accented Characters in Tkinter Widgets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 11.6. Embedding Inline GIFs Using Tkinter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 11.7. Converting Among Image Formats
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 11.8. Implementing a Stopwatch in Tkinter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 11.9. Combining GUIs and Asynchronous I/Owith Threads
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 11.10. Using IDLE's Tree Widget in Tkinter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 11.11. Supporting Multiple Values per Row in a Tkinter Listbox
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 11.12. Copying Geometry Methods and Options Between Tkinter Widgets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 11.13. Implementing a Tabbed Notebook for Tkinter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 11.14. Using a wxPython Notebook with Panels
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 11.15. Implementing an ImageJ Plug-in in Jython
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 11.16. Viewing an Image from a URL with Swing and Jython
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 11.17. Getting User Input on Mac OS
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 11.18. Building a Python Cocoa GUI Programmatically
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 11.19. Implementing Fade-in Windows with IronPython
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    15. 12. Processing XML
      1. Introduction
      2. 12.1. Checking XML Well-Formedness
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 12.2. Counting Tags in a Document
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 12.3. Extracting Text from an XML Document
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 12.4. Autodetecting XML Encoding
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 12.5. Converting an XML Document into a Tree of Python Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 12.6. Removing Whitespace-only Text Nodes from an XML DOM Node's Subtree
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 12.7. Parsing Microsoft Excel's XML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 12.8. Validating XML Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 12.9. Filtering Elements and Attributes Belonging to a Given Namespace
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 12.10. Merging Continuous Text Events with a SAX Filter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 12.11. Using MSHTML to Parse XML or HTML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    16. 13. Network Programming
      1. Introduction
      2. 13.1. Passing Messages with Socket Datagrams
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 13.2. Grabbing a Document from the Web
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 13.3. Filtering a List of FTP Sites
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 13.4. Getting Time from a Server via the SNTP Protocol
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 13.5. Sending HTML Mail
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 13.6. Bundling Files in a MIME Message
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 13.7. Unpacking a Multipart MIME Message
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 13.8. Removing Attachments from an Email Message
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 13.9. Fixing Messages Parsed by Python 2.4 email.FeedParser
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 13.10. Inspecting a POP3 Mailbox Interactively
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 13.11. Detecting Inactive Computers
        1. Problem
        2. Solution
        3. Discussion
          1. Threaded server
          2. Asynchronous server
        4. See Also
      13. 13.12. Monitoring a Network with HTTP
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 13.13. Forwarding and Redirecting Network Ports
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 13.14. Tunneling SSL Through a Proxy
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 13.15. Implementing the Dynamic IP Protocol
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 13.16. Connecting to IRC and Logging Messages to Disk
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 13.17. Accessing LDAP Servers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    17. 14. Web Programming
      1. Introduction
      2. 14.1. Testing Whether CGI Is Working
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 14.2. Handling URLs Within a CGI Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 14.3. Uploading Files with CGI
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 14.4. Checking for a Web Page's Existence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 14.5. Checking Content Type via HTTP
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 14.6. Resuming the HTTP Download of a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 14.7. Handling Cookies While Fetching Web Pages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 14.8. Authenticating with a Proxy for HTTPS Navigation
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 14.9. Running a Servlet with Jython
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 14.10. Finding an Internet Explorer Cookie
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 14.11. Generating OPML Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 14.12. Aggregating RSS Feeds
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 14.13. Turning Data into Web Pages Through Templates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 14.14. Rendering Arbitrary Objects with Nevow
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    18. 15. Distributed Programming
      1. Introduction
      2. 15.1. Making an XML-RPC Method Call
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 15.2. Serving XML-RPC Requests
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 15.3. Using XML-RPC with Medusa
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 15.4. Enabling an XML-RPC Server to Be Terminated Remotely
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 15.5. Implementing SimpleXMLRPCServer Niceties
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 15.6. Giving an XML-RPC Server a wxPython GUI
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 15.7. Using Twisted Perspective Broker
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 15.8. Implementing a CORBA Server and Client
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 15.9. Performing Remote Logins Using telnetlib
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 15.10. Performing Remote Logins with SSH
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 15.11. Authenticating an SSL Client over HTTPS
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    19. 16. Programs About Programs
      1. Introduction
        1. Lexing
        2. Parsing
        3. PLY, SPARK, and Other Python Parser Generators
        4. Using Python Itself as a Little Language
        5. Introspection
      2. 16.1. Verifying Whether a String Represents a Valid Number
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 16.2. Importing a Dynamically Generated Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 16.3. Importing from a Module Whose Name Is Determined at Runtime
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 16.4. Associating Parameters with a Function (Currying)
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 16.5. Composing Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 16.6. Colorizing Python Source Using the Built-in Tokenizer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 16.7. Merging and Splitting Tokens
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 16.8. Checking Whether a String Has Balanced Parentheses
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 16.9. Simulating Enumerations in Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 16.10. Referring to a List Comprehension While Building It
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 16.11. Automating the py2exe Compilation of Scripts into Windows Executables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 16.12. Binding Main Script and Modules into One Executable on Unix
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    20. 17. Extending and Embedding
      1. Introduction
      2. 17.1. Implementing a Simple Extension Type
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 17.2. Implementing a Simple Extension Type with Pyrex
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 17.3. Exposing a C++ Library to Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 17.4. Calling Functions from a Windows DLL
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 17.5. Using SWIG-Generated Modules in a Multithreaded Environment
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 17.6. Translating a Python Sequence into a C Array with the PySequence_Fast Protocol
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 17.7. Accessing a Python Sequence Item-by-Item with the Iterator Protocol
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 17.8. Returning None from a Python-Callable C Function
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 17.9. Debugging Dynamically Loaded C Extensions with gdb
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 17.10. Debugging Memory Problems
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    21. 18. Algorithms
      1. Introduction
        1. Timing and timeit.py
      2. 18.1. Removing Duplicates from a Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 18.2. Removing Duplicates from a Sequence While Maintaining Sequence Order
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 18.3. Generating Random Samples with Replacement
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 18.4. Generating Random Samples Without Replacement
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 18.5. Memoizing (Caching) the Return Values of Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 18.6. Implementing a FIFO Container
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 18.7. Caching Objects with a FIFO Pruning Strategy
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 18.8. Implementing a Bag (Multiset) Collection Type
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 18.9. Simulating the Ternary Operator in Python
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 18.10. Computing Prime Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 18.11. Formatting Integers as Binary Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 18.12. Formatting Integers as Strings in Arbitrary Bases
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 18.13. Converting Numbers to Rationals via Farey Fractions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 18.14. Doing Arithmetic with Error Propagation
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 18.15. Summing Numbers with Maximal Accuracy
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 18.16. Simulating Floating Point
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 18.17. Computing the Convex Hulls and Diameters of 2D Point Sets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    22. 19. Iterators and Generators
      1. Introduction
        1. The Iterator Protocol
        2. Iterators and Generators
        3. Thinking Out of the Box
        4. Odds and Ends
      2. 19.1. Writing a range-like Function with Float Increments
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 19.2. Building a List from Any Iterable
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 19.3. Generating the Fibonacci Sequence
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 19.4. Unpacking a Few Items in a Multiple Assignment
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 19.5. Automatically Unpacking the Needed Number of Items
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 19.6. Dividing an Iterable into n Slices of Stride n
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 19.7. Looping on a Sequence by Overlapping Windows
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 19.8. Looping Through Multiple Iterables in Parallel
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 19.9. Looping Through the Cross-Product of Multiple Iterables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 19.10. Reading a Text File by Paragraphs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 19.11. Reading Lines with Continuation Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 19.12. Iterating on a Stream of Data Blocks as a Stream of Lines
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 19.13. Fetching Large Record Sets from a Database with a Generator
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 19.14. Merging Sorted Sequences
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 19.15. Generating Permutations, Combinations, and Selections
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 19.16. Generating the Partitions of an Integer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 19.17. Duplicating an Iterator
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 19.18. Looking Ahead into an Iterator
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. 19.19. Simplifying Queue-Consumer Threads
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. 19.20. Running an Iterator in Another Thread
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. 19.21. Computing a Summary Report with itertools.groupby
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    23. 20. Descriptors, Decorators,and Metaclasses
      1. Introduction
        1. Descriptors
        2. Decorators
        3. Metaclasses
      2. 20.1. Getting Fresh Default Values at Each Function Call
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 20.2. Coding Properties as Nested Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 20.3. Aliasing Attribute Values
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 20.4. Caching Attribute Values
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 20.5. Using One Method as Accessorfor Multiple Attributes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 20.6. Adding Functionality to a Class by Wrapping a Method
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 20.7. Adding Functionality to a Class by Enriching All Methods
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 20.8. Adding a Method to a Class Instance at Runtime
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 20.9. Checking Whether Interfaces Are Implemented
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 20.10. Using _ _new_ _ and _ _init_ _ Appropriately in Custom Metaclasses
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. 20.11. Allowing Chaining of Mutating List Methods
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. 20.12. Using Cooperative Super calls with Terser Syntax
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 20.13. Initializing Instance Attributes Without Using _ _init_ _
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 20.14. Automatic Initialization of Instance Attributes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. 20.15. Upgrading Class Instances Automatically on reload
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. 20.16. Binding Constants at Compile Time
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. 20.17. Solving Metaclass Conflicts
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    24. Index
    25. About the Authors
    26. Colophon
    27. SPECIAL OFFER: Upgrade this ebook with O’Reilly