Mastering Object-oriented Python


Mastering Object-oriented Python
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
$22.95
save 15%!
Print + free eBook + free PacktLib access to the book: $71.98    Print cover: $44.99
$67.94
save 6%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Support
Sample Chapters
  • 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

Book Details

Language : English
Paperback : 634 pages [ 235mm x 191mm ]
Release Date : April 2014
ISBN : 1783280972
ISBN 13 : 9781783280971
Author(s) : Steven F. Lott
Topics and Technologies : All Books, Application Development, Open Source, Python


Table of Contents

Preface
Some Preliminaries
Part 1: Pythonic Classes via Special Methods
Chapter 1: The __init__() Method
Chapter 2: Integrating Seamlessly with Python – Basic Special Methods
Chapter 3: Attribute Access, Properties, and Descriptors
Chapter 4: The ABCs of Consistent Design
Chapter 5: Using Callables and Contexts
Chapter 6: Creating Containers and Collections
Chapter 7: Creating Numbers
Chapter 8: Decorators and Mixins – Cross-cutting Aspects
Part 2: Persistence and Serialization
Chapter 9: Serializing and Saving – JSON, YAML, Pickle, CSV, and XML
Chapter 10: Storing and Retrieving Objects via Shelve
Chapter 11: Storing and Retrieving Objects via SQLite
Chapter 12: Transmitting and Sharing Objects
Chapter 13: Configuration Files and Persistence
Part 3: Testing, Debugging, Deploying, and Maintaining
Chapter 14: The Logging and Warning Modules
Chapter 15: Designing for Testability
Chapter 16: Coping With the Command Line
Chapter 17: The Module and Package Design
Chapter 18: Quality and Documentation
Index
  • Some Preliminaries
    • About casino Blackjack
      • Playing the game
      • Blackjack player strategies
      • Object design for simulating Blackjack
    • Performance – the timeit module
    • Testing – unittest and doctest
      • Unit testing and technology spikes
    • Docstrings – RST markup and documentation tools
    • The IDE question
    • About special method names
    • Summary
  • 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
      • Faulty factory design and the vague else clause
      • Simplicity and consistency using elif sequences
      • Simplicity using mapping and class objects
        • Two parallel mappings
        • Mapping to a tuple of values
        • The partial function solution
        • Fluent APIs for factories
    • Implementing __init__() in each subclass
    • Simple composite objects
      • Wrapping a collection class
      • Extending a collection class
      • More requirements and another design
    • Complex composite objects
      • Complete composite object initialization
    • Stateless objects without __init__()
    • Some additional class definitions
    • Multi-strategy __init__()
      • More complex initialization alternatives
      • Initializing static methods
    • Yet more __init__() techniques
      • Initialization with type validation
      • Initialization, encapsulation, and privacy
    • Summary
  • Chapter 2: Integrating Seamlessly with Python – Basic Special Methods
    • The __repr__() and __str__() methods
      • Non collection __str__() and __repr__()
      • Collection __str__() and __repr__()
    • The __format__() method
      • Nested formatting specifications
      • Collections and delegating format specifications
    • The __hash__() method
      • Deciding what to hash
      • Inheriting definitions for immutable objects
      • Overriding definitions for immutable objects
      • Overriding definitions for mutable objects
      • Making a frozen hand from a mutable hand
    • The __bool__() method
    • The __bytes__() method
    • The comparison operator methods
      • Designing comparisons
      • Implementation of comparison for objects of the same class
      • Implementation of comparison for objects of mixed classes
      • Hard totals, soft totals, and polymorphism
      • A mixed class comparison example
    • The __del__() method
      • The reference count and destruction
      • Circular references and garbage collection
      • Circular references and the weakref module
      • The __del__() and close() methods
    • The __new__() method and immutable objects
    • The __new__() method and metaclasses
      • Metaclass example 1 – ordered attributes
      • Metaclass example 2 – self-reference
    • Summary
  • Chapter 3: Attribute Access, Properties, and Descriptors
    • Basic attribute processing
      • Attributes and the __init__() method
    • Creating properties
      • Eagerly computed properties
      • Setter and deleter properties
    • Using special methods for attribute access
      • Creating immutable objects with __slots__
      • Creating immutable objects as a tuple subclass
      • Eagerly computed attributes
    • The __getattribute__() method
    • Creating descriptors
      • Using a nondata descriptor
      • Using a data descriptor
    • Summary, design considerations, and trade-offs
      • Properties versus attributes
      • Designing with descriptors
      • Looking forward
  • Chapter 4: The ABCs of Consistent Design
    • Abstract base classes
    • Base classes and polymorphism
    • Callables
    • Containers and collections
    • Numbers
    • Some additional abstractions
      • The iterator abstraction
      • Contexts and context managers
    • The abc module
    • Summary, design considerations, and trade-offs
      • Looking forward
  • Chapter 5: Using Callables and Contexts
    • Designing with ABC callables
    • Improving performance
      • Using memoization or caching
    • Using functools for memoization
      • Aiming for simplicity using the callable API
    • Complexities and the callable API
    • Managing contexts and the with statement
      • Using the decimal context
      • Other contexts
    • Defining the __enter__() and __exit__() methods
      • Handling exceptions
    • Context manager as a factory
      • Cleaning up in a context manager
    • Summary
      • Callable design considerations and trade-offs
      • Context manager design considerations and trade-offs
      • Looking forward
  • Chapter 6: Creating Containers and Collections
    • ABCs of collections
    • Examples of special methods
    • Using the standard library extensions
      • The namedtuple() function
      • The deque class
      • The ChainMap use case
      • The OrderedDict collection
      • The defaultdict subclass
      • The counter collection
    • Creating new kinds of collections
    • Defining a new kind of sequence
      • A statistical list
      • Choosing eager versus lazy calculation
      • Working with __getitem__(), __setitem__(), __delitem__(), and slices
      • Implementing __getitem__(), __setitem__(), and __delitem__()
      • Wrapping a list and delegating
      • Creating iterators with __iter__()
    • Creating a new kind of mapping
    • Creating a new kind of set
      • Some design rationale
      • Defining the Tree class
      • Defining the TreeNode class
      • Demonstrating the binary tree set
    • Summary
      • Design considerations and Trade-offs
      • Looking forward
  • Chapter 7: Creating Numbers
    • ABCs of numbers
      • Deciding which types to use
      • The method resolution and the reflected operator concept
    • The arithmetic operator's special methods
    • Creating a numeric class
      • Defining FixedPoint initialization
      • Defining FixedPoint binary arithmetic operators
      • Defining FixedPoint unary arithmetic operators
      • Implementing FixedPoint reflected operators
      • Implementing FixedPoint comparison operators
    • Computing a numeric hash
      • Designing more useful rounding
    • Implementing other special methods
    • Optimization with the in-place operators
    • Summary
      • Design considerations and trade-offs
      • Looking forward
  • Chapter 8: Decorators and Mixins – Cross-cutting Aspects
    • Class and meaning
      • Constructing the functions
      • Constructing the class
      • Some class design principles
      • Aspect-oriented programming
    • Using built-in decorators
      • Using standard library decorators
    • Using standard library mixin classes
      • Using the context manager mixin class
      • Turning off a class feature
    • Writing a simple function decorator
      • Creating separate loggers
    • Parameterizing a decorator
    • Creating a method function decorator
    • Creating a class decorator
    • Adding method functions to a class
    • Using decorators for security
    • Summary
      • Design considerations and trade-offs
      • Looking forward
  • Chapter 9: Serializing and Saving – JSON, YAML, Pickle, CSV, and XML
    • Understanding persistence, class, state, and representation
      • Common Python terminologies
    • Filesystem and network considerations
    • Defining classes to support persistence
      • Rendering a blog and posts
    • Dumping and loading with JSON
      • Supporting JSON in our classes
      • Customizing JSON encoding
      • Customizing JSON decoding
      • The security and the eval() issue
      • Refactoring the encode function
      • Standardizing the date string
      • Writing JSON to a file
    • Dumping and loading with YAML
      • Formatting YAML data on a file
      • Extending the YAML representation
      • Security and safe loading
    • Dumping and loading with pickle
      • Designing a class for reliable pickle processing
      • Security and the global issue
    • Dumping and loading with CSV
      • Dumping simple sequences to CSV
      • Loading simple sequences from CSV
      • Handling containers and complex classes
      • Dumping and loading multiple row types in a CSV file
      • Filtering CSV rows with an iterator
      • Dumping and loading joined rows in a CSV file
    • Dumping and loading with XML
      • Dumping objects using string templates
      • Dumping objects with xml.etree.ElementTree
      • Loading XML documents
    • Summary
      • Design considerations and trade-offs
      • Schema evolution
      • Looking forward
  • Chapter 10: Storing and Retrieving Objects via Shelve
    • Analyzing persistent object use cases
      • The ACID properties
    • Creating a shelf
    • Designing shelvable objects
      • Designing keys for our objects
      • Generating surrogate keys for objects
      • Designing a class with a simple key
      • Designing classes for containers or collections
      • Referring to objects via foreign keys
      • Designing CRUD operations for complex objects
    • Searching, scanning, and querying
    • Designing an access layer for shelve
      • Writing a demonstration script
    • Creating indexes to improve efficiency
      • Creating top-level indices
    • Adding yet more index maintenance
    • The writeback alternative to index updates
      • Schema evolution
    • Summary
      • Design considerations and trade-offs
      • Application software layers
      • Looking forward
  • Chapter 11: Storing and Retrieving Objects via SQLite
    • SQL databases, persistence, and objects
      • The SQL data model – rows and tables
      • CRUD processing via SQL DML statements
      • Querying rows with the SQL SELECT statement
      • SQL transactions and the ACID properties
      • Designing primary and foreign database keys
    • Processing application data with SQL
      • Implementing class-like processing in pure SQL
    • Mapping Python objects to SQLite BLOB columns
    • Mapping Python objects to database rows manually
      • Designing an access layer for SQLite
      • Implementing container relationships
    • Improving performance with indices
    • Adding an ORM layer
      • Designing ORM-friendly classes
      • Building the schema with the ORM layer
      • Manipulating objects with the ORM layer
    • Querying post objects given a tag string
    • Improving performance with indices
      • Schema evolution
    • Summary
      • Design considerations and trade-offs
      • Mapping alternatives
      • Keys and key designs
      • Application software layers
      • Looking forward
  • Chapter 12: Transmitting and Sharing Objects
    • Class, state, and representation
    • Using HTTP and REST to transmit objects
      • Implementing CRUD operations via REST
      • Implementing non-CRUD operations
      • The REST protocol and ACID
      • Choosing a representation – JSON, XML, or YAML
    • Implementing a REST server – WSGI and mod_wsgi
      • Creating a simple REST application and server
      • Implementing a REST client
      • Demonstrating and unit testing the RESTful services
    • Using Callable classes for WSGI applications
      • Designing RESTful object identifiers
      • Multiple layers of REST services
      • Creating the roulette server
      • Creating the roulette client
    • Creating a secure REST service
      • The WSGI Authentication application
    • Implementing REST with a web application framework
    • Using a message queue to transmit objects
      • Defining processes
      • Building queues and supplying data
    • Summary
      • Design considerations and trade-offs
      • Schema evolution
      • Application software layers
      • Looking forward
  • Chapter 13: Configuration Files and Persistence
    • Configuration file use cases
    • Representation, persistence, state, and usability
      • Application configuration design patterns
      • Configuring via object construction
      • Implementing a configuration hierarchy
    • Storing the configuration in the INI files
    • Handling more literals via the eval() variants
    • Storing the configuration in PY files
      • Configuration via class definitions
      • Configuration via SimpleNamespace
      • Using Python with exec() for the configuration
    • Why is exec() a nonproblem?
    • Using ChainMap for defaults and overrides
    • Storing the configuration in JSON or YAML files
      • Using flattened JSON configurations
      • Loading a YAML configuration
    • Storing the configuration in property files
      • Parsing a properties file
      • Using a properties file
    • Storing the configuration in XML files – PLIST and others
      • Customized XML configuration files
    • Summary
      • Design considerations and trade-offs
      • Creating a shared configuration
      • Schema evolution
      • Looking Forward
  • Chapter 14: The Logging and Warning Modules
    • Creating a basic log
      • Creating a shared class-level logger
      • Configuring the loggers
      • Starting up and shutting down the logging system
      • Naming the loggers
      • Extending the logger levels
      • Defining handlers for multiple destinations
      • Managing the propagation rules
    • Configuration gotcha
    • Specializing logging for control, debug, audit, and security
      • Creating a debugging log
      • Creating audit and security logs
    • Using the warnings module
      • Showing API changes with a warning
      • Showing configuration problems with a warning
      • Showing possible software problems with a warning
    • Advanced logging – the last few messages and network destinations
      • Building an automatic tail buffer
      • Sending logging messages to a remote process
      • Preventing queue overrun
    • Summary
      • Design considerations and trade-offs
      • Looking forward
  • Chapter 15: Designing for Testability
    • Defining and isolating units for testing
      • Minimizing the dependencies
      • Creating simple unit tests
      • Creating a test suite
      • Including edge and corner cases
      • Mocking dependencies for testing
      • Using more mocks to test more behaviors
    • Using doctest to define test cases
      • Combining doctest and unittest
      • Creating a more complete test package
    • Using setup and teardown
      • Using setup and teardown with OS resources
      • Using setup and teardown with databases
    • The TestCase class hierarchy
    • Using externally defined expected results
    • Automated integration or performance testing
    • Summary
      • Design considerations and trade-offs
      • Looking forward
  • Chapter 16: Coping With the Command Line
    • The OS interface and the command line
      • Arguments and options
    • Parsing the command line with argparse
      • A simple on/off option
      • An option with an argument
      • Positional arguments
      • All other arguments
      • --version display and exit
      • --help display and exit
    • Integrating command-line options and environment variables
      • Providing more configurable defaults
      • Overriding configuration file settings with environment variables
      • Overriding environment variables with the configuration files
      • Making the configuration aware of the None values
    • Customizing the help output
    • Creating a top-level main() function
      • Ensuring DRY for the configuration
      • Managing nested configuration contexts
    • Programming In The Large
      • Designing command classes
      • Adding the analysis command subclass
      • Adding and packaging more features into an application
      • Designing a higher-level composite command
    • Additional composite command design patterns
    • Integrating with other applications
    • Summary
      • Design considerations and trade-offs
      • Looking forward
  • Chapter 17: The Module and Package Design
    • Designing a module
      • Some module design patterns
      • Module versus class
      • The expected content of a module
    • Whole module versus module items
    • Designing a package
      • Designing a module-package hybrid
      • Designing a package with alternate implementations
    • Designing a main script and the __main__ module
      • Creating an executable script file
      • Creating a __main__ module
      • Programming in the large
    • Designing long-running applications
    • Organizing code into src, bin, and test
    • Installing Python modules
    • Summary
      • Design considerations and trade-offs
      • Looking forward
  • Chapter 18: Quality and Documentation
    • Writing docstrings for the help() function
    • Using pydoc for documentation
    • Better output via the RST markup
      • Blocks of text
      • The RST inline markup
      • RST directives
      • Learning RST
    • Writing effective docstrings
    • Writing file-level docstrings, including modules and packages
      • Writing API details in RST markup
      • Writing class and method function docstrings
      • Writing function docstrings
    • More sophisticated markup techniques
    • Using Sphinx to produce the documentation
      • Using the Sphinx quickstart
      • Writing the Sphinx documentation
      • Filling in the 4+1 views for documentation
      • Writing the implementation document
      • Creating the Sphinx cross-references
      • Refactoring Sphinx files into directories
    • Writing the documentation
    • Literate programming
      • Use cases for literate programming
      • Working with a literate programming tool
    • Summary
      • Design considerations and trade-offs

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. He’s particularly adept at struggling with gnarly data representation problems. His other titles with Packt include Mastering Object-Oriented Python.

Steven is currently a technomad who lives in various places on the east coast of the US. His technology blog can be found at http://slott-softwarearchitect.blogspot.com.

Sorry, we don't have any reviews for this title yet.

Code Downloads

Download the code and support files for this book.


Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


Errata

- 1 submitted: last submission 15 May 2014

Page no: 263 | Errata type: Code

Given: return json.JSONEncoder.default(o)

Should be: return json.JSONEncoder.default(object)

 

Moreover, throughout the book as well as in the code file, if you find any instance where it is given as (o) please do consider it as (object)

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Mastering Object-oriented Python +    Haskell Data Analysis Cookbook =
50% Off
the second eBook
Price for both: $41.55

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • 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.

Approach

This book follows a standard tutorial approach with approximately 750 code samples spread through the 19 chapters. This amounts to over 5,900 lines of code that illustrate each concept.

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.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software