Preface
Who This Book Is For
The Two Levels of Learning
Our Strategy in This Book
What’s Not Covered
Getting the Most Out of This Book
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
1. Scaling with Generators
Iteration in Python
Generator Functions
Advancing next()
Converting to a Generator Function
Do You Need Generators?
Generator Patterns and Scalable Composability
Text Lines to Dicts
Composable Interfaces
Fanning Out
Fanning In
Python Is Filled with Iterators
The Iterator Protocol
Conclusion
2. Creating Collections with Comprehensions
List Comprehensions
Formatting for Readability (and More)
Multiple Sources and Filters
Independent Clauses
Multiple Filters
Comprehensions and Generators
Dictionaries, Sets, and Tuples
Conclusion
3. Advanced Functions
Accepting and Passing Variable Arguments
Argument Unpacking
Variable Keyword Arguments
Keyword Unpacking
Combining Positional and Keyword Arguments
Functions as Objects
Key Functions in Python
Conclusion
4. Decorators
The Basic Decorator
Generic Decorators
Decorating Methods
Data in Decorators
Accessing Inner Data
Nonlocal Decorator State
Decorators That Take Arguments
Class-Based Decorators
Implementing Class-Based Decorators
Benefits of Class-Based Decorators
Decorators for Classes
Conclusion
5. Exceptions and Errors
The Basic Idea
Handling Exceptions
Exceptions for Flow Control
Finally Blocks
Dictionary Exceptions
Exceptions Are Objects
Raising Exceptions
Catching and Re-Raising
The Most Diabolical Python Antipattern
Conclusion
6. Classes and Objects: Beyond the Basics
Properties
Property Patterns
Validation
Properties and Refactoring
The Factory Patterns
Alternative Constructors: The Simple Factory
Dynamic Type: The Factory Method Pattern
The Observer Pattern
The Simple Observer
A Pythonic Refinement
Several Channels
Magic Methods
Rebelliously Misusing Magic Methods
Conclusion
7. Automated Testing
What Is Test-Driven Development?
Unit Tests and Simple Assertions
Fixtures and Common Test Setup
Asserting Exceptions
Using Subtests
Conclusion
8. Module Organization
Spawning a Module
Creating Separate Libraries
Multifile Modules
Import Syntax and Version Control
Nested Submodule Structure
Antipattern Warning
Import Side Effects
Conclusion
9. Logging in Python
The Basic Interface
Log Levels
Why Do We Have Log Levels?
Configuring the Basic Interface
Passing Arguments
Beyond Basic: Loggers
Log Destinations: Handlers and Streams
Logging to Multiple Destinations
Record Layout with Formatters
Conclusion
Parting Words
Index
About the Author