About

All articles, tagged with “django”

Introspecting Python Decorators

Over the last couple of years I’ve found myself using python decorators to annotate handlers for web requests more and more, both when using Django and with micro-frameworks like mnml and newf.

Where the same functionality is required for all handlers, or the required functionality can be determined from standard request or response headers, using WSGI or Django middleware is fine, but where the required functionality is varies based on the handler its much cleaner to use a parameterised decorator than poluting the environment or response objects just to control the middleware. Functionality can be added to a framework as a suite of decorators and plugged together in an aspect oriented way like lego to easily build up sophisticated behaviours.

Unlike other mechanisms for implementing macros, templating or aspect orientation that introduce a new language, python decorators are pure syntactic sugar that under the hood are simply rewritten as python expressions:

@requires_oauth_scope("email")
def notify_friends(request):
    pass

Is simply shorthand for:

def notify_friends(request):
    pass

notify_friends = requires_oauth_scope("email")(notify_friends)

This simplicity is powerful as it allows decorators to also be used as normal functions, for example to build up higher level decorators that bundle common decorator configurations, but it also means that decorators potentially interact badly with another powerful Python feature: introspection.

In the above example the undecorated notify_friends function has the __name__ “notify_friends”, but the decorated function has the __name__ “requires_oauth_scope”. When decorators are used extensively, this can seriously impact the usefulness of introspection for debugging or generating documentation.

Decorating your decorators with the functools @wraps decorator, which copies the __name__ of the wrapped function over to the wrapping function solves this introspection problem, but introduces another: the decorators now become invisible to introspection. In the example above the __name__ of the decorated function would now be “notify_friends” as in the undecorated case, but we wouldn’t know that the function had been decorated or not.

A potential solution to this new problem is to store the details about the decoration in another attribute that can be inspected at runtime. In addition to copying over the __name__ attribute, functools.wraps also copies over the target __dict__ by default, allowing it to be used to store information about the decoration and be correctly copied over when decorators are chained:

from functools import wraps

def requires_oauth_scope(scope):

    def decorator(target):

        target.__dict__["my_project_requires_oauth_scope"] = scope

        @wraps(target)
        def wrapper(*args, **kwargs):
            # return target(*args, **kwargs) or FORBIDDEN if token does not have required scope

        return wrapper
    return decorator

By constructing decorators in this way we get the benefits of python decorators and more declarative C# style attributes that are visible to introspection.

The Why and How of Automated Testing with Python and Django

Jamie has just uploaded the movie of my talk “The Why and How of Automated Testing with Python and Django” which I gave at BrightonPy a week ago (and this time it really is a movie, clocking in at a feature length 1 hr and 35 minutes). The audio on the video is fine (and arguably the laptop-eye-view video is improved by chopping my head off for large parts of the talk), but it’s tricky to see the slides on the video, so I’ve uploaded them to slideshare.

The talk rambles a bit in places and there are a couple of things that betray my static language roots for example you can’t actually use unit tests to discover dependencies as easily in python as you can in C++. I’m also already evolving the JS testing stack I talk about here: moving from qunit, qmock and Selenium to jsmockito and possibly JsTestDriver. Overall I think it’s a pretty good overview of how an agile software engineering process can be screwed together.

Many thanks to @garethr for donating his Fabric scripts, Spike for his database migration cameo, Si for recommending Hudson, Dave for hooking me on automated testing and j4mie for organising the night and wrangling the video. If you’d like me to help your organisation improve its agile engineering process, please get in touch.

Spawning Django Blogs

Since leaving Linden Lab I have been talking to a number of people about doing freelance consulting and development work while I get my start-up off the ground and last week got round to setting up a UK limited company so that people will actually be able to pay me.

Setting up a company is insanely easy these days: if you go to companies made simple it will cost you less than £17 and 10 minutes of form filling. Coming up with a name is harder, but within a couple of hours I found that 18dex was available as a .com TLD, twitter account and facebook username. Meaningful 5 character .coms are pretty tricky to come by these days, so I snapped it up and 18 Dexterity Ltd. was born — a pretty fantastically geeky name for an agile software engineering company I hope you’ll agree.

A few minutes later I had a holding page up for 18dex.com, but it looked pretty sad with no content, so I started thinking about setting it up as a blog. I have a stack of relevant software engineering posts on jimpurbrick.com from the last few years, but they are sandwiched between less relevant posts on 100robots, Second Life and various miscellany. I didn’t want to move the software engineering posts from jimpurbrick.com as they’re part of what I do and regularly updating a single blog is quite enough work. I also didn’t want to copy the posts from one blog to another as it would potentially end up with 2 independent comment threads on each blog. There would be no definitive version of a post, a blatant violation of Don’t Repeat Yourself.

Luckily Django includes a piece of machinery to deal with this problem in its sites framework, something I’ve been meaning to have a closer look at for some time. The sites machinery simply lets you associate a piece of content with a site and keeps track of the current site, allowing you to filter the content in the database to only show a subset on each site.

While the byteflow blog engine I use for jimpurbrick.com supports the sites framework, each post is associated with a single site via a ForeignKey. In order to allow posts to be shown on both jimpurbrick.com and 18dex.com I had to change that ForeignKey field to be a ManyToManyField: a single line change in the python code, but something that requires a little wrangling to massage the existing data to fit the new model.

