« | » Main « | »

A Spot of Bother

Thursday 27 December 2007

I just finished A Spot of Bother, Mark Haddon's second novel. You may remember Haddon as the author of The Curious Incident of the Dog in the Night-Time.

A Spot of Bother has nothing to do with autism, it is a family story involving an impending wedding, infidelity, fear of death, and a few other things. It has humor but is not a comedy. It deals with serious situations, but is not melodramatic.

The thing Haddon does amazingly well is to get inside his characters' heads. In Curious Incident, he deftly imagined an autistic's inner monologue. In A Spot of Bother, he rotates among his characters, taking each of their viewpoints in turn. He does this to such a degree that there is absolutely no exposition in the book. So although the book is written in the third person, the viewpoint is actually multiple first-persons, to an extreme degree. Nothing is presented except as that character would express them.

This is a bit disorienting at first, but you quickly figure out who everyone is and their interrelationships. Presenting the story this way allows Haddon to present everything from pure points of view. I realized as I read the book that a traditional third person style gives you some distance from the characters. You are standing apart from them, even if only a little bit. In Haddon's style, you are the character.

A Spot of Bother


Wednesday 26 December 2007

Asymptote looks like a really full-featured technical drawing programming language. It makes a wide variety of drawings. They seem very proud of the LaTeX typesetting, but when I look at those drawings and see Knuth's Computer Modern fonts, it screams out for some real typography. Those fonts now seem as much a marker of time and place in technology as the plotter-based Hershey fonts did.


Sunday 23 December 2007

Hashiwokakero (or Hashi for short) is a Japanese puzzle. I discovered it in the back of a magazine at my haircutter, of all places. Like sudoku, it's an abstract puzzle requiring reasoning about a number of constraints at once.

A Hashi puzzle

The numbered circles are islands, your task is to fill in the bridges connecting them. There can be zero, one or two bridges between each pair of islands, orthogonally. Bridges cannot cross, and the number on the island indicates the number of bridges to the island. Lastly, all the islands must be connected:

The solution

Indigo Puzzles has the nicest online implementation: not only can you interactively solve the puzzle, but you can ask for advice, and it will patiently explain a possible next deduction, and why. They have lots of puzzles if you join the site (free).

BTW: Hashi is one of the puzzles published by Nikoli, a Japanese puzzle magazine with many types of puzzles.

Quick links: wrapper, design, later, launchy

Thursday 20 December 2007

A few tools I happened upon today, mostly because friends sent me links:

¶   CSS Text Wrapper: a cool GUI for making irregular text wrapping in browsers.

¶   Design: an elegant and beautiful suite of browser tools for working with design geometry.

¶   Read it Later: a Firefox extension for saving links to read later.

¶   Launchy: the Windows key-based launcher, now at version 2.0!

Ancient history: the Digital logo

Sunday 16 December 2007

My first corporate job was with Digital Equipment Corporation. I worked in the printer group, on PostScript technologies. It was common then to simulate the Digital logo by scaling Helvetica and superimposing it in white onto colored rectangles.

But I knew that was inaccurate, and it gave a bad hacked-up impression. So I took it upon myself to create a genuine Digital logo in PostScript. My association with the logo was strong enough that I still get requests every few months for the logo. I am now an HP employee, so I have contact with even more ex-DECcies interested in the logo (HP bought Compaq which bought Digital, you see).

When the latest request came in, I decided to make a serious attempt at resurrecting the logo.

» read more of: Ancient history: the Digital logo... (11 paragraphs)

The Digital logo

Hopefully, these will satisfy the needs for Digital logo fans. If you need anything more, let me know!

Human sorting

Tuesday 11 December 2007

Kate Rhodes wrote a post about simplistic vs. useful sorting: Alphabetical != ASCIIbetical (cute name). In it, she points to Dave Koelle's Alphanum algorithm, which says to split the string to be sorted into numeric and non-numeric chunks, then sort so that the numeric chunks are treated as numbers. This makes "z2" sort after "z100", for example.

I looked at the code Dave provided (in Java, C++, or Perl), and all of it is much longer than I expected: the C++ is 40 or so lines. A comment in there says it's easier in a pattern language like Perl, but the Perl is still 20 iterative lines.

