CONTENTS IN DETAIL
ACKNOWLEDGMENTS
INTRODUCTION
Who Should Read This Book and Why
About This Book
1
STARTING YOUR PROJECT
Versions of Python
Laying Out Your Project
What to Do
What Not to Do
Version Numbering
Coding Style and Automated Checks
Tools to Catch Style Errors
Tools to Catch Coding Errors
Joshua Harlow on Python
2
MODULES, LIBRARIES, AND FRAMEWORKS
The Import System
The sys Module
Import Paths
Custom Importers
Meta Path Finders
Useful Standard Libraries
External Libraries
The External Libraries Safety Checklist
Protecting Your Code with an API Wrapper
Package Installation: Getting More from pip
Using and Choosing Frameworks
Doug Hellmann, Python Core Developer, on Python Libraries
3
DOCUMENTATION AND GOOD API PRACTICE
Documenting with Sphinx
Getting Started with Sphinx and reST
Sphinx Modules
Writing a Sphinx Extension
Managing Changes to Your APIs
Numbering API Versions
Documenting Your API Changes
Marking Deprecated Functions with the warnings Module
Summary
Christophe de Vienne on Developing APIs
4
HANDLING TIMESTAMPS AND TIME ZONES
The Problem of Missing Time Zones
Building Default datetime Objects
Time Zone–Aware Timestamps with dateutil
Serializing Time Zone–Aware datetime Objects
Solving Ambiguous Times
Summary
5
DISTRIBUTING YOUR SOFTWARE
A Bit of setup.py History
Packaging with setup.cfg
The Wheel Format Distribution Standard
Sharing Your Work with the World
Entry Points
Visualizing Entry Points
Using Console Scripts
Using Plugins and Drivers
Summary
Nick Coghlan on Packaging
6
UNIT TESTING
The Basics of Testing
Some Simple Tests
Skipping Tests
Running Particular Tests
Running Tests in Parallel
Creating Objects Used in Tests with Fixtures
Running Test Scenarios
Controlled Tests Using Mocking
Revealing Untested Code with coverage
Virtual Environments
Setting Up a Virtual Environment
Using virtualenv with tox
Re-creating an Environment
Using Different Python Versions
Integrating Other Tests
Testing Policy
Robert Collins on Testing
7
METHODS AND DECORATORS
Decorators and When to Use Them
Creating Decorators
Writing Decorators
Stacking Decorators
Writing Class Decorators
How Methods Work in Python
Static Methods
Class Methods
Abstract Methods
Mixing Static, Class, and Abstract Methods
Putting Implementations in Abstract Methods
The Truth About super
Summary
8
FUNCTIONAL PROGRAMMING
Creating Pure Functions
Generators
Creating a Generator
Returning and Passing Values with yield
Inspecting Generators
List Comprehensions
Functional Functions Functioning
Applying Functions to Items with map()
Filtering Lists with filter()
Getting Indexes with enumerate()
Sorting a List with sorted()
Finding Items That Satisfy Conditions with any() and all()
Combining Lists with zip()
A Common Problem Solved
Useful itertools Functions
Summary
9
THE ABSTRACT SYNTAX TREE, HY, AND LISP-LIKE ATTRIBUTES
Looking at the AST
Writing a Program Using the AST
The AST Objects
Walking Through an AST
Extending flake8 with AST Checks
Writing the Class
Ignoring Irrelevant Code
Checking for the Correct Decorator
Looking for self
A Quick Introduction to Hy
Summary
Paul Tagliamonte on the AST and Hy
10
PERFORMANCES AND OPTIMIZATIONS
Data Structures
Understanding Behavior Through Profiling
cProfile
Disassembling with the dis Module
Defining Functions Efficiently
Ordered Lists and bisect
namedtuple and Slots
Memoization
Faster Python with PyPy
Achieving Zero Copy with the Buffer Protocol
Summary
Victor Stinner on Optimization
11
SCALING AND ARCHITECTURE
Multithreading in Python and Its Limitations
Multiprocessing vs. Multithreading
Event-Driven Architecture
Other Options and asyncio
Service-Oriented Architecture
Interprocess Communication with ZeroMQ
Summary
12
MANAGING RELATIONAL DATABASES
RDBMSs, ORMs, and When to Use Them
Database Backends
Streaming Data with Flask and PostgreSQL
Writing the Data-Streaming Application
Building the Application
Dimitri Fontaine on Databases
13
WRITE LESS, CODE MORE
Using six for Python 2 and 3 Support
Strings and Unicode
Handling Python Modules Moves
The modernize Module
Using Python Like Lisp to Make a Single Dispatcher
Creating Generic Methods in Lisp
Generic Methods with Python
Context Managers
Less Boilerplate with attr
Summary
INDEX