Free Sample
+ Collection

Mastering Object-oriented Python

Steven Lott

If you want to master object-oriented Python programming this book is a must-have With 750 code samples and a relaxed tutorial, it’s a seamless route to learn.
RRP $26.99
RRP $44.99
Print + eBook

Want this title & more?

$12.99 p/month

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781783280971
Paperback634 pages

About This Book

  • Create applications with flexible logging, powerful configuration and command-line options, automated unit tests, and good documentation
  • Use the Python special methods to integrate seamlessly with built-in features and the standard library
  • Design classes to support object persistence in JSON, YAML, Pickle, CSV, XML, Shelve, and SQL

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
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
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
Containers and collections
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
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
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
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
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
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
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
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
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
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
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
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
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
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

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.


Read More

Recommended for You

Learning Python Data Visualization
$ 28.99