Python Programming, news on the Voidspace Python Projects and all things techie.
The all new and improved unittest: unittest2
At PyCon I gave a talk on some of the new features that are coming to unittest.
unittest is the Python standard library testing framework. In recent years unittest has languished whilst other Python testing frameworks like nose and py.test have innovated. Some of the best innovations have made their way into unittest which has had quite a renovation. In Python 2.7 & 3.2 a whole bunch of improvements to unittest will arrive.
The video of my talk (30 minutes) is up online:
I've turned the slides of the talk into an article:
The talk / article introduces unittest2, a back-port of the features in unittest to work with Python 2.4, 2.5 & 2.6. unittest2 is developed with a Mercurial repository on hg.python.org/unittest2 . unittest2 is already in use for the development of distutils2 and there is an open ticket to make use of it in Django.
The new features include:
- automatic test discovery and new command line features
- addCleanups - better resource management
- many new assert methods including better defaults for comparing lists, sets, dicts unicode strings etc and the ability to specify new default methods for comparing specific types
- assertRaises as context manager, with access to the exception object afterwards
- test skipping and expected failures
- load_tests protocol for controlling the loading of tests from modules or packages
- startTestRun and stopTestRun methods on TestResult
- various other API improvements and fixes
You can install unittest2 with:
pip install unittest2
To use it in your modules change import unittest to import unittest2. Alternatively you can conditionally make use of it where it is available:
try: import unittest2 as unittest except ImportError: import unittest
|||Thanks to Dirkjan Ochtman for setting this up and finally forcing me to make the jump to Mercurial. I've found the switch very easy and am now a big fan of Hg.|
New Release: ConfigObj 4.7.2
A new version of ConfigObj has just been released: 4.7.2
This is a bugfix release with several important bugfixes. It is recommended that all users of ConfigObj 4.7 update.
The new version can be installed with:
pip install -U configobj
- BUGFIX: Restore Python 2.3 compatibility
- BUGFIX: Members that were lists were being returned as copies due to interpolation introduced in 4.7. Lists are now only copies if interpolation changes a list member. (See below.)
- BUGFIX: pop now does interpolation in list values as well.
- BUGFIX: where interpolation matches a section name rather than a value it is ignored instead of raising an exception on fetching the item.
- BUGFIX: values that use interpolation to reference members that don't exist can now be repr'd.
- BUGFIX: Fix to avoid writing '\r\r\n' on Windows when write is given a file opened in text mode ('w').
See below for important details on the change to using list values with string interpolation.
What is ConfigObj?
ConfigObj is a simple but powerful config file reader and writer: an ini file round tripper. Its main feature is that it is very easy to use, with a straightforward programmer's interface and a simple syntax for config files. ConfigObj has a host of powerful features:
Nested sections (subsections), to any level
Multiple line values
Full Unicode support
String interpolation (substitution)
Integrated with a powerful validation system
- including automatic type checking/conversion
- and allowing default values
- repeated sections
All comments in the file are preserved
The order of keys/sections is preserved
Powerful unrepr mode for storing/retrieving Python data-types
String Interpolation and List Values
For general information on string interpolation in ConfigObj see: String Interpolation in ConfigObj.
Since version 4.7 string interpolation is done on string members of list values. If interpolation changes any members of the list then what you get back is a copy of the list rather than the original list.
This makes fetching list values slightly slower when interpolation is on, it also means that if you mutate the list changes won't be reflected in the original list:
>>> c = ConfigObj() >>> c['foo'] = 'boo' >>> c['bar'] = ['%(foo)s'] >>> c['bar'] ['boo'] >>> c['bar'].append('fish') >>> c['bar'] ['boo']
Instead of mutating the list you must create a new list and reassign it.
If you want to be able to safely mutate list values and still have interpolation on, you can use the following code to fetch the 'real' list. This code bypasses interpolation:
This can be used to fetch any value without doing interpolation. In 4.8 there will be a section method to do this without having to use the unsightly underscores.
An Awesome PyCon
As always PyCon was awesome, I had a great time catching up with old friends and making some new friends. The conference wasn't bad either...
This year was particularly awesome. After a recession fuelled downturn last year, this year numbers were back up to a recording breaking level. We had around 1025 attendees, and importantly a higher than ever proportion of women. Although 11% is hardly stellar it is much higher than previous years and better than many large open source conferences. This is still an underrepresentation of the number of women in IT, but still a great improvement. Much kudos to Gloria Willasden and Steve Holden who put a lot of effort into promoting PyCon to women developers and creating the Financial Assistance Grant for Women.
UPDATE: Final count for PyCon 2010 attendees was 1106.
This year was the year of alternative implementations, with a lot of focus on Unladen Swallow, PyPy, Jython, IronPython and even Pynie (a fledgling implementation of Python on the Parrot virtual machine). Nice to see that PyPy have just posted some benchmarks showing that they are largely faster than Unladen Swallow and CPython running Twisted. Performance is a hot topic for dynamic languages in general, and there is healthy competition amongst the implementations for speed. One of the things to come out of the Language Summit is a standard repository of benchmarks for implementations to share.
I was responsible for organising the Python Language Summit immediately preceding the conference. I think it went well, but as far as I'm concerned if it wasn't a complete disaster then it counts as a great success! At the summit we spent a lot of time discussing packaging. The upshot of this was a new way of handling the transition to an improved distutils - create a new module called distutils2 that is free to break backwards compatibility and then bring this into the standard library. Tarek has been working on improving distutils over the last year whilst maintaining backwards incompatibility. The problem is that so many tools depend on, extend and monkeypatch distutils in so many ways (including relying on buggy behaviour) that this has been virtually impossible. The new approach, a clean break, is good but means a sudden change of direction of Tarek. He has written this up, including the progress he and his team have already made: The Fate of distutils.
During the language summit Guido also approved 3 PEPs, all important in their own way:
argparse is now in the standard library.
The way Python creates and stores its compiled bytecode files (.pyc) is changing.
There are still a lot of issues to resolve before the final merge happens, but in principle the merge is now approved. The team, who includes several outside contributors making google employees a minority, need to improve startup time, reduce memory use and also make it faster - but it looks like Python 3.3 will have a JIT!
As with last year the audio visual team did an outstanding job of recording the talks, many of which (but not all at the time of writing) are available to watch online at pycon.blip.tv and the Python Miro Community. Some of the ones I recommend are:
Raymond Hettinger at his best!
Easily the star talk of the conference, standing room only. An extremely well presented talk on the Python Global Interpreter Lock by David Beazley.
Jimmy Schementi shows off running Python code in the browser with Silverlight - including <script type="text/python">...
Dino Veihland shows off the new IronPython integration in Visual Studio 2010, including very cool intellisense that does type inferencing on your code.
There are lots of great talks there, with more arriving, so have a browse through.
This year I had a new toy at PyCon, a portable audio recorder that I used to record a bunch of interviews for A Little Bit of Python. I've recorded some very cool interviews, but they'll take a bit of editing and will go live over the next few weeks:
- Antoine Pitrou - CPython developer and creator of the new GIL
- Paul Hildebrandt - Senior Programmer at Disney Animation Studios
- Frank Wierzbicki - Lead developer of Jython
- Christian Tismer - Creator of Stackless Python, one of the original PyPy developers plus developer of version 2 of Pysco the specializing JIT for Python
- Allison Randall - Architect of the Parrot VM and lead developer of Pynie
- David Beazley - Python trainer and author of The Python Essential Reference
- Jacob Kaplan-Moss & James Tauber - BDFLs of Django and Pinax
- Jimmy Schementi, Dino Veihland, Dave Fugate and Bill Chiles - The IronPython team
- Tarek Ziade - Developer currently fixing Python packaging
- Jean-Paul Calderone and Jonathan Lange - Twisted core developers
- Collin Winter and Jeffrey Yaskin - Lead Unladen Swallow developers
- Maciek Fijalkowski - A core PyPy developer
- Van Lindberg - PSF lawyer, PyCon chair and author of Intellectual Property and Open Source
As soon as any of these go up I'll be sure to let you know.
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.