I l@ve RuBoard Previous Section Next Section

•  Table of Contents
•  Index
•  Reviews
•  Examples
•  Reader Reviews
•  Errata
Python Cookbook
By Alex Martelli, David Ascher
Publisher : O'Reilly
Pub Date : July 2002
ISBN : 0-596-00167-3
Pages : 606

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