Hacking the Django admin is both bad form and, at times, a great time saver. As this post shows, subclassing admin.ModelAdmin can get you very far, specially if (unlike in that post), you go beyond save_model and start wrapping and replacing other methods of the base class (e.g., you can wrap form generation to preload useful dynamic values — and why the hell don’t default callables take arguments?). The obvious use case is creatively restricting what admin users can do (yes, I know, not what the admin is there for… or so we are told), but you can do very arbitrarily funny things.

Let’s implement the classic “Maybe” monad, for simple functions.


#!/usr/local/bin/python

class Undefined:
    pass

class Maybe:
    def __init__(self, x=Undefined):
        self.value = x

    def apply(self, f):
        if not self.is_defined:
            return Maybe()
        else:
            return Maybe(f(self.value))

    def __repr__(self):
        if not self.is_defined:
            return '<Undefined>'
        else:
            return repr(self.value)

    @property
    def is_defined(self):
        return not self.value is Undefined

def monad_aware(f):
    def fprime(x):
        return x.apply(f)
    return fprime

if __name__ == '__main__':

    @monad_aware
    def double(x):
        return 2*x

    x = Maybe()
    print double(x)
    assert(not double(x).is_defined)

    x = Maybe(2)
    print double(x)
    assert(double(x).is_defined)
    assert(double(x).value == 4)

Pastebin link

Long story short: When you apply a function to a value, the function controls the process. When you apply a function to a monad, the monad controls the process. That is what allows monads to extend the semantics of functions (e.g., adding side effects, or whatever).

And why not? Why should values be passive? (Note that accessors are a bit like half-monads; but full monads can do whatever the hell they want with functions, way beyond playing with what they see.)

[Very simple abstract setup, describing an ensemble of physical systems through a probability distribution over phase space, and dynamics by a differential equation.] Then, the well-known Liouville equation describes the dynamics of this distribution. For this type of evolution the Kullback-Leibler information measure provides a convenient way to measure the distance between two distinct probability distributions P1 and P2 because, remarkably, it is invariant under dynamical changes prescribed by the Liouville equation.

I’m not a physicist, but I feel compelled to annotate that — in the style of chess games — with a (!) (although I don’t think this is really a physics result). The Kullback-Leibler is a well-known (if not the) information distance between distributions, but I had no idea that it was preserved by such a wide range of dynamic systems.

Classical No-Cloning Theorem

Quoth the master:

As I have now said many times and written in many places: program testing can be quite effective for showing the presence of bugs, but is hopelessly inadequate for showing their absence.

— Dijkstra, A Discipline of Programming

This is much less powerful than it should be when taken out of context. Clearly, test suites no more “prove” that a program is valid than a predicate is “proved” by manually checking a few cases. Test suites are actually an improvement over customary practices, but only because customary practices are abysmally primitive. I’m not talking about pre-Internet primitive. I’m talking about pre-Russell, not to say pre-Descartes primitive.

Dijkstra’s writings are still among the most clear and solid examples of reasoning I’ve read, let alone reasoning about computer programming, and the fact that one can make a living as a “programmer” without thinking formally about anything ever, is as damning an indictment of the industry as I can imagine.

Not a very new one, but (as most of what Baez writes), quite interesting. The most enlightening bit for me:

Many of the puzzling features of quantum theory come from the noncartesianness of the usual tensor product in Hilb.

Physics, Topology, Logic and Computation: A Rosetta Stone

One of the craziest things I’m working on right now is a Python interpreter for a Forth-inspired mini-language I call pyf. It’s not a practical programming tool in almost any sense, being much less useful than the language I wrote the interpreter in, but I’ve found that the combination of Forth’s very simple syntax (in its way, it’s as elegant as Lisp’s) with Python’s rich semantics (pyf’s stack holds Python objects, not bytes, making it horrendously slow by Forth standards, but also saving me the need to write a bunch of utility words) makes it a potentially useful tool for creative programming/doodling with code/however you might want to call it. When done right — and I’m not there yet — Forth-style stack-oriented programming is a surprisingly interesting cognitive tool, because it seems to mirror and take advantage of cognitive heuristics that are more awkwardly modeled by other languages (and, of course, vice versa).

I might even end up using it as a macro language in other projects, I guess.

Inspired by a Frontier Economy article on email clients, and doing a Frankenstein on mail-trends, I created a Python script that analyzes your Gmail habits and makes heuristic recommendations on things that you should move to RSS feeds, and, perhaps more interestingly, people you might want to get back in touch with (a la Facebook, but for those of us that are still email-centered).

Question is, what would be the best way to deploy this? I’d like the feature to be available to the least-technical audience possible, and either contributing back to mail-trends or distributing it as a separate Open Source program (but still a Python command line script) wouldn’t be very effective in this sense, I feel. I thought about setting it up as a web service, but it does need Gmail password, and that’s probably a privacy no-no, specially for an unknown site. So I guess I should do it as a well-packaged and user-friendly small desktop utility… pretty much the kind of project I really, really don’t enjoy doing.

Ideas? Comments? Suggestions?

AntipodeanSF has just published a short story of mine, Apocalypse Hotel

You stand in the middle of an empty street, watching missile trails converge high above you. The plasma warheads explode, turning the air into an incandescent fluid that destroys everything you see. The blinding light is cold against your skin.

Digital Rights Management is the bone-headed idea that you can control information once it has left your systems.

Privacy is the bone-headed idea that you can control information once it has left your systems.

The digital movie that can’t be pirated is as mythical as the business or government record that won’t be warehoused and cross-indexed. In both cases technology makes too easy and untraceable what interests make too desirable.

Eric Schmidt is an hypocrite and a tool. That said, I don’t think we’re going to have any more success controlling our bits than the media distributors have had with theirs, so we need to figure out something else.

Am I happy with this? Nope. But it is what it is.