Posted by Patrice Neff Fri, 13 Nov 2009

This Python on the Toilet issue is also available as PDF.

WSGI is a Python standard for how web servers can interact with web frameworks. It’s one of my favorite standards: it’s simple yet very powerful.

To write a WSGI web application you only need to create one function.

def my_app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return repr(environ)

The function receives a environ argument – a dictionary with all the environment variables. The start_response function can be called with the status line and a list of headers as soon as you’re ready to send output.

New Python releases contain the library wsgiref which can be used to get started quickly with a simple web server (that should not be used in production).

pre..from wsgiref.simple_server import make_server
httpd = make_server(’', 8000, my_app)

Save these two snippets in a file e.g. myapp.py and execute it with Python. This will serve the sample application on port 8000. Try it out.

You don’t usually want to write your own application directly on top of WSGI. But most frameworks now implement WSGI which has led to better interoperability. If you still want to use WSGI directly, there are a ton of good tools such as “WebOb”:?, “Werkzeug” or “Paste”. I used WebOb to easily build a REST service framework called WsgiService.

For more information I recommend the specification for WSGI which is a good read.

This post is part of the Python on the toilet series.

Pot: namedtuple

Posted by Patrice Neff Sun, 07 Jun 2009

For returning complex values from a method you have a few popular choices:
  1. Return a tuple with positional values. Those are very easy to unpack in Python and quite attractive because of that.
  2. Return a dictionary with the values. This can lead to slightly verbose accessing of the individual values.
  3. Return an object of some class you define. This required the definition of a class which can be too verbose for just a simple return value.

Since Python 2.6 you have one additional tool: collections.namedtuple. They give you a very lightweight class but it’s a one-liner to define them.

from collections import namedtuple

Child = namedtuple('Child', 'id, user, type, count')

def get_first_child():
    return Child('id', 'userid', 'special', 10)

res = get_first_child()
print res
print res.id, res.user, res.type, res.count

To create a class you call the namedtuple function with the type name and a string of field names. The field names are separated by whitespace and/or coma. Alternatively you can also pass in a list of strings.

The returned class can be used like any other class – but all values are read-only.

As always, the full details are available in the official API documentation for collections.namedtuple.

This post is part of the Python on the toilet series.

Pot: nose

Posted by Patrice Neff Mon, 06 Apr 2009

nose is my Python testing tool of choice. You can use it to very easily create test suites.

While it does support the unittest module which is included with Python, it also allows for a much simple style using just methods and asserts.

Put the following code into a file called test_basic.py:

def test_addition():
    print 2 + 2
    assert 2 + 2 == 4
def test_addition_wrong():
    print 2 + 3
    assert 2 + 3 == 6

Then just run nosetests in the directory where you stored this file. You’ll
get an output indicating, that there is a test failure.

nose is based completely on naming conventions. Make sure your test files and methods all start with test for them to get executed. For assertions, just use assert.

When a test goes wrong you usually need some additional output to know what happened. nose helps you with this by handling print statements nicely. In the example above, as test_addition_wrong fails, you’ll see the output of the print statement print 2 + 3. But the print output of test_addition is suppressed because that test does not fail.

One of my favourite features of nose is Test generators. Often you have a large list of inputs and corresponding expected outputs for a function. Test generators allow you to easily test all of them without having to repeat the test code many times.

An example from a Nektoon test suite:

def test_browsers():
    tests = [('Mozilla/4.0 (compatible; MSIE 8.0)', 'msie', '8'),
        ('Mozilla/4.0 (compatible; MSIE 7.0b)', 'msie', '7')]
    for ua, model, version in tests:
        yield check_browser, ua, model, version
def check_browser(ua, model, version):
    res = parse_ua(ua)
    print res
    assert res['model'] == model
    assert res['version'] == version

The tests list is of course much bigger in our test suite and that’s the beauty of it. Adding a new test is very easy, but nose still provides very meaningful error output in the case of a test failure.

Read the official nose documentation if I was able to wet your appetite.

This post is part of the Python on the toilet series.

Pot: the with statement

Posted by Patrice Neff Thu, 19 Mar 2009

Python’s with statement can be a very elegant alternative to long try/except/finally clauses. It offers a standard protocol that classes can implement to properly clean up state.

The best example for it’s value is file reading. A good implementation would have to look like this traditionally:

f = open('/tmp/myfile', 'r')
    content = f.read()

The with statement shortens this to the following code:

with open('/tmp/myfile', 'r') as f:
    content = f.read()

Behind the scenes it wraps the finally statement around this and makes sure that the file gets closed upon leaving the with block.

Your classes can implement the with statement with a simple protocol. The following example is silly, but demonstrates the point.

class Restorable(object):
    def __enter__(self):
        self.__dict_backup = copy.deepcopy(self.__dict__)
    def __exit__(self, type, value, tb):
        if tb is not None:
            self.__dict__ = self.__dict_backup
            return True

Restorable is a class that will backup the state when it’s called with the with statement. Upon leaving the with block with an exception, the previous state is restored and the exception ignored. The __exit__ method gets information about any exception that occurred inside the with block and can thus react differently to successful and failed execution.

There is more detail in the Python 2.6 changelog.

The full code of Restorable including usage examples of this classes is available as a syntax-colored paste on Lodge It.

This post is part of the Python on the toilet series.

Pot: Class properties

Posted by Patrice Neff Thu, 12 Mar 2009

As most programmers, I’ve been trained with the getter/setter idiom. It allows a programmer in any object-oriented programming language to create a class interface which makes sure that only valid data is written.

An example of a class with getter/setter in Python:

class Person1(object):
    def __init__(self):
        self._age = None
    def get_age(self):
        return self._age
    def set_age(self, age):
        self._age = age

We just wrote two methods – four lines – only to be able to modify and access some data. And we have to do that for every property. This is such a common idiom, that IDEs even have wizards for that.

Python takes a different road: properties. For starters, let’s write the simplest possible class with the behaviour from above:

class Person2(object):
    age = None

Usage of this class is a lot more natural – you just assign values – and it’s a lot shorter.

But the reason people started writing setters was access control. Maybe you want to make sure that no negative age is assigned? That’s where properties come in.

class Person3(object):
    _age = None
    def get_age(self):
        return self._age
    def set_age(self, age):
        if age < 0:
            raise Exception("Age can't be negative.")
        self._age = age
    age = property(get_age, set_age)

It’s almost the same as the first class – even a bit more code. But the usage of this class is exactly the same as before.

As you can see properties are extremely useful. They give you the flexibility of becoming more strict with the interface. But you don’t have to pass on this verbosity to the user. It also takes into account that most of the time you don’t care about the input values. So for the great majority of cases where the setter would really just be the one line, your class definition stays light and beautiful.

The full code including usage examples of this classes is available as a syntax-colored paste on Lodge It.

This post is part of the Python on the toilet series.

Pot: String formatting

Posted by Patrice Neff Thu, 05 Mar 2009

Starting with Python 2.6 there is a new format function to do string formatting. I’ve always been a bit overwhelmed by the official Format String Syntax documentation so I’ll try to present it to you with more examples.

Python so far had the '%’ operator. It’s been deprecated with Python 2.6 and removed from Python 3. Instead string has a new format method.

>>> "This is an %s-style formatting" % "old"
'This is an old-style formatting'
>>> "This is a {0}-style formatting".format("new")
'This is an new-style formatting'

Formatting options go after a colon, for example the width specifications together with alignment options which are mostly useful for tabular representations. Let’s print the following table as an exercise:

|   * Company *   |  * Employees *  |
| Apple           | 35000           |
|         Nektoon | 000000000000005 |
| local.ch        |           24.80 |

Note the centered headlines, and the different left- and right-align values with some padding and floating point formatting dropped in for good measure.

This is easily achieved with the following code:

>>> "| {0:^15} | {1:^15} |".format("* Company *", "* Employees *")
>>> "| {0:15} | {1:<15} |".format("Apple", 35000)
>>> "| {0:>15} | {1:015} |".format("Nektoon", 5)
>>> "| {0:15} | {1:15.2f} |".format("local.ch", 24.8)

As you can see, numbers are right-aligned and strings left-aligned by default. But you can force a different behaviour by manually aligning them using '<' or '>'.

Last but not least, format also accepts named parameters:

>>> "| {company:15} | {employees:15} |".format(
...     company="Nektoon", employees=5)
'| Nektoon         |               5 |'

That covers the most basic use cases of the format method. Having read this introduction, you can now use the Format String Syntax documentation as the reference it is.

The Python interpreter session is also available as a syntax-colored paste on Lodge It.

This post is part of the Python on the toilet series.

Pot - Python on the toilet

Posted by Patrice Neff Thu, 05 Mar 2009

With the Pot: String formatting I’m starting a series on my blog: Python on the toilet. I’ll try to write short and dense introductions for Python features or packages. Each introduction should fit on a A4 paper. So if you want to, you could print it out and hang on your toilet doors.

I’m not entirely sure yet what I’ll cover, but we’ll see what comes up. If you have topic proposals shoot me a mail.

This series was inspired of course by the great Testing on the toilet (ToT) series.