Archive for April, 2007

Babelfish can’t touch this one

I have been incredibly disinterested in the ongoing spat between Alex Payne and David Hansson. That is, until this gem from Dive Into Mark: Translation From PR-Speak to English of Selected Portions of Rails Developer David Heinemeier Hansson’s Response to Alex Payne’s Interview. Far from impartial, but it’s worth reading.

Inline callbacks with Twisted and Python 2.5

A while ago I wrote about Defgen, which lets you use generator syntax to write Python code in a “blocking” format. I thought it was pretty cool, but it required some strange backflips to work right.

Yesterday, Glyph showed me something that was added to Twisted trunk a little while ago: inlineCallbacks. It’s the same concept, but because it uses the new yield syntax in Python 2.5, it’s even prettier.

Here’s the current source from doc/core/examples/ampclient.py:

from twisted.internet import reactor, defer
from twisted.internet.protocol import ClientCreator
from twisted.protocols import amp
from ampserver import Sum, Divide

def doMath():
    d1 = ClientCreator(reactor, amp.AMP).connectTCP(
        '127.0.0.1', 1234).addCallback(
            lambda p: p.callRemote(Sum, a=13, b=81)).addCallback(
                lambda result: result['total'])
    def trapZero(result):
        result.trap(ZeroDivisionError)
        print “Divided by zero: returning INF”
        return 1e1000
    d2 = ClientCreator(reactor, amp.AMP).connectTCP(
        ‘127.0.0.1′, 1234).addCallback(
            lambda p: p.callRemote(
                Divide,
                numerator=1234,
                denominator=0)
            ).addErrback(trapZero)
    def done(result):
        print ‘Done with math:’, result
    defer.DeferredList([d1, d2]).addCallback(done)

Here’s what it looks like using inlineCallbacks:

@defer.inlineCallbacks
def doMath():
    client = ClientCreator(reactor, amp.AMP)
    conn = yield client.connectTCP('127.0.0.1', 1234)
    sumResult = (yield conn.callRemote(Sum, a=13, b=81))['total']
    try:
        divideResult = yield conn.callRemote(
            Divide, numerator=1234, denominator=0)
    except ZeroDivisionError:
        print “Divided by zero: returning INF”
        divideResult = 1e1000
    result = [sumResult, divideResult]
    print ‘Done with math:’, result
    defer.returnValue(result)

It’s infinitely more legible. I’ve always hated the process of writing callback-driven procedures, just because the code no longer flows top-to-bottom. Even a simple client-side operation gets mangled (like above). But with inlineCallbacks, your code looks totally synchronous; it’s just executed asynchronously.

Too bad it’s only usable on Python 2.5. That means only Twisted-based applications can use it; the Twisted library itself won’t shed backwards-compatibility for a long time.

Dancing in the streets

Lisa Katayama links to a video of anime fans organizing an impromptu dance session in the middle of Akihabara. Pretty funny, until you see how quickly they flee when police show up; then it’s just a little creepy.

I suggest the Boston programming community organize something similar. Anyone interested in a Python Meetup in the middle of the street on Comm Ave?

Quo vadis?

Democrats are pushing as hard as they can to bring the troops home, and some Republicans are jumping on the bandwagon. Meanwhile, Bush has promised to veto any spending bill that includes a timetable. Pelosi is putting her weight into this, and it looks like we’re headed for a showdown. What a shame.

Americans elected the Democrats because they were sick of the shallow ineptitude characterized by the previous Congress. If Democrats want to maintain the impressive lead they hold, they need to prove they can make real progress on domestic issues before 2008. While it’s electrifying for their base to go head-to-head with Bush over troop withdrawal, it’s not going to be a constructive debate: both sides will walk away unsatisfied and angry, and other deals that might have happened in the meantime will get scuttled.

I’m not touching the issue of troop withdrawal because I’m not sure either side has it right. But if the Democrats want the upper hand, I think they’d be better off making inroads on topics like immigration and Medicare. The political will is already there to find solutions, and if Democrats prove they can solve these problems–after years of Republican intransigence–I think they’re locked in for the 2008 elections. But if Democrats spend their political capital in a fist fight with Bush on troop withdrawal, they’ll have a serious image problem to deal with later on.

Once in the White House, Democrats could orchestrate troop withdrawal the right way, using all of the tools and resources that are only available to the Commander-in-Chief. Trying to push withdrawal through a spending bill is short-sighted; it distracts Congress from problems that are solvable today, and that will ultimately hurt their chances for a presidential win.