Faking files

Sometimes we need code that provides a file-like interface but doesn't actually read from or write to any real files. For example, we might want to retrieve a string from a third-party library that only knows how to write to a file. This is an example of the adapter pattern in action we need an adapter that converts the file-like interface into a string-like one. Two such adapters already exist in the standard library, Stringio and Bytesio. They behave in much the same way, except that one...

Unpacking arguments

There's one more nifty trick involving variable arguments and keyword arguments. We've used it in some of our previous examples, but it's never too late for an explanation. Given a list or dictionary of values, we can pass those values into a function as if they were normal positional or keyword arguments. Have a look at this code def show_args arg1, arg2, arg3 THREE print arg1, arg2, arg3 arg1 ONE, arg2 TWO print Unpacking a sequence , end show_args some_args print Unpacking a dict , end...

A completely different way to set up variables

One of the most common uses for the various setup and teardown functions is to ensure certain class or module variables are available with a known value before each test method is run. py.test offers a completely different way to do this using what are known as funcargs, short for function arguments. Funcargs are basically named variables that are previously set up in a test configuration file. This allows us to separate configuration from execution of tests, and allows the funcargs to be used...

PyQt

The other major graphical toolkit supported under Python 3 is called PyQt. It is a set of bindings to the popular cross-platform Qt library, which is available under both commercial and open source licenses. PyQt can be downloaded from http www. PyQt is an advanced library that supports many features not normally considered part of a GUI toolkit. In some ways, PyQt is a desktop application framework, with extended support for everything from web browser widgets to databases to multimedia....

Extending builtins

We discussed briefly in Chapter 3 how built-in data types can be extended using inheritance. Now, we'll go into more detail as to when we would want to do that. When we have a built-in container object that we want to add functionality to, we have two options. We can either create a new object, which holds that container as an attribute composition , or we can subclass the built-in object and add or adapt methods on it to do what we want inheritance . Composition is usually the best alternative...

String manipulation

As you know, strings can be created in Python by wrapping a sequence of characters in single or double quotes. Multi-line strings can easily be created using three quote characters, and multiple hard-coded strings can be concatenated together by placing them side by side this is useful for placing long strings in function calls without exceeding a text width limit on your editor . Here are some examples a hello b 'world' c '''a multiple line string''' d More multiple e Three Strings Together...

Introducing SQLAlchemy

SQLAlchemy can be downloaded from http www.sqlalchemy.org . Only the 0.6 version and higher is supported on Python 3, and at the time of writing, the only underlying databases that are supported are SQLite and PostgresSQL. This isn't a huge deal, as SQLAlchemy provides an abstraction over database APIs, it is theoretically possible to write SQLAlchemy code that works on one database system and later use the exact same or only slightly modified code on another one. So if you're looking for MySQL...

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

Assertion methods

The general layout of a test case is to set certain variables to known values, run one or more functions, methods, or processes, and then prove that the correct results were returned or calculated by using Testcase assertion methods. There are a few different assertion methods available to confirm that specific results have been achieved. We just saw assertEqual, which will cause a test failure if the two parameters do not pass an equality check. The inverse, assertNotEqual, will fail if the...

Observer pattern

The observer pattern is useful for state monitoring and event handling situations. This pattern ensures a single core object can be monitored by an unknown, possibly expanding, array of observer objects. Whenever a value on the core object changes, it lets all the observer objects know that a change has occurred, by calling an update method. Each observer may be responsible for different tasks whenever the core object changes the core object doesn't know or care what those tasks are, and the...

Summary Bkn

We've covered string manipulation, file IO, and object serialization. We discussed how to combine hard-coded strings and program variables into outputtable strings using the powerful string formatting system, and learned the difference between binary and textual data. All told, we've seen How to use the various str methods Files in binary and textual formatters Context managers and the with statement Serializing data with pickle and json In the next chapter, we'll cover one of the most...

Template pattern

The template pattern is useful for removing duplicate code it's an implementation to support the Don't Repeat Yourself principle we discussed in Chapter 5. It is designed for situations where we have several different tasks to accomplish that have some, but not all, steps in common. The common steps are implemented in a base class, and the different steps are overridden in subclasses to provide custom behavior. In some ways, it's like a generalized strategy pattern, except similar sections of...

State pattern

State pattern is structurally similar to strategy pattern, but its intent and purpose are very different. The goal of the state pattern is to represent state-transition systems systems where it is obvious that an object can be in a specific state, and that certain activities may drive it to a different state. To make this work, we need a manager, or context class that provides an interface for switching states. Internally, this class contains a pointer to the current state each state knows what...

Facade pattern

