Python Programming, news on the Voidspace Python Projects and all things techie.

The Testing Panel at EuroPython

emoticon:paper EuroPython 2010 is getting closer (July 19th - 22nd) with such esteemed speakers as Guido van Rossum, Raymond Hettinger, Brett Cannon and Bruce Lawson. Google are also making available grants for women to help girl geeks attend EuroPython.

One of the talks will be a testing panel, headed by the inestimable Ali Afshar, where you get to test the knowledge and wisdom of several Python testing 'experts' including:

  • Raymond Hettinger
  • Holger Krekel
  • Uhm... me
  • Geoff Bache
  • Mark Ramm (to be confirmed)
  • Jason Huggins (to be confirmed)

Ali has setup a google moderator instance for the panel, so you can prepare your fiendish questions in advance and vote on the questions that other people have asked:

Like this post? Digg it or it.

Posted by Fuzzyman on 2010-05-11 00:51:31 | |

Categories: , Tags: , ,

CherryPy with a serving of werkzeug

emoticon:avocado My next work project is an interesting application. We're porting a C++ Windows desktop application to IronPython and modernising it in the process. We'd also like to make it run on the Mac as well, which will be a relief to me as it will save me developing inside a VM. WPF, the most powerful Windows user interface is WPF - and this isn't available on Mac OS X even with Mono. The Silverlight UI is a cut-down version of WPF however, and so if we develop the application as an out-of-browser Silverlight application then it will be trivially easy (at least compared to porting a desktop application) to move it to the web at some future point. For the app to work as an out of browser Silverlight application it will need a local data server and so I've been looking at the simplest way of achieving this with Python (vanilla CPython).

The fastest pure Python web server is still good old CherryPy [1], but to make the application easily portable to the web it would be nice to develop the local component as a wsgi app. Fortunately CherryPy and wsgi are good friends and play nicely together. We can create a simple wsgi application with werkzeug and serve it with CherryPy. In our case the wsgi app is really just a very thin wrapper around a custom data processing / calculation component.

I couldn't find an example of exactly how to do this, but it wasn't very hard to work out from the documentation. Anyway, here's a simple proof-of-concept example:

from cherrypy import wsgiserver

# the werkzeug wsgi app
from app import application

d = wsgiserver.WSGIPathInfoDispatcher({'/': application})
server = wsgiserver.CherryPyWSGIServer(('localhost', 8080), d)

if __name__ == '__main__':
   except KeyboardInterrupt:

For the wsgi app the werkzeug example on the front page of the documentation works fine, but an even simpler one that doesn't require a templating engine installed (in our case we will be sending and receiving data - probably encoded using protobuf as it is more compact / faster than json and there are both CPython and Silverlight libraries for working with it) is:

from werkzeug import Request, Response, SharedDataMiddleware
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException

url_map = Map([Rule('/', endpoint='index')])

def application(request):
    adapter = url_map.bind_to_environ(request.environ)
        endpoint, values = adapter.match()
        response = Response(mimetype='text/html') = u'<h2>foo</h2>'
        return response
    except HTTPException, e:
        return e

application = SharedDataMiddleware(application, {})

There are interesting possibilities and challenges ahead, like creating a Windows service that starts the local server when you open the Silverlight app, or possibly creating a WPF desktop application and a Silverlight application that share code - something that became a lot easier with Silverlight 4.

[1]The development servers that ship with frameworks like Django are typically single threaded, blocking on individual requests and not suitable for production deployment even as a local server.

Like this post? Digg it or it.

Posted by Fuzzyman on 2010-05-11 00:29:26 | |

Categories: , Tags: , ,

Python 2.7 Beta 2 and unittest2 0.4.1

emoticon:black_hat The second, and hopefully last, beta of Python 2.7 has just been released. The next release will be a release candidate, so it is especially important that as many of you as possible try the beta and report any problems or incompatibilities!

Interesting new features in Python 2.7 (all of which are also in Python 3.1 or will be in Python 3.2) include:

  • DeprecationWarning is now silent by default as it is intended to be a message to developers and not to end-users of code written in Python
  • set literal syntax
  • dictionary and set comprehensions
  • the with statement can now use multiple context managers in one statement
  • improved rounding of strings from floating point numbers (a small but very nice change that took a huge amount of work)
  • str.format() method supports automatic numbering of the replacement fields (making it less verbose)
  • addition of an ordered dictionary to the collections module
  • addition of the total_ordering class decorator to the functools module
  • addition of the make_archive function to the shutil module
  • addition of WeakSet class to the weakref module
  • new modules argparse, importlib, ttk and sysconfig
  • dictionary based configuration for the logging module
  • new type the memoryview
  • dictionary views from Python 3 have been provided as the viewkeys, viewvalues and viewitems methods

Because of the Python language moratorium new language features are basically restricted to backports of changes that are already in Python 3. There have been many other library improvements, optimizations and bug fixes.

One important change in Python 2.7, possible the most important, that isn't mentioned in the list above is the set of improvements to the unittest module. There are many new features, including automatic test discovery that allows you to run the tests from a project without having to manually create the test suites and pass them to a test runner. This should make it much easier to get started with testing. The "What's New" document provides a very good summary of these changes, but I have written them up in a bit more detail in the article New Features in unittest.

Most of the changes to unittest were already in Python 2.7 Beta 1, but there have been a few bugfixes since then. I have backported the shiny new unittest to work with Python 2.4, 2.5 & 2.6 as the unittest2 module. To go alongside Python 2.7 Beta 2 there is a new release of unittest2, version 0.4.1, with exactly the same set of features:

You can install unittest2 with: pip install -U unittest2. The best documentation on the new features is the development version of the unittest documentation. Look for "New in Python 2.7" or "Changed in Python 2.7".

Changes in unittest2 0.4.1:

  • If test discovery imports a module from the wrong location (usually because the module is globally installed and the user is expecting to run tests against a development version in a different location) then discovery halts with an ImportError and the problem is reported
  • Added docstrings to assertRegexpMatches and assertNotRegexpMatches
  • Putting functions in test suites no longer crashes

Like this post? Digg it or it.

Posted by Fuzzyman on 2010-05-09 12:51:34 | |

Categories: , Tags: , ,

Hosted by Webfaction