Archive for November 27th, 2008

Michael Crichton, RIP

November 27, 2008

[Belated, but better late than never…]

My fond memories of Michael Crichton, who died a few weeks ago, long predate his latter-day status as reactionary alarmist. As a lad I saw The Andromeda Strain when it was first on TV, and, being nerdly even then, I was captivated. The alien virus plot—hm, come to think of it, maybe he was a reactionary alarmist even then—was (especially to a nine-year-old) clever and thought-provoking; the five-level super-sterile top-secret government germ-fighting lab (with computer-controlled lasers!) was as nifty as the Enterprise (for which I of course had “blueprints”); the pseudo-factual quasi-documentary style was impressive. And best of all, the heroes were brainy science guys (and one gal), just like I wanted to be. I was hooked.

Crichton wasn’t exactly a great writer, but then, most bestsellers aren’t written by great writers, and his prose was certainly sturdy and serviceable. His great gifts included a rare ability to work meticulously through details and present them in a completely understandable way. His plot devices were often wildly imaginative, but always tethered to reality. Not only did Jurassic Park give you resurrected dinosaurs, it convinced you that resurrected dinosaurs were entirely possible, probably even inevitable. Even Crichton’s wilder plots and weaker books—I’m talking about Sphere here—had a certain weird plausibility to them, and were chock full of interesting ideas.

For Attention To Detail I don’t think one can beat The Great Train Robbery. I’ve just reread it (having decided to read something by Crichton in memoriam, and that being the first of his books to hand), and I enjoyed it as much as when I first read it thirty years ago. I’m not sure whether it’s a novel chock full of seamy Victoriana, or an essay about seamy Victoriana in novel form. I also don’t know how closely it follows the real robbery, but that hardly matters. The mechanics of the robbery, fiction or not, are fascinating, the criminal slang (doubtless dumbed down, insofar as it is accurate, or else it would be completely incomprehensible) is atmospheric, the discourses on criminals and prostitutes and London life in general wonderfully lurid.

And Crichton himself directed the movie of The Great Train Robbery. I haven’t seen that in years, but I remember liking it as much as I liked the book. How not, with Sean Connery at the height of his suavity as the master criminal, Donald Sutherland as his sidekick, and mind-blowingly gorgeous Lesley-Anne Down as his mistress? I’ve just talked myself into seeing it again; on the Netflix queue it goes.

Django Testing

November 27, 2008

Like many a geek, I’m a lazy bastard, so it’s only recently I’ve gotten around to writing unit tests for my tiny website (I at least have the excuse of doing this only for jollies; I’m no professional web developer). I had vaguely assumed that writing (and tests) would be more trouble than it’s worth, and that it would be difficult to test Really Important Stuff anyway. I had also vaguely assumed that those vague assumptions were almost certainly wrong, and I was a contemptible fool for not having written the tests up front.

It turns out, no surprise, that the latter vague assumption was correct. Python’s doctest and unittest frameworks are already relatively simple, and the django testing framework makes them simpler still (there’s a bit of annoying boilerplate to figure out in python’s raw unittest, which the django framework thoughtfully hides). The setup for test databases is especially nice.

[Mind you, in my first attempt I did somehow manage to blow away my local copy of my database—not just the test database, but the real one. I never did figure that one out…]

Embarrassingly, one of the first tests I wrote turned up a bug. Not to be wondered at, I suppose.

One note, on the very slim chance someone finds it useful. The django test framework looks for tests in each app’s model and test modules. At first I was annoyed by this: I like to put doctests in other places, as they really belong in the docstrings of the functions and classes they test. But—and you already know this if you’re a better python programmer than I—there’s a trivial way around this. The unittest and doctest modules already coexist awfully well. doctest.DocTestSuite creates a TestCase from a module’s doctests, and this can be amalgamated with unittests by defining a function called suite in the test module, which django will look for. That is, put something like this in tests.py or tests/__init__.py:

def suite():
    # An easy way of finding all the unittests in this module
    suite = unittest.TestLoader().loadTestsFromName(__name__)
    for mod in myapp.views, myapp.forms:
        suite.addTest(doctest.DocTestSuite(mod))
    return suite