Home Page Icon
Home Page
Table of Contents for
II. Data Structures
Close
II. Data Structures
by Luciano Ramalho
Fluent Python
Preface
Who This Book Is For
Who This Book Is Not For
How This Book Is Organized
Hands-On Approach
Hardware Used for Timings
Soapbox: My Personal Perspective
Python Jargon
Python Version Covered
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
I. Prologue
1. The Python Data Model
A Pythonic Card Deck
How Special Methods Are Used
Emulating Numeric Types
String Representation
Arithmetic Operators
Boolean Value of a Custom Type
Overview of Special Methods
Why len Is Not a Method
Chapter Summary
Further Reading
II. Data Structures
2. An Array of Sequences
Overview of Built-In Sequences
List Comprehensions and Generator Expressions
List Comprehensions and Readability
Listcomps Versus map and filter
Cartesian Products
Generator Expressions
Tuples Are Not Just Immutable Lists
Tuples as Records
Tuple Unpacking
Nested Tuple Unpacking
Named Tuples
Tuples as Immutable Lists
Slicing
Why Slices and Range Exclude the Last Item
Slice Objects
Multidimensional Slicing and Ellipsis
Assigning to Slices
Using + and * with Sequences
Building Lists of Lists
Augmented Assignment with Sequences
A += Assignment Puzzler
list.sort and the sorted Built-In Function
Managing Ordered Sequences with bisect
Searching with bisect
Inserting with bisect.insort
When a List Is Not the Answer
Arrays
Memory Views
NumPy and SciPy
Deques and Other Queues
Chapter Summary
Further Reading
3. Dictionaries and Sets
Generic Mapping Types
dict Comprehensions
Overview of Common Mapping Methods
Handling Missing Keys with setdefault
Mappings with Flexible Key Lookup
defaultdict: Another Take on Missing Keys
The __missing__ Method
Variations of dict
Subclassing UserDict
Immutable Mappings
Set Theory
set Literals
Set Comprehensions
Set Operations
dict and set Under the Hood
A Performance Experiment
Hash Tables in Dictionaries
Practical Consequences of How dict Works
How Sets Work—Practical Consequences
Chapter Summary
Further Reading
4. Text versus Bytes
Character Issues
Byte Essentials
Structs and Memory Views
Basic Encoders/Decoders
Understanding Encode/Decode Problems
Coping with UnicodeEncodeError
Coping with UnicodeDecodeError
SyntaxError When Loading Modules with Unexpected Encoding
How to Discover the Encoding of a Byte Sequence
BOM: A Useful Gremlin
Handling Text Files
Encoding Defaults: A Madhouse
Normalizing Unicode for Saner Comparisons
Case Folding
Utility Functions for Normalized Text Matching
Extreme “Normalization”: Taking Out Diacritics
Sorting Unicode Text
Sorting with the Unicode Collation Algorithm
The Unicode Database
Dual-Mode str and bytes APIs
str Versus bytes in Regular Expressions
str Versus bytes on os Functions
Chapter Summary
Further Reading
III. Functions as Objects
5. First-Class Functions
Treating a Function Like an Object
Higher-Order Functions
Modern Replacements for map, filter, and reduce
Anonymous Functions
The Seven Flavors of Callable Objects
User-Defined Callable Types
Function Introspection
From Positional to Keyword-Only Parameters
Retrieving Information About Parameters
Function Annotations
Packages for Functional Programming
The operator Module
Freezing Arguments with functools.partial
Chapter Summary
Further Reading
6. Design Patterns with First-Class Functions
Case Study: Refactoring Strategy
Classic Strategy
Function-Oriented Strategy
Choosing the Best Strategy: Simple Approach
Finding Strategies in a Module
Command
Chapter Summary
Further Reading
7. Function Decorators and Closures
Decorators 101
When Python Executes Decorators
Decorator-Enhanced Strategy Pattern
Variable Scope Rules
Closures
The nonlocal Declaration
Implementing a Simple Decorator
How It Works
Decorators in the Standard Library
Memoization with functools.lru_cache
Generic Functions with Single Dispatch
Stacked Decorators
Parameterized Decorators
A Parameterized Registration Decorator
The Parameterized Clock Decorator
Chapter Summary
Further Reading
IV. Object-Oriented Idioms
8. Object References, Mutability, and Recycling
Variables Are Not Boxes
Identity, Equality, and Aliases
Choosing Between == and is
The Relative Immutability of Tuples
Copies Are Shallow by Default
Deep and Shallow Copies of Arbitrary Objects
Function Parameters as References
Mutable Types as Parameter Defaults: Bad Idea
Defensive Programming with Mutable Parameters
del and Garbage Collection
Weak References
The WeakValueDictionary Skit
Limitations of Weak References
Tricks Python Plays with Immutables
Chapter Summary
Further Reading
9. A Pythonic Object
Object Representations
Vector Class Redux
An Alternative Constructor
classmethod Versus staticmethod
Formatted Displays
A Hashable Vector2d
Private and “Protected” Attributes in Python
Saving Space with the __slots__ Class Attribute
The Problems with __slots__
Overriding Class Attributes
Chapter Summary
Further Reading
10. Sequence Hacking, Hashing, and Slicing
Vector: A User-Defined Sequence Type
Vector Take #1: Vector2d Compatible
Protocols and Duck Typing
Vector Take #2: A Sliceable Sequence
How Slicing Works
A Slice-Aware __getitem__
Vector Take #3: Dynamic Attribute Access
Vector Take #4: Hashing and a Faster ==
Vector Take #5: Formatting
Chapter Summary
Further Reading
11. Interfaces: From Protocols to ABCs
Interfaces and Protocols in Python Culture
Python Digs Sequences
Monkey-Patching to Implement a Protocol at Runtime
Alex Martelli’s Waterfowl
Subclassing an ABC
ABCs in the Standard Library
ABCs in collections.abc
The Numbers Tower of ABCs
Defining and Using an ABC
ABC Syntax Details
Subclassing the Tombola ABC
A Virtual Subclass of Tombola
How the Tombola Subclasses Were Tested
Usage of register in Practice
Geese Can Behave as Ducks
Chapter Summary
Further Reading
12. Inheritance: For Good or For Worse
Subclassing Built-In Types Is Tricky
Multiple Inheritance and Method Resolution Order
Multiple Inheritance in the Real World
Coping with Multiple Inheritance
1. Distinguish Interface Inheritance from Implementation Inheritance
2. Make Interfaces Explicit with ABCs
3. Use Mixins for Code Reuse
4. Make Mixins Explicit by Naming
5. An ABC May Also Be a Mixin; The Reverse Is Not True
6. Don’t Subclass from More Than One Concrete Class
7. Provide Aggregate Classes to Users
8. “Favor Object Composition Over Class Inheritance.”
Tkinter: The Good, the Bad, and the Ugly
A Modern Example: Mixins in Django Generic Views
Chapter Summary
Further Reading
13. Operator Overloading: Doing It Right
Operator Overloading 101
Unary Operators
Overloading + for Vector Addition
Overloading * for Scalar Multiplication
Rich Comparison Operators
Augmented Assignment Operators
Chapter Summary
Further Reading
V. Control Flow
14. Iterables, Iterators, and Generators
Sentence Take #1: A Sequence of Words
Why Sequences Are Iterable: The iter Function
Iterables Versus Iterators
Sentence Take #2: A Classic Iterator
Making Sentence an Iterator: Bad Idea
Sentence Take #3: A Generator Function
How a Generator Function Works
Sentence Take #4: A Lazy Implementation
Sentence Take #5: A Generator Expression
Generator Expressions: When to Use Them
Another Example: Arithmetic Progression Generator
Arithmetic Progression with itertools
Generator Functions in the Standard Library
New Syntax in Python 3.3: yield from
Iterable Reducing Functions
A Closer Look at the iter Function
Case Study: Generators in a Database Conversion Utility
Generators as Coroutines
Chapter Summary
Further Reading
15. Context Managers and else Blocks
Do This, Then That: else Blocks Beyond if
Context Managers and with Blocks
The contextlib Utilities
Using @contextmanager
Chapter Summary
Further Reading
16. Coroutines
How Coroutines Evolved from Generators
Basic Behavior of a Generator Used as a Coroutine
Example: Coroutine to Compute a Running Average
Decorators for Coroutine Priming
Coroutine Termination and Exception Handling
Returning a Value from a Coroutine
Using yield from
The Meaning of yield from
Use Case: Coroutines for Discrete Event Simulation
About Discrete Event Simulations
The Taxi Fleet Simulation
Chapter Summary
Further Reading
17. Concurrency with Futures
Example: Web Downloads in Three Styles
A Sequential Download Script
Downloading with concurrent.futures
Where Are the Futures?
Blocking I/O and the GIL
Launching Processes with concurrent.futures
Experimenting with Executor.map
Downloads with Progress Display and Error Handling
Error Handling in the flags2 Examples
Using futures.as_completed
Threading and Multiprocessing Alternatives
Chapter Summary
Further Reading
18. Concurrency with asyncio
Thread Versus Coroutine: A Comparison
asyncio.Future: Nonblocking by Design
Yielding from Futures, Tasks, and Coroutines
Downloading with asyncio and aiohttp
Running Circles Around Blocking Calls
Enhancing the asyncio downloader Script
Using asyncio.as_completed
Using an Executor to Avoid Blocking the Event Loop
From Callbacks to Futures and Coroutines
Doing Multiple Requests for Each Download
Writing asyncio Servers
An asyncio TCP Server
An aiohttp Web Server
Smarter Clients for Better Concurrency
Chapter Summary
Further Reading
VI. Metaprogramming
19. Dynamic Attributes and Properties
Data Wrangling with Dynamic Attributes
Exploring JSON-Like Data with Dynamic Attributes
The Invalid Attribute Name Problem
Flexible Object Creation with __new__
Restructuring the OSCON Feed with shelve
Linked Record Retrieval with Properties
Using a Property for Attribute Validation
LineItem Take #1: Class for an Item in an Order
LineItem Take #2: A Validating Property
A Proper Look at Properties
Properties Override Instance Attributes
Property Documentation
Coding a Property Factory
Handling Attribute Deletion
Essential Attributes and Functions for Attribute Handling
Special Attributes that Affect Attribute Handling
Built-In Functions for Attribute Handling
Special Methods for Attribute Handling
Chapter Summary
Further Reading
20. Attribute Descriptors
Descriptor Example: Attribute Validation
LineItem Take #3: A Simple Descriptor
LineItem Take #4: Automatic Storage Attribute Names
LineItem Take #5: A New Descriptor Type
Overriding Versus Nonoverriding Descriptors
Overriding Descriptor
Overriding Descriptor Without __get__
Nonoverriding Descriptor
Overwriting a Descriptor in the Class
Methods Are Descriptors
Descriptor Usage Tips
Descriptor docstring and Overriding Deletion
Chapter Summary
Further Reading
21. Class Metaprogramming
A Class Factory
A Class Decorator for Customizing Descriptors
What Happens When: Import Time Versus Runtime
The Evaluation Time Exercises
Metaclasses 101
The Metaclass Evaluation Time Exercise
A Metaclass for Customizing Descriptors
The Metaclass __prepare__ Special Method
Classes as Objects
Chapter Summary
Further Reading
Afterword
Further Reading
A. Support Scripts
Chapter 3: in Operator Performance Test
Chapter 3: Compare the Bit Patterns of Hashes
Chapter 9: RAM Usage With and Without __slots__
Chapter 14: isis2json.py Database Conversion Script
Chapter 16: Taxi Fleet Discrete Event Simulation
Chapter 17: Cryptographic Examples
Chapter 17: flags2 HTTP Client Examples
Chapter 19: OSCON Schedule Scripts and Tests
Python Jargon
Index
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
1. The Python Data Model
Next
Next Chapter
2. An Array of Sequences
Part II.
Data Structures
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