Case study

Let's tie all our new object-oriented knowledge together by going through a few iterations of object-oriented design on a somewhat real-world example. The system we'll be modeling is a library catalog. Libraries have been tracking their inventory for centuries, originally using card catalogs, and, more recently, electronic inventories. Modern libraries have web-based catalogs that we can query from our home. Let's start with an analysis. The local librarian has asked us to write a new card...

Lists

Lists are the least object-oriented of Python's data structures. While lists are, themselves, objects, there is a lot of syntax in Python to make using them as painless as possible. Unlike many other object-oriented languages, lists in Python are simply available. We don't need to import them and rarely need to call methods on them. We can loop over a list without explicitly requesting an iterator object, and we can construct a list (like a dictionary) with custom syntax. Further, list...

Composition and inheritance

So far, we've learned to design systems as a group of interacting objects, where each interaction is viewing the objects involved at an appropriate level of abstraction. But we don't know yet how to create those levels of abstraction. There are a variety of ways to do this we'll discuss some advanced design patterns in Chapter 8 and Chapter 9. But even most design patterns rely on two basic principles known as composition and inheritance. Composition is the act of collecting together several...

Testing with pytest

The Python unittest module is very verbose and requires a lot of boilerplate code to set up and initialize tests. It is based on the very popular JUnit testing framework for Java. It even uses the same method names you may have noticed they don't conform to the PEP-8 naming standard, which suggests underscores be used to separate words in a method name, rather than CamelCase and test layout. While this is effective for testing in Java, it's not necessarily the best design for Python testing....

Jinja Templating

Now, we can set up Jinja to serve some templates from a folder for us Well that was easy. This gives us a templates variable that we can use to load templates based on filename from the given folder. Before we create the CherryPy app server, let's have a look at the templates. Let's scrutinize the simple template for adding a blog article first block title New Entry endblock block content form method POST Title lt input name title type text size 40 gt lt br gt lt textarea name message rows 10...

Command pattern

The command pattern adds a level of abstraction between actions that must be done, and the object that invokes those actions, normally at a later time. In the command pattern, client code creates a Command object that can be executed at a later date. This object knows about a receiver object that manages its own internal state when the command is executed on it. The Command object implements a specific interface typically it has an execute or do_action method, and also keeps track of any...

String formatting

Python 3 has a versatile string formatting mechanism that allows us to easily construct strings comprised of hard-coded text and interspersed variables. We've used it in many previous examples, but it is much more versatile than the simple formatting specifiers we've used. Any string can be turned into a format string by calling the format method on it. This method returns a new string where specific characters in the input string have been replaced with values provided in the arguments and...

Python Design Pattern Command Object

Absolute imports 46 abstract factory pattern about 271 examples 272 implementing 273-275 UML class diagram 272 abstraction 16 access control 50, 51 adapter pattern about 257, 259 benefits 258 structure 258 UML diagram 258 add_child method 279 add_point method 127 Agent class 90 aggregation 19 all method 352 API SQLite 348 append method 170 append method 98 anchor 356 expand 356 fill 356 ipadx 356 ipady 356 padx 356 pady 356 side 357 assertDictEqual method 320 assertEqual method 318 assertFalse...

CherryPy

CherryPy version 3.2 is the first major web application server to be made available on the Python 3 platform. It can be downloaded from http cherrypy.org . It is not a full-stack framework like the very popular Django, TurboGears, or Zope libraries. These frameworks provide extra support for data storage, templating, authentication, and other common web operations. Such features are not impossible in CherryPy, you re just responsible for finding or implementing them yourself. CherryPy is a very...