Book description
For those who know the basics of object-oriented Python this book is a must-have. With 750 code samples and a relaxed tutorial approach, it's the seamless route to more sophisticated programming.
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.
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
Publisher resources
Table of contents
-
Mastering Object-oriented Python
- Table of Contents
- Mastering Object-oriented Python
- Credits
- About the Author
- About the Reviewers
- www.PacktPub.com
- Preface
- Some Preliminaries
-
1. Pythonic Classes via Special Methods
- Pythonic Classes via Special Methods
-
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
- 2. Integrating Seamlessly with Python Basic Special Methods
- 3. Attribute Access, Properties, and Descriptors
- 4. The ABCs of Consistent Design
- 5. Using Callables and Contexts
- 6. Creating Containers and Collections
- 7. Creating Numbers
- 8. Decorators and Mixins – Cross-cutting Aspects
-
2. Persistence and Serialization
- Persistence and Serialization
- 9. Serializing and Saving – JSON, YAML, Pickle, CSV, and XML
- 10. Storing and Retrieving Objects via Shelve
-
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
- 12. Transmitting and Sharing Objects
-
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
-
3. Testing, Debugging, Deploying, and Maintaining
- Testing, Debugging, Deploying, and Maintaining
- 14. The Logging and Warning Modules
- 15. Designing for Testability
-
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
- 17. The Module and Package Design
-
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
- Index
Product information
- Title: Mastering Object-oriented Python
- Author(s):
- Release date: April 2014
- Publisher(s): Packt Publishing
- ISBN: 9781783280971
You might also like
book
Python Challenges: 100 Proven Programming Tasks Designed to Prepare You for Anything
Augment your knowledge of Python with this entertaining learning guide, which features 100 exercises and programming …
book
Modern C++ Programming Cookbook - Second Edition
A pragmatic recipe book for acquiring a comprehensive understanding of the complexities and core fundamentals of …
book
Pointers in C Programming: A Modern Approach to Memory Management, Recursive Data Structures, Strings, and Arrays
Gain a better understanding of pointers, from the basics of how pointers function at the machine …
book
Object-Oriented Python
Object-Oriented Python (OOP) is a paradigm that combines data and code into cohesive units, allowing you …