I’ve been using the excellent South in all my recent projects to allow me to easily migrate data across django model changes. Although jimpurbrick.com dates from long before South was available I managed to convince south to manage the migration by dumping the blog_post table to json, dropping the table and recreating it with south, reloading the data and then letting south migrate the data to the new ManyToMany schema. While this was slightly more fiddly than it could have been it means that the blog app is now being managed by south, which will make future development on the blogs much easier.

Once I had migrated the data to the new model and associated the software engineering posts in jimpurbrick.com with both sites in the django admin interface all that remained was for me to clone the jimpurbrick.com directory with mercurial to create an 18dex.com directory and choose and tweak a byteflow theme for the new site.

Once again I’ve been very impressed with Django and Byteflow, which have proven to be incredibly powerful tools that are very easy to work with. In a few hours I was able to create professional and personal views on to my blogging which can be easily administered from a single interface and allow comment threads and users to easily flow between them. If you’re just interested in my software engineering posts, head over to 18dex.com, if you want to hear about music, Second Life and everything else I get up to, stay subscribed to jimpurbrick.com. If you notice anything broken on either blog, then please leave a comment to let me know.

A Collaborative User Generated Ambient Augmented Virtual Reality Scientific Visualisation The Size Of Denmark

2 years ago at [Euro FOO 2006] (http://wiki.oreillynet.com/eurofoo06/index.cgi “Euro FOO 2006]) I met a mass of great people and enjoyed a torrent of wonderful conversations, but 2 of them in particular stuck with me. The first was with [Gavin Starks] (http://www.dgen.net/biog/ “d::gen network”) who commented that climate change would be much easier to deal with if we could see carbon dioxide. The second was with Claus Dahl who observed that [Second Life] (http://secondlife.com “Second Life”) is a great platform to prototype large scale [augmented reality] (http://en.wikipedia.org/wiki/Augmented_reality “Augmented Reality”) applications as every object in Second Life has an Id and you can give away free augmented reality glasses in the form of heads up displays (HUDs).

A year later I started to experiment with the latter idea with SLateIt, an augmented reality application that can be used to find, tag and rate virtual objects in Second Life. Although I think tagging, rating and recommendation systems have a bright future in navigating the vast quantities of people, places and stuff in Second Life, SLateIt mostly came about as a way to demo augmented virtual reality in Second Life without a large data set to associate with objects in SL.

Finally, last week, the awesome team of Max Williams, Ryan Alexander, Andrew Conway, [Simon Willison] (http://simonwillison.net), [Natalie Downe] (http://notes.natbat.net) and Chris Waigl helped me bring the two ideas together by mashing up SLateIt, SecondLife and Gavin Starks’ new [AMEE] (http://amee.cc) emissions data base to create [Carbon Goggles] (http://carbongoggles.org). Instead of mapping Second Life object Ids to tags and ratings, Carbon Goggles maps Second Life object Ids to AMEE URLs. The HUD queries carbongoggles.org for emissions data for nearby objects and, if found, overlays a sphere on the object with a volume corresponding to the monthly carbon emissions of the object. In 24 hours we managed to hack together a working system to demo at Mashed and 2 days later added an annotation interface that allows new objects to be annotated with emissions data.

Carbon Goggles has had some great coverage over the last week, but I really hope the story doesn’t end there. The goal is to annotate objects across Second Life to produce a collaborative user generated ambient augmented virtual reality scientific visualisation the size of Denmark. Together we can add an extra layer of information to Second Life allowing people to learn to make more informed decisions in real life while living their Second Life. If you’re part of a group in Second Life that would like to help annotate objects, host Carbon Goggles vendors in world, create videos or images of Carbon Goggles visualisations or would like to help in any other way, please join the Carbon Goggles group in Second Life and get in touch.

![Carbon Goggles] (http://farm4.static.flickr.com/3090/2601623427_e1a3d3076b.jpg)

Hello World

Well, not exactly. Having blogged previously on Terra Nova, the original Creation Engine and currently on the Official Second Life Blog, I’m not exactly stumbling blinking in to the blinding light of the blogosphere. Recently a number of things have come up that I’ve wanted to write more than 140 words about, but that wouldn’t fit on the Official Second Life Blog any more, so I’ve finally stopped mooching off other people and set up my own blog.

One reason I hadn’t got around to it sooner is that I’ve been torn between platforms. Although it’s been tempting to throw up a WordPress blog every time I’ve had something to talk about, I really wanted to build a blog in Django that I could tinker and experiment with. Although it’s just a matter of plugging bits together, it still takes a few hours to get a basic Django blog up and running and longer to add all the bells and whistles. I finally managed to break the impasse last week when I came across this list of Django blog engines and after some routing around decided to go with byteflow which has all the bells and whistles but is made of standard Django bits and is eminently tinkerable.

So, that’s what you see here: a default byteflow blog running on Django trunk running in mod_python as a virtual host (alongside the slateit.org and carbongoggles.org Django apps) inside apache2 running on ubuntu dapper on a virtual machine hosted by bytemark. It took long enough to get round to, but once I’d found byteflow it only took an hour to set up. I’ll be kicking the wheels and tinkering over the coming weeks, but if you find anything broken, please let me know.