The facade pattern is designed to provide a simple interface to a complex system of components. The objects in this system may need to be interacted with directly for complex tasks and interactions. Often, however, there is 'typical' usage for the system, and these complicated interactions aren't necessary in that common scenario. The facade pattern allows us to define a new object that wraps this typical usage of the system. Any code that wants to use the typical functionality can use the...

Decorator pattern

The decorator pattern allows us to wrap an object that provides core functionality with other objects that alter that functionality. Any object that uses the decorated object will interact with it in exactly the same way as if it were undecorated that is, the interface of the decorated object is identical to the core object . There are two primary uses of the decorator pattern Enhancing the response of a component that is sending data to a second component Supporting multiple optional behaviors...

State versus strategy

State pattern looks very similar to strategy pattern, indeed the UML diagrams for the two are identical. The implementation, too, is identical we could even have written our states as first-class functions instead of wrapping them in objects, as was suggested for strategy. While the two patterns have identical structures, their purposes are very different. The strategy pattern is used to choose an algorithm at runtime generally, only one of those algorithms is going to be chosen for a...

Equivalent Functions Lxml Libxml2

lxml is an advanced XML parsing library that uses the lightning fast libxml2 library to do the underlying hard work. It can be downloaded from the lxml website at http codespeak.net lxml . It is a third-party library and, in the past, has been difficult to install on some operating systems, although this should not be the case with the latest releases. If your needs are basic and can be covered by the ElementTree API we just discussed, then, by all means, use that. But if you need to parse...

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

Strategy pattern

The strategy pattern is a common demonstration of abstraction in object-oriented programming. The pattern implements different solutions to a single problem each in a different object. The client code can then choose the most appropriate implementation dynamically at runtime. Typically, different algorithms have different trade-offs one might be faster than another, but uses a lot more memory, while a third algorithm may be most suitable when multiple CPUs are present or a distributed system is...

Mutable byte strings

The bytes type, like str, is immutable. We can use index and slice notation on a bytes object and search for a particular sequence of bytes, but we can't extend or modify them. This can be very inconvenient when dealing with IO, as it is often necessary to buffer incoming or outgoing bytes until they are ready to be sent. For example, if we are receiving data from a socket, it may take several recv calls before we have received an entire message. This is where the bytearray built-in comes in....

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

Python Object Oriented Exercises

We covered a wide variety of material in this chapter, but we didn't cover anything in a lot of detail. These exercises, then, will be all about extra reading. All of the tools we discussed in this chapter have terrific documentation on their websites, including tutorials, API references, and specific examples. If any of the topics we discussed are of special interest to you, review the documentation for those libraries. Try them out, see how far you can push them. Acquire the knowledge you...

Placing it in context

This need to always close a file can make for some ugly code. Because an exception may occur during file IO, we ought to wrap all calls to a file in a try finally clause, and close the file in finally, regardless of whether IO was successful. This isn't very Pythonic there must be a more elegant way to do it. If we run dir on a file-like object, we see that it has two special methods named _enter_and_exit_. These methods turn the file object into what is known as a context manager. Basically,...

Adapter pattern

Unlike most of the patterns we reviewed in Chapter 8, the adapter pattern is designed to interact with existing code. We would not design a brand new set of objects that implement the adapter pattern. Adapters are used to allow two preexisting objects to work together, even if their interfaces are not compatible. Like the keyboard adapters that allow USB keyboards to be plugged into PS 2 ports, an adapter object sits between two different interfaces, translating between them on the fly. The...

Composite pattern

Python Script Nodal Interface

The composite pattern allows complex tree-like structures to be built from simple components. Composite objects are simply container objects, where the content may actually be another composite object. Traditionally, each component in a composite object must be either a leaf node that cannot contain other objects or a composite node. The key is that both composite and leaf nodes can be treated identically. The UML diagram is very simple This simple pattern, however, allows us to create very...

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

Case study Xdc

To tie everything together, we'll be writing a simple link collector, which will visit a website and collect every link on every page it finds in that site. Before we start, though, we'll need some test data to work with. Simply write some HTML files to work with that contain links to each other and to other sites on the internet, something like this contact.html gt Contact us lt a gt esme.html gt My Dog lt a gt hobbies.html gt Some hobbies lt a gt contact.html gt Contact AGAIN lt a gt Name one...

Flyweight pattern

The flyweight pattern is a memory optimization pattern. Novice Python programmers tend to ignore memory optimization, assuming the built-in garbage collector will take care of them. This is often perfectly acceptable, but when developing larger applications with many related objects, paying attention to memory concerns can have a huge payoff. In real life, the flyweight pattern is often implemented only after a program has demonstrated memory problems. It may make sense to design an optimal...

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