python


This one is amusing … the case of the mysterious syntax error … turns out that repeated keyword arguments (kwargs) were illegal in python 2.4, ignored in python 2.5 and illegal again in python 2.6. This means that if some have crept into your codebase, you’ve now got a handful of syntax errors!

For example, here’s test.py:

    def foo(**kwargs):
        for k, v in kwargs.iteritems():
            print "%s: %s" % (k, v)

    foo(a = 1, b = 2, a = 3)

And here’s what happens when you run it under 2.4, 2.5 and 2.6:

    nick@pluto:~/tmp$ python2.4 test.py
      File "test.py", line 5
        foo(a = 1, b = 2, a = 3)
    SyntaxError: duplicate keyword argument
    nick@pluto:~/tmp$ python2.5 test.py
    a: 3
    b: 2
    nick@pluto:~/tmp$ python2.6 test.py
      File "test.py", line 5
        foo(a = 1, b = 2, a = 3)
    SyntaxError: keyword argument repeated

How could this creep in? Most likely through revision control mergers. Function calls with lots of kwargs are often laid out like:

    foo(
        kwarg_with_a_long_name=1,
        another_self_documenting_flag=False,
    )

Alice adds a kwarg for yadda="yadda yadda", at the start on the kwargs list, before kwarg_with_a_long_name. So does Bob, but Bob adds it at the end. The revision control system will happily merge these two changes without conflicts as:

    foo(
        yadda="yadda yadda",
        kwarg_with_a_long_name=1,
        another_self_documenting_flag=False,
        yadda="yadda yadda",
    )

And under python 2.5, neither Alice or Bob are likely to notice … after all, yadda=”yadda yadda” just as they intended.

But when the underlying system is upgraded to python 2.6, a mysterious “SyntaxError: keyword argument repeated” appears.

So I’m playing around with Google AppEngine (still!) trying to put together some kind of sensible use for it. AppEngine is neat-O, but it is also quite limited in what it can and can’t do. One of the most glaring problems (for my toy app) is the datastore query API, which has various restrictions, including:

Inequality Filters Are Allowed On One Property Only

Now this is pretty obviously an efficiency measure: retrieving on inequalities involves iterating along one index, and the datastore isn’t in the business of picking which one to iterate along. But its also really annoying if you actually want to do something which needs multiple inequalities.
(more…)

I’m still messing around with template languages … I’ve added a couple of new ones and here’s the revised leader board.  I tried adding ClearSilver for Perl & Python but I couldn’t get either of those to work …  pity, its an interesting approach.  As with the previous posts this is time in seconds for  1000 tables of 100 rows by 10 columns each, not counting template compilation time.  The 200x difference in performance is what makes it interesting:
(more…)

Why should Perl have all the fun?  Lets do the same thing but in Python, comparing Mako, Jinja and SimpleTAL: (more…)