Master the secret tools every Python programmer needs to know
Professional Python goes beyond the basics to teach beginner- and intermediate-level Python programmers the little-known tools and constructs that build concise, maintainable code. Design better architecture and write easy-to-understand code using highly adoptable techniques that result in more robust and efficient applications. Coverage includes Decorators, Context Managers, Magic Methods, Class Factories, Metaclasses, Regular Expressions, and more, including advanced methods for unit testing using asyncio and CLI tools. Each topic includes an explanation of the concept and a discussion on applications, followed by hands-on tutorials based on real-world scenarios. All sample code is available for download from the companion website, and the "Python 3 first" approach covers multiple current versions, while ensuring long-term relevance.
Python offers many tools and techniques for writing better code, but often confusing documentation leaves many programmers in the dark about how to use them. This book shines a light on these incredibly useful methods, giving you clear guidance toward building stronger applications.
Learn advanced Python functions, classes, and libraries
Utilize better development and testing tools
Understand the "what," "when," "why," and "how"
Download example code to start programming right away
More than just theory or a recipe-style walk-through, this guide helps you learn — and understand — these little-known tools and techniques. You'll streamline your workflow while improving the quality of your output, producing more robust applications with cleaner code and stronger architecture. If you're ready to take your Python skills to the next level, Professional Python is the invaluable guide that will get you there.
About the Author
Luke Sneeringer is a Python developer living in Austin, TX. He is a regular speaker at PyCon, the largest gathering of Python programmers, and frequently instructs on Python, Ansible, and other topics related to software development and system administration. He currently works for Ansible, Inc., on the Ansible Tower, a comprehensive tool for IT automation.
Table of Contents: INTRODUCTION
PART I: FUNCTIONS
CHAPTER 1: DECORATORS
Understanding Decorators
Decorator Syntax
Order of Decorator Application
Where Decorators Are Used
Why You Should Write Decorators
When You Should Write Decorators
Additional Functionality
Data Sanitization or Addition
Function Registration
Writing Decorators
An Initial Example: A Function Registry
Execution-Time Wrapping Code
A Simple Type Check
Preserving the help
User Verification
Output Formatting
Logging
Variable Arguments
Decorator Arguments
How Does This Work?
The Call Signature Matters
Decorating Classes
Type Switching
A Pitfall
Summary
CHAPTER 2: CONTEXT MANAGERS
What Is a Context Manager?
Context Manager Syntax
The with Statement
The enter and exit Methods
Exception Handling
When You Should Write Context Managers
Resource Cleanliness
Avoiding Repetition
Propagating Exceptions
Suppressing Exceptions
A Simpler Syntax
Summary
CHAPTER 3: GENERATORS
Understanding What a Generator Is
Understanding Generator Syntax
The next Function
The StopIteration Exception
Python 2
Python 3
Communication with Generators
Iterables Versus Iterators
Generators in the Standard Library
range
dict.items and Family
zip
map
File Objects
When to Write Generators
Accessing Data in Pieces
Computing Data in Pieces
Sequences Can Be Infi nite
When Are Generators Singletons?
Generators within Generators
Summary
PART II: CLASSES
CHAPTER 4: MAGIC METHODS
Magic Method Syntax
Available Methods
Creation and Destruction
__init__ 61
__new__ 62
__del__ 62
Type Conversion 63
__str__, __unicode__, and __bytes__ 63
__bool__ 64
__int__, __fl oat__, and __complex__ 65
Comparisons
Binary Equality
Relative Comparisons
Operator Overloading
Overloading Common Methods
Collections
Other Magic Methods
Summary
CHAPTER 5: METACLASSES
Classes and Objects
Using type Directly
Creating a Class
Creating a Subclass
The type Chain
The Role of type
Writing Metaclasses
The new Method
new Versus init
A Trivial Metaclass
Metaclass Inheritance
Using Metaclasses
Python 3
Python 2
What About Code That Might Run on Either Version?
When Is Cross-Compatibility Important?
When to Use Metaclasses
Declarative Class Declaration
An Existing Example
How This Works
Why This Is a Good Use for Metaclasses
Class Verification
Non-Inheriting Attributes
The Question of Explicit Opt-In
Meta-Coding
Summary
CHAPTER 6: CLASS FACTORIES
A Review of type
Understanding a Class Factory Function
Determining When You Should Write Class Factories
Runtime Attributes
Understanding Why You Should Do This
Attribute Dictionaries
Fleshing Out the Credential Class
The Form Example
Dodging Class Attribute Consistency
Class Attributes Versus Instance Attributes
The Class Method Limitation
Tying This in with Class Factories
Answering the Singleton Question
Summary
CHAPTER 7: ABSTRACT BASE CLASSES
Using Abstract Base Classes
Declaring a Virtual Subclass
Why Declare Virtual Subclasses?
Using register as a Decorator
__subclasshook__
Declaring a Protocol
Other Existing Approaches
Using NotImplementedError
Using Metaclasses
The Value of Abstract Base Classes
Abstract Properties
Abstract Class or Static Methods
Built-in Abstract Base Classes
Single-Method ABCs
Alternative-Collection ABCs
Using Built-In Abstract Base Classes
Additional ABCs
Summary
PART III: DATA
CHAPTER 8: STRINGS AND UNICODE
Text String Versus Byte String
String Data in Python
Python 3 Strings
Python 2 Strings
six
Strings with Non-ASCII Characters
Observing the Difference
Unicode Is a Superset of ASCII
Other Encodings
Encodings Are Not Cross-Compatible
Reading Files
Python 3
Specifying Encoding
Reading Bytes
Python 2
Reading Other Sources
Specifying Python File Encodings
Strict Codecs
Suppressing Errors
Registering Error Handlers
Summary
CHAPTER 9: REGULAR EXPRESSIONS
Why Use Regular Expressions?
Regular Expressions in Python
Raw Strings
Match Objects
Finding More Than One Match
Basic Regular Expressions
Character Classes
Ranges
Negation
Shortcuts
Beginning and End of String
Any Character
Optional Characters
Repetition
Repetition Ranges
Open-Ended Ranges
Shorthand
Grouping
The Zero Group
Named Groups
Referencing Existing Groups
Lookahead
Flags
Case Insensitivity
ASCII and Unicode
Dot Matching Newline
Multiline Mode
Verbose Mode
Debug Mode
Using Multiple Flags
Inline Flags
Substitution
Compiled Regular Expressions
Summary
PART IV: EVERYTHING ELSE
CHAPTER 10: PYTHON 2 VERSUS PYTHON 3
Cross-Compatibility Strategies
The __future__ Module
2to3
Writing Changes
Limitations
six
Changes in Python 3
Strings and Unicode
The print Function
Division
Absolute and Relative Imports
Removal of "Old-Style" Classes
Metaclass Syntax
six.with_metaclass
six.add_metaclass
Exception Syntax
Handling Exceptions
Exception Chaining
Dictionary Methods
Function Methods
Iterators
Standard Library Relocations
Merging "Fast" Modules
io
pickle
The URL Modules
Renames
Other Package Reorganizations
Version Detection
Summary
CHAPTER 11: UNIT TESTING
The Testing Continuum
The Copied Ecosystem
The Isolated Environment
Advantages and Disadvantages
Speed
Interactivity
Testing Code
Code Layout
Testing the Function
The assert Statement 1
Unit Testing Frameworks
Running Unit Tests
Failures
Errors
Skipped Tests
Loading Tests
Mocking
Mocking a Function Call
Asserting Mocked Calls
Inspecting Mocks
Call Count and Status
Multiple Calls
Inspecting Calls
Other Testing Tools
coverage
tox
Other Test Runners
Summary
CHAPTER 12: CLI TOOLS
optparse
A Simple Argument
name == ‘ main__'
OptionParser
Options
Types of Options
Adding Options to OptionParser
Options with Values
Non-String Values
Specifying Option Values
Positional Arguments
Counters
List Values
Why Use optparse?
argparse
The Bare Bones
Arguments and Options 2
Option Flags
Alternate Prefi xes
Options with Values
Positional Arguments
Reading Files
Why Use argparse?
Summary
CHAPTER 13: ASYNCIO
The Event Loop
A Simple Event Loop
Running the Loop
Registering Tasks and Running the Loop
Delaying Calls
Partials
Running the Loop until a Task Completes
Running a Background Loop
Coroutines
Nested Coroutines
Futures and Tasks
Futures
Tasks
Callbacks
No Guarantee of Success
Under the Hood
Callbacks with Arguments
Task Aggregation
Gathering Tasks
Waiting on Tasks
Timeouts
Waiting on Any Task
Waiting on an Exception
Queues
Maximum Size
Servers
Summary
CHAPTER 14: STYLE
Principles
Assume Your Code Will Require Maintenance
Be Consistent
Think About Ontology, Especially with Data
Do Not Repeat Yourself
Have Your Comments Explain the Story
Occam's Razor
Standards
Trivial Rules
Documentation Strings
Blank Lines
Imports
Variables
Comments
Line Length
Summary
INDEX