<<  <  Jan  2009  >  >>
Su Mo Tu We Th Fr Sa
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Moving house

I am moving my blog to Posterous and taking the opportunity to separate concerns.

Technical articles will be posted to http://swapoff.posterous.com while personal stuff (probably mostly rants) will go to http://alecthomas.posterous.com.


Emulating ActionScript's "with" statement in Python

If you haven't had much to do with ActionScript or Visual Basic, right now you might be thinking, as I did, "huh?" Basically the with statement in both of these languages allows one to access the attributes of an object as if they were in the local scope.

I know, I know. Why would you want to do this? I have no idea, but I thought it was cool that it was even possible in Python. However, there are some fairly major limitations: (...)


Walking a py3k AST

The compiler module has been removed from py3k. Fortunately there's a replacement in the _ast module introduced in Python 2.5. Unfortunately, while the compiler module has a useful __repr__esentation for its AST objects...

>>> compiler.parse('x = 10')
    Module(None, Stmt([Assign([AssName('x', 'OP_ASSIGN')], Const(10))]))

This is not the case for the _ast module, so here's a function that will dump _ast.AST objects as a dict: (...)


Type masquerading using dynamic base classes

Python allows us to construct new classes on-the-fly with type(). By (ab)using this feature we can construct a class that preserves its own interface while assuming all of the behaviour of an existing object. Useful for wrapping compound types such as dictionaries, lists, sets, etc. without having to proxy __getitem__, __iter__ and so on, or write a custom __getattr__.

One example of when this could be useful is a JSON-specific HTTP client object that assumes the type of the JSON data, while maintaining response-specific attributes such as headers and status: (...)


CLY 0.9 released

CLY is a project I've been working on for a while now, and I've finally gotten around to releasing it.

It's basically a CLI parser and grammar constructor that lets you easily add command-line interfaces to your applications:

echo.py:

from cly import *

def echo(text):
    print text

grammar = Grammar(
    echo=Node(help='Echo text')(
        text=Variable(help='Text to echo', pattern=r'.+')(

(...)


Adding support for a workingenv sandbox to setuptools/distutils

It seems that all I blog about is workingenv.

This time it's a snippet of code that adds a "sandbox" command to distutils, which automatically creates a workingenv from the setuptools extras_require, install_requires and dependency_links options listed in your setup() call. It only supports *nix systems for now, but could easily be extended to (...)


Dev Todo 0.1.20 released

Finally, a new release of Dev Todo. All bugs I'm aware of are fixed, and Dev Todo now builds cleanly on compilers from this century.

Check out the changelog for details.