Mastering Object-oriented Python

Mastering Object-oriented Python

Language: English

Pages: 634

ISBN: 1783280972

Format: PDF / Kindle (mobi) / ePub


Key Features

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

An object-oriented approach to Python web development gives you a much more fully-realised experience of the language. The flexibility and power of Python, combined with the improvements in design, coding and software maintenance that object-oriented programming allows, is built to respond to the challenges of increasingly more complex and data-intensive application development, making difficult tasks much more manageable. This book has been designed to make this sophisticated approach to programming easier to learn quickly, providing you with a clear and coherent learning journey.

Beginning by looking at a range of design patterns for the _init_() method, you will learn how to effectively use a range of Python s special methods to create classes that integrate with Python s built-in features, and find detailed explorations and demonstrations of callables and contexts, containers and collections, numbers, and decorators and mixins, with a focus on best practices for effective and successful design. The book also features information that demonstrates how to create persistent objects using JSON, YAML, Pickle, CSV, XML, Shelve and SQL and shows you how to transmit objects between processes. Going further into OOP, you ll find expert information on logging, warnings, unit testing as well as working with the command line.

Structured in 3 parts to make the complexity of OOP more manageable - Pythonic Classes via Special Methods, Persistence and Serialization and Testing, Debugging, Deploying, and Maintaining this book offers deep insight into OOP that will help you develop expert level object-oriented Python skills.

What you will learn

  • Create applications with flexible logging, powerful configuration and command-line options, automated unit tests, and good documentation
  • Get to grips with different design patterns for the __init__() method
  • Design callable objects and context managers
  • Perform object serialization in formats such as JSON, YAML, Pickle, CSV, and XML
  • 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

About the Author

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.

Table of Contents

  1. The _init_() Method
  2. Integrating Seamlessly with Basic Python Special Methods
  3. Attribute Access, Properties, and Descriptors
  4. The ABCs of Consistent Design
  5. Using Callables and Contexts
  6. Creating Contrainers and Collections
  7. Creating Numbers
  8. Decorators and Mixins: Cross-Cutting Aspects
  9. Serializing and Saving JSON, YAML, Pickle, CSV, and XML
  10. Storing and Retrieving Objects via Shelve
  11. Storing and Retrieving Objects via SQLite
  12. Transmitting and Sharing Objects
  13. Configuration Files and Persistence
  14. The Logging and Warning Modules
  15. Designing for Testability
  16. Coping with the Command Line

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

look at this in depth in Chapter 9, Serializing and Saving - JSON, YAML, Pickle, CSV, and XML. Note also that the hard total and soft total method functions shown here don't fully follow the rules of blackjack. We return to this issue in Chapter 2, Integrating Seamlessly with Python – Basic Special Methods. Complete composite object initialization Ideally, the __init__() initializer method will create a complete instance of an object. This is a bit more complex when creating a complete instance

of play with a collection of Players. The following is the beginning of a Table class that handles the bets and cards: class Table: def __init__( self ): self.deck = Deck() def place_bet( self, amount ): print( "Bet", amount ) def get_hand( self ): try: self.hand= Hand2( d.pop(), d.pop(), d.pop() ) self.hole_card= d.pop() except IndexError: # Out of cards: need to shuffle. self.deck= Deck() return self.get_hand() print( "Deal", self.hand ) return self.hand def can_insure( self, hand ): return

parameter is the new value that the descriptor needs to be set to. • Descriptor.__delete__( self, instance ): In this method, the instance parameter is the self variable of the object being accessed. This method of the descriptor must delete this attribute's value. Sometimes, a descriptor class will also need an __init__() method function to initialize the descriptor's internal state. There are two species of descriptors based on the methods defined, as follows: • A nondata descriptor: This

kind of descriptor defines __set__() or __ delete__() or both. It cannot define __get__(). The nondata descriptor object will often be used as part of some larger expression. It might be a callable object, or it might have attributes or methods of its own. An immutable nondata descriptor must implement __set__() but may simply raise AttributeError. These descriptors are slightly simpler to design because the interface is more flexible. • A data descriptor: This descriptor defines __get__() at a

at the deck of cards that cards are shuffled and dealt from. Performance – the timeit module We'll make use of the timeit module to compare the actual performance of different object-oriented designs and Python constructs. The timeit module contains a number of functions. The one we'll focus on is named timeit. This function creates a Timer object for some statement. It can also include some setup code that prepares the environment. It then calls the timeit() method of Timer to execute the

Download sample

Download