Alex Martelli, David Ascber - Python Cookbook.pdf

(1593 KB) Pobierz
Chapter 3 -20.doc
1770444.001.png
Table of Contents
Foreword
Preface
1. Python Shortcuts
1.1 Swapping Values Without Using a Temporary Variable
1.2 Constructing a Dictionary Without Excessive Quoting
1.3 Getting a Value from a Dictionary
1.4 Adding an Entry to a Dictionary
1.5 Associating Multiple Values with Each Key in a Dictionary
1.6 Dispatching Using a Dictionary
1.7 Collecting a Bunch of Named Items
1.8 Finding the Intersection of Two Dictionaries
1.9 Assigning and Testing with One Statement
1.10 Using List Comprehensions Instead of map and filter
1.11 Unzipping Simple List-Like Objects
1.12 Flattening a Nested Sequence
1.13 Looping in Parallel over Index and Sequence Items
1.14 Looping Through Multiple Lists
1.15 Spanning a Range Defined by Floats
1.16 Transposing Two-Dimensional Arrays
1.17 Creating Lists of Lists Without Sharing References
2. Searching and Sorting
2.1 Sorting a Dictionary
2.2 Processing Selected Pairs of Structured Data Efficiently
2.3 Sorting While Guaranteeing Sort Stability
2.4 Sorting by One Field, Then by Another
2.5 Looking for Items in a Sorted Sequence Using Binary Search
2.6 Sorting a List of Objects by an Attribute of the Objects
2.7 Sorting by Item or by Attribute
2.8 Selecting Random Elements from a List Without Repetition
2.9 Performing Frequent Membership Tests on a Sequence
2.10 Finding the Deep Index of an Item in an Embedded Sequence
2.11 Showing Off Quicksort in Three Lines
2.12 Sorting Objects Using SQL's ORDER BY Syntax
3. Text
3.1 Processing a String One Character at a Time
3.2 Testing if an Object Is String-Like
3.3 Aligning Strings
3.4 Trimming Space from the Ends of a String
3.5 Combining Strings
3.6 Checking Whether a String Contains a Set of Characters
3.7 Filtering a String for a Set of Characters
3.8 Controlling Case
3.9 Reversing a String by Words or Characters
3.10 Accessing Substrings
3.11 Changing the Indentation of a Multiline String
3.12 Testing Whether a String Represents an Integer
3.13 Expanding and Compressing Tabs
3.14 Replacing Multiple Patterns in a Single Pass
3.15 Converting Between Different Naming Conventions
3.16 Converting Between Characters and Values
3.17 Converting Between Unicode and Plain Strings
3.18 Printing Unicode Characters to Standard Output
3.19 Dispatching Based on Pattern Matches
3.20 Evaluating Code Inside Strings
3.21 Replacing Python Code with the Results of Executing That
Code
3.22 Module: Yet Another Python Templating Utility (YAPTU)
3.23 Module: Roman Numerals
4. Files
4.1 Reading from a File
4.2 Writing to a File
4.3 Searching and Replacing Text in a File
4.4 Reading a Particular Line from a File
4.5 Retrieving a Line at Random from a File of Unknown Size
4.6 Counting Lines in a File
4.7 Processing Every Word in a File
4.8 Reading a Text File by Paragraphs
4.9 Reading Lines with Continuation Characters
4.10 Reading Data from ZIP Files
4.11 Reading INI Configuration Files
4.12 Sending Binary Data to Standard Output Under Windows
4.13 Using Random-Access Input/Output
4.14 Updating a Random-Access File
4.15 Splitting a Path into All of Its Parts
4.16 Treating Pathnames as Objects
4.17 Creating Directories Including Necessary Parent Directories
4.18 Walking Directory Trees
4.19 Swapping One File Extension for Another Throughout a
Directory Tree
4.20 Finding a File Given an Arbitrary Search Path
4.21 Finding a File on the Python Search Path
4.22 Dynamically Changing the Python Search Path
4.23 Computing Directory Sizes in a Cross-Platform Way
4.24 File Locking Using a Cross-Platform API
4.25 Versioning Filenames
4.26 Module: Versioned Backups
5. Object-Oriented Programming
5.1 Overriding a Built-In Method
5.2 Getting All Members of a Class Hierarchy
5.3 Calling a Superclass _ _init_ _ Method if It Exists
5.4 Calling a Superclass Implementation of a Method
5.5 Implementing Properties
5.6 Implementing Static Methods
5.7 Implementing Class Methods
5.8 Delegating Automatically as an Alternative to Inheritance
5.9 Decorating an Object with Print-Like Methods
5.10 Checking if an Object Has Necessary Attributes
5.11 Making a Fast Copy of an Object
Zgłoś jeśli naruszono regulamin