You are previewing Python Cookbook, 3rd Edition.
O'Reilly logo
Python Cookbook, 3rd Edition

Book Description

If you need help writing programs in Python 3, or want to update older Python 2 code, this book is just the ticket. Packed with practical recipes written and tested with Python 3.3, this unique cookbook is for experienced Python programmers who want to focus on modern tools and idioms.

Table of Contents

  1. Python Cookbook
  2. Preface
    1. Who This Book Is For
    2. Who This Book Is Not For
    3. Conventions Used in This Book
    4. Online Code Examples
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
      1. David Beazley’s Acknowledgments
      2. Brian Jones’ Acknowledgments
  3. 1. Data Structures and Algorithms
    1. 1.1. Unpacking a Sequence into Separate Variables
      1. Problem
      2. Solution
      3. Discussion
    2. 1.2. Unpacking Elements from Iterables of Arbitrary Length
      1. Problem
      2. Solution
      3. Discussion
    3. 1.3. Keeping the Last N Items
      1. Problem
      2. Solution
      3. Discussion
    4. 1.4. Finding the Largest or Smallest N Items
      1. Problem
      2. Solution
      3. Discussion
    5. 1.5. Implementing a Priority Queue
      1. Problem
      2. Solution
      3. Discussion
    6. 1.6. Mapping Keys to Multiple Values in a Dictionary
      1. Problem
      2. Solution
      3. Discussion
    7. 1.7. Keeping Dictionaries in Order
      1. Problem
      2. Solution
      3. Discussion
    8. 1.8. Calculating with Dictionaries
      1. Problem
      2. Solution
      3. Discussion
    9. 1.9. Finding Commonalities in Two Dictionaries
      1. Problem
      2. Solution
      3. Discussion
    10. 1.10. Removing Duplicates from a Sequence while Maintaining Order
      1. Problem
      2. Solution
      3. Discussion
    11. 1.11. Naming a Slice
      1. Problem
      2. Solution
      3. Discussion
    12. 1.12. Determining the Most Frequently Occurring Items in a Sequence
      1. Problem
      2. Solution
      3. Discussion
    13. 1.13. Sorting a List of Dictionaries by a Common Key
      1. Problem
      2. Solution
      3. Discussion
    14. 1.14. Sorting Objects Without Native Comparison Support
      1. Problem
      2. Solution
      3. Discussion
    15. 1.15. Grouping Records Together Based on a Field
      1. Problem
      2. Solution
      3. Discussion
    16. 1.16. Filtering Sequence Elements
      1. Problem
      2. Solution
      3. Discussion
    17. 1.17. Extracting a Subset of a Dictionary
      1. Problem
      2. Solution
      3. Discussion
    18. 1.18. Mapping Names to Sequence Elements
      1. Problem
      2. Solution
      3. Discussion
    19. 1.19. Transforming and Reducing Data at the Same Time
      1. Problem
      2. Solution
      3. Discussion
    20. 1.20. Combining Multiple Mappings into a Single Mapping
      1. Problem
      2. Solution
      3. Discussion
  4. 2. Strings and Text
    1. 2.1. Splitting Strings on Any of Multiple Delimiters
      1. Problem
      2. Solution
      3. Discussion
    2. 2.2. Matching Text at the Start or End of a String
      1. Problem
      2. Solution
      3. Discussion
    3. 2.3. Matching Strings Using Shell Wildcard Patterns
      1. Problem
      2. Solution
      3. Discussion
    4. 2.4. Matching and Searching for Text Patterns
      1. Problem
      2. Solution
      3. Discussion
    5. 2.5. Searching and Replacing Text
      1. Problem
      2. Solution
      3. Discussion
    6. 2.6. Searching and Replacing Case-Insensitive Text
      1. Problem
      2. Solution
      3. Discussion
    7. 2.7. Specifying a Regular Expression for the Shortest Match
      1. Problem
      2. Solution
      3. Discussion
    8. 2.8. Writing a Regular Expression for Multiline Patterns
      1. Problem
      2. Solution
      3. Discussion
    9. 2.9. Normalizing Unicode Text to a Standard Representation
      1. Problem
      2. Solution
      3. Discussion
    10. 2.10. Working with Unicode Characters in Regular Expressions
      1. Problem
      2. Solution
      3. Discussion
    11. 2.11. Stripping Unwanted Characters from Strings
      1. Problem
      2. Solution
      3. Discussion
    12. 2.12. Sanitizing and Cleaning Up Text
      1. Problem
      2. Solution
      3. Discussion
    13. 2.13. Aligning Text Strings
      1. Problem
      2. Solution
      3. Discussion
    14. 2.14. Combining and Concatenating Strings
      1. Problem
      2. Solution
      3. Discussion
    15. 2.15. Interpolating Variables in Strings
      1. Problem
      2. Solution
      3. Discussion
    16. 2.16. Reformatting Text to a Fixed Number of Columns
      1. Problem
      2. Solution
      3. Discussion
    17. 2.17. Handling HTML and XML Entities in Text
      1. Problem
      2. Solution
      3. Discussion
    18. 2.18. Tokenizing Text
      1. Problem
      2. Solution
      3. Discussion
    19. 2.19. Writing a Simple Recursive Descent Parser
      1. Problem
      2. Solution
      3. Discussion
    20. 2.20. Performing Text Operations on Byte Strings
      1. Problem
      2. Solution
      3. Discussion
  5. 3. Numbers, Dates, and Times
    1. 3.1. Rounding Numerical Values
      1. Problem
      2. Solution
      3. Discussion
    2. 3.2. Performing Accurate Decimal Calculations
      1. Problem
      2. Solution
      3. Discussion
    3. 3.3. Formatting Numbers for Output
      1. Problem
      2. Solution
      3. Discussion
    4. 3.4. Working with Binary, Octal, and Hexadecimal Integers
      1. Problem
      2. Solution
      3. Discussion
    5. 3.5. Packing and Unpacking Large Integers from Bytes
      1. Problem
      2. Solution
      3. Discussion
    6. 3.6. Performing Complex-Valued Math
      1. Problem
      2. Solution
      3. Discussion
    7. 3.7. Working with Infinity and NaNs
      1. Problem
      2. Solution
      3. Discussion
    8. 3.8. Calculating with Fractions
      1. Problem
      2. Solution
      3. Discussion
    9. 3.9. Calculating with Large Numerical Arrays
      1. Problem
      2. Solution
      3. Discussion
    10. 3.10. Performing Matrix and Linear Algebra Calculations
      1. Problem
      2. Solution
      3. Discussion
    11. 3.11. Picking Things at Random
      1. Problem
      2. Solution
      3. Discussion
    12. 3.12. Converting Days to Seconds, and Other Basic Time Conversions
      1. Problem
      2. Solution
      3. Discussion
    13. 3.13. Determining Last Friday’s Date
      1. Problem
      2. Solution
      3. Discussion
    14. 3.14. Finding the Date Range for the Current Month
      1. Problem
      2. Solution
      3. Discussion
    15. 3.15. Converting Strings into Datetimes
      1. Problem
      2. Solution
      3. Discussion
    16. 3.16. Manipulating Dates Involving Time Zones
      1. Problem
      2. Solution
      3. Discussion
  6. 4. Iterators and Generators
    1. 4.1. Manually Consuming an Iterator
      1. Problem
      2. Solution
      3. Discussion
    2. 4.2. Delegating Iteration
      1. Problem
      2. Solution
      3. Discussion
    3. 4.3. Creating New Iteration Patterns with Generators
      1. Problem
      2. Solution
      3. Discussion
    4. 4.4. Implementing the Iterator Protocol
      1. Problem
      2. Solution
      3. Discussion
    5. 4.5. Iterating in Reverse
      1. Problem
      2. Solution
      3. Discussion
    6. 4.6. Defining Generator Functions with Extra State
      1. Problem
      2. Solution
      3. Discussion
    7. 4.7. Taking a Slice of an Iterator
      1. Problem
      2. Solution
      3. Discussion
    8. 4.8. Skipping the First Part of an Iterable
      1. Problem
      2. Solution
      3. Discussion
    9. 4.9. Iterating Over All Possible Combinations or Permutations
      1. Problem
      2. Solution
      3. Discussion
    10. 4.10. Iterating Over the Index-Value Pairs of a Sequence
      1. Problem
      2. Solution
      3. Discussion
    11. 4.11. Iterating Over Multiple Sequences Simultaneously
      1. Problem
      2. Solution
      3. Discussion
    12. 4.12. Iterating on Items in Separate Containers
      1. Problem
      2. Solution
      3. Discussion
    13. 4.13. Creating Data Processing Pipelines
      1. Problem
      2. Solution
      3. Discussion
    14. 4.14. Flattening a Nested Sequence
      1. Problem
      2. Solution
      3. Discussion
    15. 4.15. Iterating in Sorted Order Over Merged Sorted Iterables
      1. Problem
      2. Solution
      3. Discussion
    16. 4.16. Replacing Infinite while Loops with an Iterator
      1. Problem
      2. Solution
      3. Discussion
  7. 5. Files and I/O
    1. 5.1. Reading and Writing Text Data
      1. Problem
      2. Solution
      3. Discussion
    2. 5.2. Printing to a File
      1. Problem
      2. Solution
      3. Discussion
    3. 5.3. Printing with a Different Separator or Line Ending
      1. Problem
      2. Solution
      3. Discussion
    4. 5.4. Reading and Writing Binary Data
      1. Problem
      2. Solution
      3. Discussion
    5. 5.5. Writing to a File That Doesn’t Already Exist
      1. Problem
      2. Solution
      3. Discussion
    6. 5.6. Performing I/O Operations on a String
      1. Problem
      2. Solution
      3. Discussion
    7. 5.7. Reading and Writing Compressed Datafiles
      1. Problem
      2. Solution
      3. Discussion
    8. 5.8. Iterating Over Fixed-Sized Records
      1. Problem
      2. Solution
      3. Discussion
    9. 5.9. Reading Binary Data into a Mutable Buffer
      1. Problem
      2. Solution
      3. Discussion
    10. 5.10. Memory Mapping Binary Files
      1. Problem
      2. Solution
      3. Discussion
    11. 5.11. Manipulating Pathnames
      1. Problem
      2. Solution
      3. Discussion
    12. 5.12. Testing for the Existence of a File
      1. Problem
      2. Solution
      3. Discussion
    13. 5.13. Getting a Directory Listing
      1. Problem
      2. Solution
      3. Discussion
    14. 5.14. Bypassing Filename Encoding
      1. Problem
      2. Solution
      3. Discussion
    15. 5.15. Printing Bad Filenames
      1. Problem
      2. Solution
      3. Discussion
    16. 5.16. Adding or Changing the Encoding of an Already Open File
      1. Problem
      2. Solution
      3. Discussion
    17. 5.17. Writing Bytes to a Text File
      1. Problem
      2. Solution
      3. Discussion
    18. 5.18. Wrapping an Existing File Descriptor As a File Object
      1. Problem
      2. Solution
      3. Discussion
    19. 5.19. Making Temporary Files and Directories
      1. Problem
      2. Solution
      3. Discussion
    20. 5.20. Communicating with Serial Ports
      1. Problem
      2. Solution
      3. Discussion
    21. 5.21. Serializing Python Objects
      1. Problem
      2. Solution
      3. Discussion
  8. 6. Data Encoding and Processing
    1. 6.1. Reading and Writing CSV Data
      1. Problem
      2. Solution
      3. Discussion
    2. 6.2. Reading and Writing JSON Data
      1. Problem
      2. Solution
      3. Discussion
    3. 6.3. Parsing Simple XML Data
      1. Problem
      2. Solution
      3. Discussion
    4. 6.4. Parsing Huge XML Files Incrementally
      1. Problem
      2. Solution
      3. Discussion
    5. 6.5. Turning a Dictionary into XML
      1. Problem
      2. Solution
      3. Discussion
    6. 6.6. Parsing, Modifying, and Rewriting XML
      1. Problem
      2. Solution
      3. Discussion
    7. 6.7. Parsing XML Documents with Namespaces
      1. Problem
      2. Solution
      3. Discussion
    8. 6.8. Interacting with a Relational Database
      1. Problem
      2. Solution
      3. Discussion
    9. 6.9. Decoding and Encoding Hexadecimal Digits
      1. Problem
      2. Solution
      3. Discussion
    10. 6.10. Decoding and Encoding Base64
      1. Problem
      2. Solution
      3. Discussion
    11. 6.11. Reading and Writing Binary Arrays of Structures
      1. Problem
      2. Solution
      3. Discussion
    12. 6.12. Reading Nested and Variable-Sized Binary Structures
      1. Problem
      2. Solution
      3. Discussion
    13. 6.13. Summarizing Data and Performing Statistics
      1. Problem
      2. Solution
      3. Discussion
  9. 7. Functions
    1. 7.1. Writing Functions That Accept Any Number of Arguments
      1. Problem
      2. Solution
      3. Discussion
    2. 7.2. Writing Functions That Only Accept Keyword Arguments
      1. Problem
      2. Solution
      3. Discussion
    3. 7.3. Attaching Informational Metadata to Function Arguments
      1. Problem
      2. Solution
      3. Discussion
    4. 7.4. Returning Multiple Values from a Function
      1. Problem
      2. Solution
      3. Discussion
    5. 7.5. Defining Functions with Default Arguments
      1. Problem
      2. Solution
      3. Discussion
    6. 7.6. Defining Anonymous or Inline Functions
      1. Problem
      2. Solution
      3. Discussion
    7. 7.7. Capturing Variables in Anonymous Functions
      1. Problem
      2. Solution
      3. Discussion
    8. 7.8. Making an N-Argument Callable Work As a Callable with Fewer Arguments
      1. Problem
      2. Solution
      3. Discussion
    9. 7.9. Replacing Single Method Classes with Functions
      1. Problem
      2. Solution
      3. Discussion
    10. 7.10. Carrying Extra State with Callback Functions
      1. Problem
      2. Solution
      3. Discussion
    11. 7.11. Inlining Callback Functions
      1. Problem
      2. Solution
      3. Discussion
    12. 7.12. Accessing Variables Defined Inside a Closure
      1. Problem
      2. Solution
      3. Discussion
  10. 8. Classes and Objects
    1. 8.1. Changing the String Representation of Instances
      1. Problem
      2. Solution
      3. Discussion
    2. 8.2. Customizing String Formatting
      1. Problem
      2. Solution
      3. Discussion
    3. 8.3. Making Objects Support the Context-Management Protocol
      1. Problem
      2. Solution
      3. Discussion
    4. 8.4. Saving Memory When Creating a Large Number of Instances
      1. Problem
      2. Solution
      3. Discussion
    5. 8.5. Encapsulating Names in a Class
      1. Problem
      2. Solution
      3. Discussion
    6. 8.6. Creating Managed Attributes
      1. Problem
      2. Solution
      3. Discussion
    7. 8.7. Calling a Method on a Parent Class
      1. Problem
      2. Solution
      3. Discussion
    8. 8.8. Extending a Property in a Subclass
      1. Problem
      2. Solution
      3. Discussion
    9. 8.9. Creating a New Kind of Class or Instance Attribute
      1. Problem
      2. Solution
      3. Discussion
    10. 8.10. Using Lazily Computed Properties
      1. Problem
      2. Solution
      3. Discussion
    11. 8.11. Simplifying the Initialization of Data Structures
      1. Problem
      2. Solution
      3. Discussion
    12. 8.12. Defining an Interface or Abstract Base Class
      1. Problem
      2. Solution
      3. Discussion
    13. 8.13. Implementing a Data Model or Type System
      1. Problem
      2. Solution
      3. Discussion
    14. 8.14. Implementing Custom Containers
      1. Problem
      2. Solution
      3. Discussion
    15. 8.15. Delegating Attribute Access
      1. Problem
      2. Solution
      3. Discussion
    16. 8.16. Defining More Than One Constructor in a Class
      1. Problem
      2. Solution
      3. Discussion
    17. 8.17. Creating an Instance Without Invoking init
      1. Problem
      2. Solution
      3. Discussion
    18. 8.18. Extending Classes with Mixins
      1. Problem
      2. Solution
      3. Discussion
    19. 8.19. Implementing Stateful Objects or State Machines
      1. Problem
      2. Solution
      3. Discussion
    20. 8.20. Calling a Method on an Object Given the Name As a String
      1. Problem
      2. Solution
      3. Discussion
    21. 8.21. Implementing the Visitor Pattern
      1. Problem
      2. Solution
      3. Discussion
    22. 8.22. Implementing the Visitor Pattern Without Recursion
      1. Problem
      2. Solution
      3. Discussion
    23. 8.23. Managing Memory in Cyclic Data Structures
      1. Problem
      2. Solution
      3. Discussion
    24. 8.24. Making Classes Support Comparison Operations
      1. Problem
      2. Solution
      3. Discussion
    25. 8.25. Creating Cached Instances
      1. Problem
      2. Solution
      3. Discussion
  11. 9. Metaprogramming
    1. 9.1. Putting a Wrapper Around a Function
      1. Problem
      2. Solution
      3. Discussion
    2. 9.2. Preserving Function Metadata When Writing Decorators
      1. Problem
      2. Solution
      3. Discussion
    3. 9.3. Unwrapping a Decorator
      1. Problem
      2. Solution
      3. Discussion
    4. 9.4. Defining a Decorator That Takes Arguments
      1. Problem
      2. Solution
      3. Discussion
    5. 9.5. Defining a Decorator with User Adjustable Attributes
      1. Problem
      2. Solution
      3. Discussion
    6. 9.6. Defining a Decorator That Takes an Optional Argument
      1. Problem
      2. Solution
      3. Discussion
    7. 9.7. Enforcing Type Checking on a Function Using a Decorator
      1. Problem
      2. Solution
      3. Discussion
    8. 9.8. Defining Decorators As Part of a Class
      1. Problem
      2. Solution
      3. Discussion
    9. 9.9. Defining Decorators As Classes
      1. Problem
      2. Solution
      3. Discussion
    10. 9.10. Applying Decorators to Class and Static Methods
      1. Problem
      2. Solution
      3. Discussion
    11. 9.11. Writing Decorators That Add Arguments to Wrapped Functions
      1. Problem
      2. Solution
      3. Discussion
    12. 9.12. Using Decorators to Patch Class Definitions
      1. Problem
      2. Solution
      3. Discussion
    13. 9.13. Using a Metaclass to Control Instance Creation
      1. Problem
      2. Solution
      3. Discussion
    14. 9.14. Capturing Class Attribute Definition Order
      1. Problem
      2. Solution
      3. Discussion
    15. 9.15. Defining a Metaclass That Takes Optional Arguments
      1. Problem
      2. Solution
      3. Discussion
    16. 9.16. Enforcing an Argument Signature on *args and **kwargs
      1. Problem
      2. Solution
      3. Discussion
    17. 9.17. Enforcing Coding Conventions in Classes
      1. Problem
      2. Solution
      3. Discussion
    18. 9.18. Defining Classes Programmatically
      1. Problem
      2. Solution
      3. Discussion
    19. 9.19. Initializing Class Members at Definition Time
      1. Problem
      2. Solution
      3. Discussion
    20. 9.20. Implementing Multiple Dispatch with Function Annotations
      1. Problem
      2. Solution
      3. Discussion
    21. 9.21. Avoiding Repetitive Property Methods
      1. Problem
      2. Solution
      3. Discussion
    22. 9.22. Defining Context Managers the Easy Way
      1. Problem
      2. Solution
      3. Discussion
    23. 9.23. Executing Code with Local Side Effects
      1. Problem
      2. Solution
      3. Discussion
    24. 9.24. Parsing and Analyzing Python Source
      1. Problem
      2. Solution
      3. Discussion
    25. 9.25. Disassembling Python Byte Code
      1. Problem
      2. Solution
      3. Discussion
  12. 10. Modules and Packages
    1. 10.1. Making a Hierarchical Package of Modules
      1. Problem
      2. Solution
      3. Discussion
    2. 10.2. Controlling the Import of Everything
      1. Problem
      2. Solution
      3. Discussion
    3. 10.3. Importing Package Submodules Using Relative Names
      1. Problem
      2. Solution
      3. Discussion
    4. 10.4. Splitting a Module into Multiple Files
      1. Problem
      2. Solution
      3. Discussion
    5. 10.5. Making Separate Directories of Code Import Under a Common Namespace
      1. Problem
      2. Solution
      3. Discussion
    6. 10.6. Reloading Modules
      1. Problem
      2. Solution
      3. Discussion
    7. 10.7. Making a Directory or Zip File Runnable As a Main Script
      1. Problem
      2. Solution
      3. Discussion
    8. 10.8. Reading Datafiles Within a Package
      1. Problem
      2. Solution
      3. Discussion
    9. 10.9. Adding Directories to sys.path
      1. Problem
      2. Solution
      3. Discussion
    10. 10.10. Importing Modules Using a Name Given in a String
      1. Problem
      2. Solution
      3. Discussion
    11. 10.11. Loading Modules from a Remote Machine Using Import Hooks
      1. Problem
      2. Solution
      3. Discussion
    12. 10.12. Patching Modules on Import
      1. Problem
      2. Solution
      3. Discussion
    13. 10.13. Installing Packages Just for Yourself
      1. Problem
      2. Solution
      3. Discussion
    14. 10.14. Creating a New Python Environment
      1. Problem
      2. Solution
      3. Discussion
    15. 10.15. Distributing Packages
      1. Problem
      2. Solution
      3. Discussion
  13. 11. Network and Web Programming
    1. 11.1. Interacting with HTTP Services As a Client
      1. Problem
      2. Solution
      3. Discussion
    2. 11.2. Creating a TCP Server
      1. Problem
      2. Solution
      3. Discussion
    3. 11.3. Creating a UDP Server
      1. Problem
      2. Solution
      3. Discussion
    4. 11.4. Generating a Range of IP Addresses from a CIDR Address
      1. Problem
      2. Solution
      3. Discussion
    5. 11.5. Creating a Simple REST-Based Interface
      1. Problem
      2. Solution
      3. Discussion
    6. 11.6. Implementing a Simple Remote Procedure Call with XML-RPC
      1. Problem
      2. Solution
      3. Discussion
    7. 11.7. Communicating Simply Between Interpreters
      1. Problem
      2. Solution
      3. Discussion
    8. 11.8. Implementing Remote Procedure Calls
      1. Problem
      2. Solution
      3. Discussion
    9. 11.9. Authenticating Clients Simply
      1. Problem
      2. Solution
      3. Discussion
    10. 11.10. Adding SSL to Network Services
      1. Problem
      2. Solution
      3. Discussion
    11. 11.11. Passing a Socket File Descriptor Between Processes
      1. Problem
      2. Solution
      3. Discussion
    12. 11.12. Understanding Event-Driven I/O
      1. Problem
      2. Solution
      3. Discussion
    13. 11.13. Sending and Receiving Large Arrays
      1. Problem
      2. Solution
      3. Discussion
  14. 12. Concurrency
    1. 12.1. Starting and Stopping Threads
      1. Problem
      2. Solution
      3. Discussion
    2. 12.2. Determining If a Thread Has Started
      1. Problem
      2. Solution
      3. Discussion
    3. 12.3. Communicating Between Threads
      1. Problem
      2. Solution
      3. Discussion
    4. 12.4. Locking Critical Sections
      1. Problem
      2. Solution
      3. Discussion
    5. 12.5. Locking with Deadlock Avoidance
      1. Problem
      2. Solution
      3. Discussion
    6. 12.6. Storing Thread-Specific State
      1. Problem
      2. Solution
      3. Discussion
    7. 12.7. Creating a Thread Pool
      1. Problem
      2. Solution
      3. Discussion
    8. 12.8. Performing Simple Parallel Programming
      1. Problem
      2. Solution
      3. Discussion
    9. 12.9. Dealing with the GIL (and How to Stop Worrying About It)
      1. Problem
      2. Solution
      3. Discussion
    10. 12.10. Defining an Actor Task
      1. Problem
      2. Solution
      3. Discussion
    11. 12.11. Implementing Publish/Subscribe Messaging
      1. Problem
      2. Solution
      3. Discussion
    12. 12.12. Using Generators As an Alternative to Threads
      1. Problem
      2. Solution
      3. Discussion
    13. 12.13. Polling Multiple Thread Queues
      1. Problem
      2. Solution
      3. Discussion
    14. 12.14. Launching a Daemon Process on Unix
      1. Problem
      2. Solution
      3. Discussion
  15. 13. Utility Scripting and System Administration
    1. 13.1. Accepting Script Input via Redirection, Pipes, or Input Files
      1. Problem
      2. Solution
      3. Discussion
    2. 13.2. Terminating a Program with an Error Message
      1. Problem
      2. Solution
      3. Discussion
    3. 13.3. Parsing Command-Line Options
      1. Problem
      2. Solution
      3. Discussion
    4. 13.4. Prompting for a Password at Runtime
      1. Problem
      2. Solution
      3. Discussion
    5. 13.5. Getting the Terminal Size
      1. Problem
      2. Solution
      3. Discussion
    6. 13.6. Executing an External Command and Getting Its Output
      1. Problem
      2. Solution
      3. Discussion
    7. 13.7. Copying or Moving Files and Directories
      1. Problem
      2. Solution
      3. Discussion
    8. 13.8. Creating and Unpacking Archives
      1. Problem
      2. Solution
      3. Discussion
    9. 13.9. Finding Files by Name
      1. Problem
      2. Solution
      3. Discussion
    10. 13.10. Reading Configuration Files
      1. Problem
      2. Solution
      3. Discussion
    11. 13.11. Adding Logging to Simple Scripts
      1. Problem
      2. Solution
      3. Discussion
    12. 13.12. Adding Logging to Libraries
      1. Problem
      2. Solution
      3. Discussion
    13. 13.13. Making a Stopwatch Timer
      1. Problem
      2. Solution
      3. Discussion
    14. 13.14. Putting Limits on Memory and CPU Usage
      1. Problem
      2. Solution
      3. Discussion
    15. 13.15. Launching a Web Browser
      1. Problem
      2. Solution
      3. Discussion
  16. 14. Testing, Debugging, and Exceptions
    1. 14.1. Testing Output Sent to stdout
      1. Problem
      2. Solution
      3. Discussion
    2. 14.2. Patching Objects in Unit Tests
      1. Problem
      2. Solution
      3. Discussion
    3. 14.3. Testing for Exceptional Conditions in Unit Tests
      1. Problem
      2. Solution
      3. Discussion
    4. 14.4. Logging Test Output to a File
      1. Problem
      2. Solution
      3. Discussion
    5. 14.5. Skipping or Anticipating Test Failures
      1. Problem
      2. Solution
      3. Discussion
    6. 14.6. Handling Multiple Exceptions
      1. Problem
      2. Solution
      3. Discussion
    7. 14.7. Catching All Exceptions
      1. Problem
      2. Solution
      3. Discussion
    8. 14.8. Creating Custom Exceptions
      1. Problem
      2. Solution
      3. Discussion
    9. 14.9. Raising an Exception in Response to Another Exception
      1. Problem
      2. Solution
      3. Discussion
    10. 14.10. Reraising the Last Exception
      1. Problem
      2. Solution
      3. Discussion
    11. 14.11. Issuing Warning Messages
      1. Problem
      2. Solution
      3. Discussion
    12. 14.12. Debugging Basic Program Crashes
      1. Problem
      2. Solution
      3. Discussion
    13. 14.13. Profiling and Timing Your Program
      1. Problem
      2. Solution
      3. Discussion
    14. 14.14. Making Your Programs Run Faster
      1. Problem
      2. Solution
        1. Use functions
        2. Selectively eliminate attribute access
        3. Understand locality of variables
        4. Avoid gratuitous abstraction
        5. Use the built-in containers
        6. Avoid making unnecessary data structures or copies
      3. Discussion
  17. 15. C Extensions
    1. 15.1. Accessing C Code Using ctypes
      1. Problem
      2. Solution
      3. Discussion
    2. 15.2. Writing a Simple C Extension Module
      1. Problem
      2. Solution
      3. Discussion
    3. 15.3. Writing an Extension Function That Operates on Arrays
      1. Problem
      2. Solution
      3. Discussion
    4. 15.4. Managing Opaque Pointers in C Extension Modules
      1. Problem
      2. Solution
      3. Discussion
    5. 15.5. Defining and Exporting C APIs from Extension Modules
      1. Problem
      2. Solution
      3. Discussion
    6. 15.6. Calling Python from C
      1. Problem
      2. Solution
      3. Discussion
    7. 15.7. Releasing the GIL in C Extensions
      1. Problem
      2. Solution
      3. Discussion
    8. 15.8. Mixing Threads from C and Python
      1. Problem
      2. Solution
      3. Discussion
    9. 15.9. Wrapping C Code with Swig
      1. Problem
      2. Solution
      3. Discussion
    10. 15.10. Wrapping Existing C Code with Cython
      1. Problem
      2. Solution
      3. Discussion
    11. 15.11. Using Cython to Write High-Performance Array Operations
      1. Problem
      2. Solution
      3. Discussion
    12. 15.12. Turning a Function Pointer into a Callable
      1. Problem
      2. Solution
      3. Discussion
    13. 15.13. Passing NULL-Terminated Strings to C Libraries
      1. Problem
      2. Solution
      3. Discussion
    14. 15.14. Passing Unicode Strings to C Libraries
      1. Problem
      2. Solution
      3. Discussion
    15. 15.15. Converting C Strings to Python
      1. Problem
      2. Solution
      3. Discussion
    16. 15.16. Working with C Strings of Dubious Encoding
      1. Problem
      2. Solution
      3. Discussion
    17. 15.17. Passing Filenames to C Extensions
      1. Problem
      2. Solution
      3. Discussion
    18. 15.18. Passing Open Files to C Extensions
      1. Problem
      2. Solution
      3. Discussion
    19. 15.19. Reading File-Like Objects from C
      1. Problem
      2. Solution
      3. Discussion
    20. 15.20. Consuming an Iterable from C
      1. Problem
      2. Solution
      3. Discussion
    21. 15.21. Diagnosing Segmentation Faults
      1. Problem
      2. Solution
      3. Discussion
  18. A. Further Reading
    1. Online Resources
    2. Books for Learning Python
    3. Advanced Books
  19. Index
  20. About the Authors
  21. Colophon
  22. Copyright