In Python:

import re

def tryint(s):
        return int(s)
        return s
def alphanum_key(s):
    """ Turn a string into a list of string and number chunks.
        "z23a" -> ["z", 23, "a"]
    return [ tryint(c) for c in re.split('([0-9]+)', s) ]

def sort_nicely(l):
    """ Sort the given list in the way that humans expect.

A few helpful Python features make this more compact: re.split provides just the function we want for chunking the string, sort takes a key function for computing sort keys from data, the key to sort on can itself be a list, and comparing two lists compares lexicographically among the elements of the list.

Each of these features in and of itself may have only occasional use, but here they conspire to help me write code nearly as expressive as the English description in my first paragraph. And there are enough of those features that they often help make expressive code like that.

Presidential prediction

Monday 10 December 2007

It's still 11 months until the presidential election, and we haven't had a primary or caucus yet, but I'm willing to make a prediction: the Republican nominee will win.

I have two reasons. First, all the likely Democrat nominees are senators, and the likely Republican nominees are either governors or mayors. Historically, executive branch candidates do much better than those from the legislative branch. To quote Wikipedia's U.S. Presidential Elections page:

Contemporary electoral success has favored state governors. Of the last five presidents (Jimmy Carter, Ronald Reagan, George H. W. Bush, Bill Clinton, George W. Bush), four have been governors of a state (all except for George H. W. Bush). Geographically, these presidents were all from either very large states (California, Texas) or from a state south of the Mason-Dixon Line and east of Texas (Georgia, Arkansas). The last sitting U.S. Senator to be elected president was John F. Kennedy of Massachusetts in 1960. The only other sitting senator to be elected was Warren G. Harding in 1920, whereas major-party candidate Senators Andrew Jackson (1824), Lewis Cass (1848), Stephen Douglas (1860), Barry Goldwater (1964), George McGovern (1972), Walter Mondale (1984), Bob Dole (1996), and John Kerry (2004) all lost their elections.

Maybe this is because of the compromise nature of the legislative process, and the hay that can be made of it. Remember "He voted for the war before he voted against the war"? Maybe it's that personalities that succeed in Congress are not well-suited to executive branch work. I don't know.

The second reason is that both parties have moved to the left. Clinton and Obama are both more liberal than Kerry was, and neither Romney or Giuliani is as conservative as Bush was. That means that the Republicans are moving toward the center at the same time that the Democrats are moving away from it. I've heard more than one liberal Massachusetts friend say that Giuliani wouldn't be bad, for example.

I hope I am wrong, but from this (admittedly early) vantage point, it doesn't look good for Democrats.

Extracting JPGs from PDFs

Thursday 6 December 2007

I was trying to diagnose a problem with a PDF file we generated yesterday, and suspected that the images were corrupted. To see, I wrote this quick script to extract JPGs from PDF files. It is quick and dirty, with the absolute minimum understanding of PDF files, which can be quite opaque.

# Extract jpg's from pdf's. Quick and dirty.
import sys

pdf = file(sys.argv[1], "rb").read()

startmark = "\xff\xd8"
startfix = 0
endmark = "\xff\xd9"
endfix = 2
i = 0

njpg = 0
while True:
    istream = pdf.find("stream", i)
    if istream < 0:
    istart = pdf.find(startmark, istream, istream+20)
    if istart < 0:
        i = istream+20
    iend = pdf.find("endstream", istart)
    if iend < 0:
        raise Exception("Didn't find end of stream!")
    iend = pdf.find(endmark, iend-20)
    if iend < 0:
        raise Exception("Didn't find end of JPG!")
    istart += startfix
    iend += endfix
    print "JPG %d from %d to %d" % (njpg, istart, iend)
    jpg = pdf[istart:iend]
    jpgfile = file("jpg%d.jpg" % njpg, "wb")
    njpg += 1
    i = iend

This script works for my PDF files. Maybe it doesn't work for all, I don't know. PDF files are complex beasts. Your mileage may vary.

What I'd really like is a tool for exploring inside PDF files, so that I could see exactly what's going on in there. pyPdf is a start, but only scratches the surface of the kind of stuff I'd like to see...

« | » Main « | »