I've been doing a lot of MySQL hacking in Python. And like all Python projects I do, I start by stuffing data into anonymous lists and remembering "oh yeah, foo[3] is the name of the wine, and foo[1] is the year". This doesn't scale well, and fortunately MySQLdb has a better way.
import MySQLdb, MySQLdb.cursors
db = MySQLdb.connect(db="wine")
c = db.cursor(cursorclass=MySQLdb.cursors.DictCursor)
  select name, type, year from wine
  where color = %(color)s and year < %(year)s
  """, { "color": "Red", "year": 1972 })

for row in c.fetchall():
  print row['year'], row['name']
The code snippet above is using dictionaries everywhere; both forming the query and handling the response. This lets me name parameters so that if I add a new condition to the where clause or a new field to the select, the rest of my code doesn't break.

I'm taking advantage of two MySQLdb features that go beyond the standard Python DB API. The first is simple; the magic query construction of execute() handles dictionary style substitution just like you'd expect.

The second is more subtle. MySQLdb supports different cursor classes that extend the basic "tuple of tuples" datatype you usually get from fetchall(). I'm using DictCursor, which builds a dictionary from the names in the description field of the cursor. There are also server-side cursors for efficiency with large result sets. It's all implemented via mixins for flexibility.

I'm particularly looking forward to Andy's 2.0 plan to have a "row object that can behave like a sequence or a mapping (dictionary)", giving you the best of both worlds. Combine that with iterators and you could really have something.

PS: if you search for MySQLdb docs, you quickly land at the obsolete module docs. I used these docs for two years! The MySQLdb project has moved to SourceForge and the MySQLdb docs are nicely hosted there.

  2005-01-31 17:08 Z
I had a nice dining surprise last night. After spending twenty minutes driving around the Inner Richmond looking for a place to park, we gave up and drove back towards home, me grumbling about how SF is choking on itself. On the way back we stopped by Chou Chou, a newish restaurant near the Forest Hills Muni stop.

And miraculously they had a parking place, and a table. And best of all, it's a a great little neighbourhood bistro. Friendly, busy, nicely decorated, good staff, good wine list, great menu. I had escargot (tender, and with a flavour more interesting than just butter and garlic) and a seafood stew (fantastic, quite spicy: almost like an etoufee). Ken had foie gras mousse and a lamb daube which looked rich and delicious. Happy all around.

My part of town is outside what most people even think of as San Francisco. I like it that way, quiet and relaxed neighbourhood. But sadly lacking in good restaurants. Chou Chou is a nice addition. They were packed, too, so I hope they are doing well.

  2005-01-30 16:12 Z
I usually pronounce "URL" like a word, rhymes with "hurl". Most of my tech geek friends do too, probably because it's the shortest pronunciation. That means I say "an URL". But if you pronounce "URL" "You Are Ell", then it becomes "a URL". Which is more common?

"A URL" is more popular. Google has 8,320,000 results for "a URL" vs. 658,000 for "an URL", the new MSN search is 5,086,962 vs. 1,107,446. Yahoo search seems to treat the two queries the same.

These are rough estimates, but it gives you an idea. Too bad search engines are case sensitive: curious the difference between "URL" and "url".

  2005-01-30 16:02 Z
I don't talk much about work on my blog, but I wanted to brag a bit. My project at Google launched! I've been working on an API to AdWords that allows advertisers and third parties to write programs to manage AdWords campaigns. Lots of interesting work, I plan to summarize some of what I've learned at the ETech conference in March. See the AdWords API Blog for occasional updates.
  2005-01-28 03:15 Z
AOL announced they will no longer be carrying Usenet (MeFi thread). What's saddest about this announcement is how few people will care. Usenet is dead.

"Imminent death of Usenet" was a big joke in the heydey of Usenet, in the early 90s. The store and forward network obviously couldn't scale and it had no purpose you could justify on a budget, and yet we all knew that it was hugely vital to the growth of the Internet culture. So it survived.

Then, in the mid 90s, it became irrelevant. We got an explosion of communication technologies: web sites, email lists, message boards, instant messaging. And the culture became diluted as the Internet stopped being the elitist haven for a few nerds.

And so Usenet died, but no one really noticed. There are still a few useful pockets, and a couple of vestigal communities. But Usenet's main purpose these days is as a giant distribution network for pr0n, music, and warez. The community is gone, subsumed into a larger Internet.

-- ogicse!reed!minar
  2005-01-26 16:18 Z
The referer spam on my blog has gotten awful. Twenty sites a day show up as supposedly having linked to me. Somehow I don't think prozac.best-buy-site.poker.xxx.tk is really legit, though.

One of the spammers is adminshop.com. They advertise a product named "Reffy", which for $50 (PayPal, natch) offers "mass referrer marketing". Even better, it "comes with a pre-generated list of 3047 active blog websites". Including, presumably, mine. I guess this is my punishment for having an automated referer backlink two years ago, before nofollow.

Dear Reffy author. Your use of my site is not authorized. And because you're advertising access to my site as part of your package, I expect a license fee. I'm a reasonable guy: $5 per sale will do it. Just email me for payment instructions!

  2005-01-23 22:09 Z
Ken does all the cooking. I feel guilty about that, so from time to time I try to make something myself. And having seen Marc turn out an excellent home-made pizza, I figured I'd try making one.

Following my new life of buying everything through Amazon, I set myself up with a pizza peel, a baking stone, and a copy of the pizza nerd book American Pie. And read, and it seemed simple enough.

And so it mostly was. Two things tripped me up. I have no intuition for bread dough, so it was a surprise to me that it's not like clay. You can't just fold it up and start over; the gluten structure won't let you do that. Scratch one doughball. The other problem is that Bad Things happen when the dough sticks to the peel. Note to self: more flour, and don't press down on the raw pizza, ever.

End result was fairly good, especially the second one. Not great, but fairly good. More practice needed.

  2005-01-23 21:10 Z
I played Half Life 2 for a bit, then got tired of it and forgot about it for other things. John Sullivan's letter in the Feb 2005 PC Gamer sums it up nicely for me:
Every puzzle, every stop point in the game, has one solution. There's only one way to play the game, only one way to interact in any given circumstance. ... City 17 is unpopulated and unexplorable. NPCs are non-existent except for the indestructible main characters ... There's a lot to like or admire ... but beyond that, it's just a console railed shooter.
Half Life 2 is a good rail shooter with superb art direction. But in the wake of games like Deus Ex or Far Cry it's hard for me to enjoy a game with no real player choice.
  2005-01-22 22:41 Z
I'd forgotten how nice old game music can be. Great crunchy synth sounds and surprisingly lyrical melodies. Check out Silent Light, from Chrono Trigger: ogg or original RSN.

This music is still available thanks to the preservation efforts of the emulator scene. Folks have ripped the music code from the cartridges and published it as files: rsn or spc for SNES, nsf for NES. So you get the original music, not a recording. You'll need a player / sequencer. I recommend NotSoFatso for NES and SNESAmp for SNES, both WinAmp plugins.

Archives of music are readily online. Zophar has a good store of NES music (beware: popups), and SNESMusic is great for SNES. There's a lot of music out there. Some useful guides are Skytopia's list and cly5m's list. I like Earthbound, Chrono Trigger, Super Metroid, and the original Zelda.

  2005-01-21 23:04 Z
When is ten actually eight? When it starts with zero.
$ python -c 'print 010'
Base 8 notation is a bad left-over feature from the old days of 12 bit words when programmers only had eight fingers. Yet it persists in our modern computing environments, where almost everything has a magic rule that if a number starts with 0, it must be in octal.

The problem is people sometimes left-pad numbers with zeroes so they line up nicely. For instance, the wonderful ISO date format is written like 2004-09-15. If you're not careful and you try to convert that '09' into an integer, you may get an error because 9 is not a valid octal digit. It's worse with IP addresses: what does '' mean? Well, that depends on who is reading it.

Octal is the cause of all sorts of contemporary bugs for modern ten fingered programmers. Even Javascript has this holdover from the old days, causing confusing errors when users type numbers into fancy forms.

The only place I've ever explicitly wanted octal was setting file modes with chmod. Because Unix permissions come in groups of 3, base 8 is convenient shorthand that's now deep in my brain. Ironically, chmod doesn't require a leading 0. chmod 600 secret.txt is implicitly assumed to be 0600, or 384.

  2005-01-21 18:03 Z
I love The Amazing Race. Like Survivor, it's a well edited show. And even more than Survivor, it enables us to indulge our base desire to watch people in all their awfulness.

This season has been blessed with a truly hideous couple, Jonathan and Victoria. They're billed as "entrepreneurs", always a bad sign. Jonathan is actively abusive, belittling and physically hurting his wife. And Victoria is whiny and useless, although she gives the abuse right back. It's hideous and hysterical and entertaining all at once.

Of course these portrayals are fiction. The situation is artificial and the reality is heavily filtered through the editing process. Only it turns out that in real life, they're just as awful! Yes, Jonathan and Victoria have a web site. And while they were nasty and mean on the show, in real life they're SoCal nightmares.

Jonathan runs a day spa that "creates the physical manifestation of an epiphany". But really he's a film maker. Victoria is a Playboy model (NSFW). But really she's an artist, making paintings of Beanie Babies. From the bios we learn that Jonathan's favourite colours are "wheat and hunter green" and the question he's asked most is "are you really 42 or 24?". Victoria's ideal date is "sushi, sex, and starbucks" and thinks "the secret to success is always choosing the same thing". Remember, they chose to write these things about themselves. No editors.

This is the joy of reality TV: you can't make this stuff up. On the TV show they're awful and abusive, caricatures of aggressive entrepreneurship. And on their web site they're awful and self indulgent, caricatures of SoCal sybarisis. I give them credit, they've constructed an amazing image.

  2005-01-20 17:32 Z
Looking back on my trip to New Zealand, Queenstown was my favourite place to tourist. That's easy to say, since Queenstown is the tourist center of NZ. But it's a lovely spot in the middle of the Southern Alps and the tourist infrastructure is welcome. Lots to do.
Queenstown is the center for "adventure tourism". Bungee jumping was invented there, paragliding, etc. Not for me, but I did enjoy the jet boats, rafts that draw only three inches of water and go 50 miles an hour. Kind of like a roller coaster on an alluvial river. And Ken and I enjoyed the luge on the hill above town. Nice view and goofy fun.

We also took a great helicopter tour to Milford Sound on the west coast: beats the 10 hour bus ride, and the views of the glaciers from the air are amazing. And we enjoyed a leisurely boat cruise on the Earnslaw, a steamboat that was improbably built, disassembled, carried by train, then reassembled to be trapped forever in the lake. And there are a lot of wineries to visit south of Queenstown in Central Otago, an up and coming pinot noir area. Akarua was my favourite.

Queenstown also has the best restaurant concentration we found in New Zealand. We had a fantastic tasting menu at Wai, a great seafood place. We were somewhat disappointed by The Bathhouse; eccentric service, OK but not great food. But the best dinner we had was at The Bunker, a tiny little hidden place with amazing preparation. I ate a lot of lamb in NZ, this was by far the most succulent and with the best flavour.

We found most of these things thanks to the help of Lavinia, our host at the Evergreen Lodge. A wonderful little four room hotel about two miles out of town, up on the hill with a great view of the lake. Great hospitality, the kind of place where you enjoy talking to the host and the other guests. It was good to be out of Queenstown itself, much quieter, but still a quick cab ride into town for dinner. Well recommended.

  2005-01-18 16:43 Z
Many thanks to Uche for his thoughts and code responding to my frustration working with XML in Python. If you're reading this because you want to write good XML code in Python, read his stuff! He knows much better than I. And he gives clear guidance: use his Amara if you want something Pythonic that can deal with XML.

But reading Uche's posts confirms my main point. There are too many XML choices in Python. And the obvious ones aren't right. Apparently PyXML isn't what I'm supposed to be using (despite it being the default when I type import xml on my Debian box), and if you use it the way the docs say to you're wrong. Urgh!

And while I like what Uche says about Amara, is this the easy way to say "parse an XML document"?

from amara import binderytools
rule = binderytools.preserve_attribute_details(u'*')
doc = binderytools.bind_file("foo.opml", rules=[rule])
He explains why all this is necessary for this example (Amara by default doesn't support XPath attributes), but it's just this kind of complexity that frustrates me. Python's strength is that there's a clear, obvious way to do simple things. But not with XML.

See this response from Uche, with lots of good samples and comments.
  2005-01-15 16:47 Z
I'd thought Queer Eye for the Straight Guy had passed entirely into irrelevance, but the third season premier for Ray S. (ep. 153) was excellent and heartbreaking. The episode is about a 37 year old soldier about to be shipped out to Iraq. The Fab 5 swoop in to lift him and his woman out of their bleak apartment lives, help them quickly get married, and set up Ray's wife for the year and a half he's about to be gone.

Queer Eye is usually a silly show, with howlingly bad fashions (from both sides), Rip Taylor dialogue, trivial consumer indulgences, and some sappy sentimentality that doesn't work. But here we have a guy who's about to go to Iraq, some truly generous and thoughtful gifts, and an emotional honesty that's rare on TV. Maybe the show has some life in it yet.

It seems appropriate that the show was not overtly political. There's one small "gays in the military" joke, but no comments about what's going on in Iraq, just lots of sympathy for the situation the soldier is in. But you can't watch the human scale of this, this poor guy being taken away to fight a disastrous war started under false pretenses, and not get angry. At least, I couldn't. But the Queer Eye team was smart and stayed away from that. Let their viewers draw their own conclusions.

  2005-01-15 16:20 Z
I was having trouble ripping some badly manufactured CDs on my new CD drive, a Sony DW D22A. I think 48x is just too fast, particularly since the CPU can't possibly keep up and so things buffer in weird ways. But slowing the drive down to 12x seems to have fixed things nicely. The rip/encode cycle is just as fast, but it's quieter and more reliable.

AudioGrabber has a bug where you have to open the "Settings" dialog for each rip to have it set the CD speed. And switching from "buffered burst copy" to "unbuffered burst copy" helped reliability with no speed cost.
  2005-01-14 16:54 Z
I hate working with XML. It's easy to extract data from simple text files or CSV files, but XML is all nested, and has entities, and lots of pointy brackets. Regexp just doesn't cut it, you really need an XML parser. And for some reason Python is not so great at XML.

Python has too many XML choices. There's the stock Python install, which barely does anything. Then there's what you probably should use, PyXML, which has an ugly hack to confusingly install on top of the default Python libraries. But if you follow the advice of Python's most visible XML expert, Uche Ogbuji, you may think there's something wrong with PyXML and install 4Suite instead, which is the same as PyXML only different. Or should you use Amara instead? Then there's ElementTree which is brilliantly fast and simple to use, but limited, or xmltramp, which is even more hacky. On the other extreme there's libxml2, which is fast and powerful but has an awful API.

Mind you, this is all for the basic stuff, like parsing XML. There's lots more Python XML options too. But what's missing is a clear single simple library to use. PyXML seems the most standard, but it seems very slow and it tries to be more DOM-like than Python-like. I hate DOM.

All of this is a long-winded preamble to my attempt to do something simple with XPath in Python.

More inside ...

  2005-01-14 15:32 Z
I listen to an audiobook a month. The online audiobook market is being sewn up by audible.com, which has aggregated lots of books and periodicals and presented them in a uniform web site. You can buy recordings outright for regular audiobook prices. Or for $15 a month you get one book and one subscription a month.

Sounds pretty reasonable, except.. you don't get MP3, you get some screwed up DRM encumbered format that only plays on certain devices. You're allowed to burn a CD, but you can't turn the recording into an MP3.

Audible uses security technologies, including encryption, to protect purchased programs. While the more typical MP3 files contain the same kind of audio data, they are not protected in this way. You will not be able to convert the Audible format to MP3 because of this encryption. The measures taken by Audible are required to protect both the intellectual property rights of our Content Providers as well as the Authors. Audible's secure distribution system prevents a customer from passing along duplicate digital audio files to another listener.
Why would I want to buy some crappy format whose main purpose is to restrict my usage? After 10 minutes on the web site I still can't figure out whether your book-a-month is owned by you to listen to in perpetuity or if you only get to listen to it for the month you rented it. Who wants the uncertainty? I'm going to keep buying CDs and ripping them to MP3 myself.

Todd tells me the books apparently don't expire.
  2005-01-08 17:09 Z
As rendered by a T616 cameraphone.
  2005-01-02 23:59 Z
Enron style accounting comes to the White House. The chicanery is all about "meeting" a Bush campaign promise to halve the deficit. How do you do that?
Administration officials have decided to measure their progress against a $521 billion deficit they predicted last February rather than last year's actual shortfall of $413 billion. By starting with the outdated projection, Mr. Bush can say he has already reduced the shortfall by about $100 billion and claim victory if the deficit falls to just $260 billion. ...

Administration officials are also invoking optimistic assumptions about rising tax revenue while excluding costs for the wars in Iraq and Afghanistan as well as trillions of dollars in costs that lie just outside Mr. Bush's five-year budget window. ...

The budget is also expected to exclude Mr. Bush's goal to replace Social Security in part with a system of private savings accounts, even though administration officials concede that such a plan could require the government to borrow $2 trillion over the next decade or two.

You start with a fake target, then you exclude enormous costs that are a direct result of your policies. I really don't understand how this kind of lying happens in plain sight with no one caring. I guess the only accountability is the voters, and the electorate ain't too good at the numbers.
  2005-01-02 16:24 Z
I had a great time at the circus yesterday, the Pickle Circus, a new-style circus that goes back 30 years in SF. Like the famous Cirque du Soleil, the Pickle Circus focusses on artistic presentation and human performance, eschewing the creepy animal acts and tacky carny crap that's the usual American circus experience.

My favourite act was Sandra Feusi and Sam Payne's vertical tango, a Chinese pole act that was astonishingly graceful and sexy. Chris Lashua's German Wheel act was also excellent, and I liked the juggling and the contortionist. This review describes the show well. Alas, you probably missed it: Sunday the 2nd is the last performance.

Thanks to Seth whose comment got me to see the show
  2005-01-01 23:51 Z
The Dell 2001FP is good hardware. Nice, simple 20" LCD, 1600x1200 @60Hz. Great picture quality, very bright, mine doesn't even have any dead pixels. And good features, it rotates to portrait mode, has a 4 port USB hub, takes composite and S-Video inputs as well as DVI and VGA and can even do picture-in-picture display. $800 retail, although you can usually get a better deal out of Dell. Dell has always sold good displays. Somehow they seem to do more than just rebrand someone else's displays.

I had held off getting an LCD because I was afraid running a game at lower resolution and scaling it up would be ugly. But it's not bad, at least with the Radeon's video card scaling. Slightly blurry, but the sharper picture and colour from the DVI more than makes up for it. And the pixels are fast enough (16ms) to genuinely draw 60 frames a second; more in this AnandTech preview for how significant that is.

  2005-01-01 23:21 Z
For New Year's last night I stayed at the Harbor Court Hotel, a medium sized hotel on the Embarcadero just south of the Ferry Building in the YMCA Building. Nice place. Not cheap, and the room was pretty small, but the location is fantastic and the place was good. The top floor view of the Bay Bridge was great.

This hotel is part of the Kimpton Group, a nice line of modest sized upscale hotels. I can also vouch for the Hotel Vintage Plaza in Portland and the Pacific Palisades in Vancouver.

  2005-01-01 22:53 Z