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

A Little Bit of Python Episode 4: A Pre-PyCon Special

emoticon:home A Little Bit of Python is an occasional podcast on Python related topics with myself, Brett Cannon, Jesse Noller, Steve Holden and Andrew Kuchling.

The website is in progress and apparently nearly ready, thanks to Jesse and various other people who we will thank as soon as it is done. In the meantime, episode 4 is out. PyCon 2010 is only ten days away and it is the highlight of the year for many of us in the Python community. This episode is a pre-PyCon special where we discuss some of the things that will be happening at the conference and how to get the best out of it.

General links for the podcast feeds and a webpage with an embedded flash player:

If you have feedback, insults or suggestions for new topics you can email us on: all@bitofpython.com. We don't yet have the podcast listed on iTunes; we'll set that up once our permanent online home goes live.

We do have a twitter account, so for news on new episodes follow @bitofpython. A Little Bit of Python is also syndicated on Hacker Public Radio (although they've only released episode one so far).

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2010-02-08 00:13:48 | |

Categories: , Tags: , , ,


ConfigObj 4.7.1 (and how to test warnings)

emoticon:beaker I hate doing releases. I haven't managed to automate the whole process (I should probably work on that), although setup.py sdist upload certainly helps. Anyway, the short version of the story (and the real reason I hate releases) is that was a bug in ConfigObj 4.7.0. 4.7.1 is a brown paper bag release to fix the bug

The bug was an error in the way I had setup the deprecation warning for the obsolete options dictionary in the ConfigObj constructor. The bug only affects you if you were still using the options dictionary to configure ConfigObj instances.

If you've never heard of ConfigObj now is an ideal time to try it out. 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. It has lots of other features though :

  • Nested sections (subsections), to any level

  • List values

  • 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

The reason the bug happened is because not only did I not even try the deprecation warning to make sure it worked, let alone add a test for it. It turns out that adding a test for warnings is easy using the catch_warnings context manager, new in Python 2.6.

from warnings import catch_warnings
with catch_warnings(record=True) as log:
    ConfigObj(options={})

# unpack the only member of log
warning, = log
self.assertEqual(warning.category, DeprecationWarning)

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2010-02-07 23:52:41 | |

Categories: , Tags: , , ,


Discover 0.3.2 and the load_tests protocol

emoticon:dollars discover is a test discovery module for the standard library unittest test framework. Test discovery is built into unittest in Python 2.7 and 3.2. The discover module is a back-port of test discovery to work with Python 2.4 - 2.6 and Python 3.0 / 3.1 [1].

3.2 is a minor bugfix release. Test discovery also includes a new protocol called load_tests. In previous versions the standard tests would be passed in as a list instead of a TestSuite instance. This bug is now fixed both in untitest and in discover.

discover can be installed with pip or easy_install. After installing switch the current directory to the top level directory of your project and run:

python -m discover
python discover.py

This will discover all tests (with certain restrictions) from the current directory. The discover module has several options to control its behavior (full usage options are displayed with python -m discover -h). See the documentation on the PyPI homepage for details.

The load_tests protocol is interesting. It allows you to customize how tests are loaded from a module by defining a load_tests function. load_tests takes three arguments, the test loader, the standard set of tests for that module (allowing you to just add tests to the standard set if you want). The third argument is only used for load_tests functions in the __init__.py of test packages.

Here's an example of a test module with two test classes. The load_tests function returns a test suite that only uses one of the test classes:

import unittest


class FirstTest(unittest.TestCase):
    def testFoo(self):
        self.fail()

class SecondTest(unittest.TestCase):
    def testFoo(self):
        pass

def load_tests(loader, tests, _):
    return loader.loadTestsFromTestCase(SecondTest)

When the test module is loaded by discover, or unittest from Python 2.7 / 3.2, then load_tests will be called to create the test suite for the module.

[1]discover.py is only about 300 lines of Python. Supporting 2.x and 3.x in a single code-base is easy with small modules but I wouldn't recommend it for larger projects. Smile

Like this post? Digg it or Del.icio.us it.

Posted by Fuzzyman on 2010-02-07 23:25:47 | |

Categories: , , Tags: , ,


Hosted by Webfaction

Counter...