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