Flow 08: Saturday and Sunday

On Saturday, it was raining cats and dogs. We were appropriately equipped and sniggered at the people in trainers etc trying to dodge the puddles. However, didn't see too many acts — saw a bit of Sébastien Tellier, but decided the crowds were too much and went to find some food (which was excellent and at 25 € for a three course vegetarian menu pretty good value.)

Next up on our schedule was CSS, which was pretty good. For some reason, it has never quite clicked for me, but still, they were busy as hell and obviously having fun.

And finally, The Roots. What a great show. No bling, no diva manners, just excellent hip hop with a surprising amount of jazz thrown in, just like on their early albums. And an incredibly diverse band, with Captain Kirk shredding his guitar and Tuba Gooding Jr on sousaphone.

Saturday was the only day it really felt like there were too many people stuffed into too small a space. Maybe it was the rain, maybe it was the fact that it was sold out, even though I don't think the other two days were that far behind.

On Sunday we were definitely starting to feel old and tired. It's surprising how tiring three days of festival gets, even without excessive drinking. Maybe I'm just too old. While eating on Saturday, I had the idea that they really should offer a show and dinner version of the festival; they already have an excellent restaurant on board, now just stretch out the dinner experience a bit and place the restaurant in a suitable location, and hey, the middle aged among us could be nice and comfy while checking out the gigs. And I really think they should have put the restaurant on the roof of the newer (if it is newer, the black one) gasometer.

We caught a glimpse of Astro Can Caravan, who had the weirdo jazz thing pretty well covered. Next up was The Five Corners Quintet, whose retro jazz was excellent as always. After that, we first checked out Plutonium 74, and had enough after one and a half songs. We listened to the first two or three songs from Señor Coconut and while their version of Daft Punk's Around the World wasn't horrible it wasn't nowhere near as good as Christian Prommer's on Friday, and by the time they hit Eurythmics' Sweet Dreams, we decided we had had enough of that too.

The next act we saw was José James who was one of the high points of the festival. He did a surprisingly jazzy gig and the crowd appreciated. After his gig, we listened for a couple of songs by Cut Copy, who really revealed themselves to be very summery party pop. Loud summery party pop. Their album In Ghost Colors was decent but I didn't get into it all that much, but they were better live. However, we were just too tired at that point and after a while headed home.

Article page

Flow08, Day 1

Flow08 started off a whole lot better than last year. Everything worked smoothly despite the fact that there were twice as many people and twice as large an area as last year. In fact, the enlarged space felt better than the more constrained area of last year, maybe because we got to see more of the Suvilahti grounds.

Jamie Lidell

Artists we saw:

  • Jamie Lidell was a positive surprise. I found a video I saw earlier a bit meh, but his show was energetic and fun, moving from style to style effortlessly.
  • Kings of Convenience and Múm were both nice enough and lots of people seemed to enjoy them a lot, but... eh, not really Friday night music? Múm did occasionally manage to get a bit of a rhythm going, but they were more in the artsy fartsy moody camp I'd rather listen on my headphones. Both would have been better on Saturday or Sunday afternoon.
  • 22-Pistepirkko was excellent. It's not like they are a new band, but it was the first time I saw them live. They were loud, some of the songs got completely different treatments than on their albums, and the feeling was great.
  • Christian Prommer's Drumlesson was fabulous. The guys got an incredible groove going from the start, jazzing straight into the hedonistic heart of dance music.

Some mostly lousy pics on Flickr. Maybe Marko will post some better ones.

Article page

Using custom widgets with Django's newforms-admin

tags python,django

The following isn't magic but it was unclear to me and required reading both documentation and source code and some additional Googling to get right. Maybe that's because I'm a Django newbie, but hey, I'm probably not the only one. By the way, the following applies to Django SVN revision 8068. That's roughly Django 1.0 alpha.

Anyway, I have in my model a field that's basically a time-stamped boolean: a field called deleted of type DateTimeField. If it's NULL, the thing, let's call it Foo, is not deleted; if it has a value, it tells us the Foo in question was marked as deleted back then. It's not the only way I could have implemented it but it meets my requirements and I didn't want to change it.

