Who This Book Is For

This book is aimed at programmers who have already learned the basics of object-oriented Python and need to write more sophisticated, flexible code that integrates seamlessly with the rest of Python. This book assumes a computer science background, with experience of common Python design patterns.

Table of Contents

Chapter 1: The __init__() Method

The implicit superclass – object

The base class object __init__() method

Implementing __init__() in a superclass

Using __init__() to create manifest constants

Leveraging __init__() via a factory function

Implementing __init__() in each subclass

Simple composite objects

Complex composite objects

Stateless objects without __init__()

Some additional class definitions

Multi-strategy __init__()

Yet more __init__() techniques

Summary

Chapter 2: Integrating Seamlessly with Python Basic Special Methods

The __repr__() and __str__() methods

The __format__() method

The __hash__() method

The __bool__() method

The __bytes__() method

The comparison operator methods

The __del__() method

The __new__() method and immutable objects

The __new__() method and metaclasses

Summary

Chapter 3: Attribute Access, Properties, and Descriptors

Basic attribute processing

Creating properties

Using special methods for attribute access

The __getattribute__() method

Creating descriptors

Summary, design considerations, and trade-offs

Chapter 4: The ABCs of Consistent Design

Abstract base classes

Base classes and polymorphism

Callables

Containers and collections

Numbers

Some additional abstractions

The abc module

Summary, design considerations, and trade-offs

Chapter 5: Using Callables and Contexts

Designing with ABC callables

Improving performance

Using functools for memoization

Complexities and the callable API

Managing contexts and the with statement

Defining the __enter__() and __exit__() methods

Context manager as a factory

Summary

Chapter 6: Creating Containers and Collections

ABCs of collections

Examples of special methods

Using the standard library extensions

Creating new kinds of collections

Defining a new kind of sequence

Creating a new kind of mapping

Creating a new kind of set

Summary

Chapter 7: Creating Numbers

ABCs of numbers

The arithmetic operator's special methods

Creating a numeric class

Computing a numeric hash

Implementing other special methods

Optimization with the in-place operators

Summary

Chapter 8: Decorators and Mixins – Cross-cutting Aspects

Class and meaning

Using built-in decorators

Using standard library mixin classes

Writing a simple function decorator

Parameterizing a decorator

Creating a method function decorator

Creating a class decorator

Adding method functions to a class

Using decorators for security

Summary

Chapter 9: Serializing and Saving – JSON, YAML, Pickle, CSV, and XML

Understanding persistence, class, state, and representation

Filesystem and network considerations

Defining classes to support persistence

Dumping and loading with JSON

Dumping and loading with YAML

Dumping and loading with pickle

Dumping and loading with CSV

Dumping and loading with XML

Summary

Chapter 10: Storing and Retrieving Objects via Shelve

Analyzing persistent object use cases

Creating a shelf

Designing shelvable objects

Searching, scanning, and querying

Designing an access layer for shelve

Creating indexes to improve efficiency

Adding yet more index maintenance

The writeback alternative to index updates

Summary

Chapter 11: Storing and Retrieving Objects via SQLite

SQL databases, persistence, and objects

Processing application data with SQL

Mapping Python objects to SQLite BLOB columns

Mapping Python objects to database rows manually

Improving performance with indices

Adding an ORM layer

Querying post objects given a tag string

Improving performance with indices

Summary

Chapter 12: Transmitting and Sharing Objects

Class, state, and representation

Using HTTP and REST to transmit objects

Implementing a REST server – WSGI and mod_wsgi

Using Callable classes for WSGI applications

Creating a secure REST service

Implementing REST with a web application framework

Using a message queue to transmit objects

Summary

Chapter 13: Configuration Files and Persistence

Configuration file use cases

Representation, persistence, state, and usability

Storing the configuration in the INI files

Handling more literals via the eval() variants

Storing the configuration in PY files

Why is exec() a nonproblem?

Using ChainMap for defaults and overrides

Storing the configuration in JSON or YAML files

Storing the configuration in property files

Storing the configuration in XML files – PLIST and others

Summary

Chapter 14: The Logging and Warning Modules

Creating a basic log

Configuration gotcha

Specializing logging for control, debug, audit, and security

Using the warnings module

Advanced logging – the last few messages and network destinations

Summary

Chapter 15: Designing for Testability

Defining and isolating units for testing

Using doctest to define test cases

Using setup and teardown

The TestCase class hierarchy

Using externally defined expected results

Automated integration or performance testing

Summary

Chapter 16: Coping With the Command Line

The OS interface and the command line

Parsing the command line with argparse

Integrating command-line options and environment variables

Customizing the help output

Creating a top-level main() function

Programming In The Large

Additional composite command design patterns

Integrating with other applications

Summary

Chapter 17: The Module and Package Design

Designing a module

Whole module versus module items

Designing a package

Designing a main script and the __main__ module

Designing long-running applications

Organizing code into src, bin, and test

Installing Python modules

Summary

Chapter 18: Quality and Documentation

Writing docstrings for the help() function

Using pydoc for documentation

Better output via the RST markup

Writing effective docstrings

Writing file-level docstrings, including modules and packages

More sophisticated markup techniques

Using Sphinx to produce the documentation

Writing the documentation

Literate programming

Summary

What You Will Learn

Understand the different design patterns for the __init__() method

Discover the essential features of Python 3's abstract base classes and how you can use them for your own applications

Design callable objects and context managers that leverage the with statement

Perform object serialization in formats such as JSON, YAML, Pickle, CSV, and XML

Employ the Shelve module as a sophisticated local database

Map Python objects to a SQL database using the built-in SQLite module

Transmit Python objects via RESTful web services

Devise strategies for automated unit testing, including how to use the doctest and the unittest.mock module

Parse command-line arguments and integrate this with configuration files and environment variables

In Detail

This practical example-oriented guide will teach you advanced concepts of object-oriented programming in Python. This book will present detailed examples of almost all of the special method names that support creating classes that integrate seamlessly with Python's built-in features. It will show you how to use JSON, YAML, Pickle, CSV, XML, Shelve, and SQL to create persistent objects and transmit objects between processes. The book also covers logging, warnings, unit testing, configuration files, and how to work with the command line.

This book is broken into three major parts: Pythonic Classes via Special Methods; Persistence and Serialization; Testing, Debugging, Deploying, and Maintaining. The special methods are broken down into several focus areas: initialization, basics, attribute access, callables, contexts, containers, collections, numbers, and more advanced techniques such as decorators and mixin classes.

Authors

Steven F. Lott

Steven F. Lott has been programming since the 70s, when computers were large, expensive, and rare. As a contract software developer and architect, he has worked on hundreds of projects, from very small to very large. He's been using Python to solve business problems for over 10 years.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.