How to display that in admin? I could have left it as just a split datetime field, but that doesn't really communicate the intent. A checkbox with additional text telling the date is a much better representation.

I found some help in in Stuart Langridge's Overriding a single field in the Django admin, using newforms-admin, but it seems it's slightly outdated and didn't contain all the details.

Turns out for this to work well, I needed three bits: a widget class, a field class and a formfield_for_dbfield method in my ModelAdmin class.

Here's my widget class:

class BooleanDateWidget(forms.CheckboxInput):
    def __init__(self, attrs=None, check_test=lambda v: v is not None):
        super(BooleanDateWidget, self).__init__(attrs, check_test)

    def render(self, name, value, attrs=None):
        final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
        try:
            result = self.check_test(value)
        except: # Silently catch exceptions
            result = False
        if result:
            final_attrs['checked'] = 'checked'
            dt = ' <label for="%s" class="vCheckboxLabel">(%s)</label>' % (final_attrs["id"], value)
        else:
            dt = ""
        if value not in ('', True, False, None):
            # Only add the 'value' attribute if a value is non-empty.
            final_attrs['value'] = force_unicode(value)
        return mark_safe(u'<input%s />%s' % (flatatt(final_attrs), dt))

The render method has mostly been copied and pasted from django.forms.CheckboxInput.render, with a few modifications to create an additional label. When database value is rendered to the admin form, that method gets called. If deleted is NULL, it creates an empty checkbox; if there's a date there, it creates a checked checkbox with an additional label that contains the timestamp (not very prettily formatted, though.)

Next, the field class:

class BooleanDateField(fields.BooleanField):
    widget = BooleanDateWidget

    def clean(self, value):
        v = super(BooleanDateField, self).clean(value)
        if v:
            return datetime.datetime.now()
        return None

That's pretty simple. I just specify the widget to use and make the clean method which is called when the value sent by the browser is validated for storage in the model return either None if the checkbox was checked or None otherwise.

And finally the last piece is formfield_for_dbfield method in the ModelAdmin class.

class FooAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'deleted':
            field = db_field.formfield(form_class=booleandate.BooleanDateField)
        else:
            field = super(FooAdmin,self).formfield_for_dbfield(db_field,**kwargs)
        return field

That method is invoked when the newforms-admin interface needs the field object for displaying and handling the deleted value. It checks the field name to specialize the interface. For the deleted class, we call the db_field object's formfield method, otherwise we delegate to ModelAdmin. ModelAdmin's formfield_for_dbfield is pretty simple itself, it mostly just sets the widget type for a few field types and calls the formfield method of the db_field object. That's what we could have done here, too; instead of specifying the widget type in the Field class, we could have done it here.

That's all it takes! With those modifications, the split datetime field disappears and instead you get a datetime-labeled checkbox.

Article page

A moment of C++ hate

Apologies, I'm going to indulge myself for a moment. If you aren't interested in C++ ranting, skip this.

I'm in the process of converting some C++ code to heap allocate objects instead of putting them in the stack, because I need to use them in Objective-C++ and stack-allocated objects aren't the best idea there.

Who in their right mind wants to spend programming time worrying about where to allocate objects? Why do I have to care? There's an actual problem domain here with business objects, and I'm twiddling object allocation. I feel like bashing my head against the keyboard.

Article page

JUGC 0.2 with Python support

I just released version 0.2 (and 0.2.1, now with a NEWS file!) of JUGC, the unit conversion library generator. It allows you to specify a set of measuring units and translations in XML files and generate a conversion library from them, thus avoiding parsing a definition file at startup. The generator portion is still written in Java, but now in addition to Java, it can also generate Python code. There are small examples on the Examples wiki page. There isn't much documentation, but the example data files and tests should be clear enough.

Article page

pgrok

I put up pgrok ("Project Grokking for Emacs"), a simple Elisp package for project settings and tools, on Launchpad. It's a tidied up version of a couple of things I've been using for ages, basically for loading project (a source tree) settings, functions or whatever you need from files that are looked up in your directory hierarchy. There are also two (at the moment) functions for locating stuff in your project with find and grep.

There are no releases yet, but it's just one file in the bzr branch hosted on Launchpad. I use GNU Emacs 23.0.60, no guarantees about any other Emacsen.

Article page