Friday, January 8, 2010

stackoverflow.com, in which we circumvent Django's template system

The whole separate-code-from-presentation imperative that inspired Django's template system ... let's say I have my doubts. From my experience, Django's particular approach to templates diverts much too much of my attention to circumventing its limitations.

Update:

In response to Jake's observation that "variables should always be set in the view, not the template," in general, I agree.

But the specific counter-example I'll supply is the very common title example. Let's say you'd like to create a base template like so:

<head><title>{% block title %}</title></head>
<body>
<h1>{% block title %}</h1>
...


And then you inherit from that template:

{% block title %}User Feedback{% endblock %}

This is a very simple, commonplace scenario, and it's simply unsupported. Django will error out on the second call to {% block title %}, and I think that's dumb.

Monday, January 4, 2010

Python's Zipfile library

So you want to generate a big bundle of files all at once, and send 'em in one convenient package? Python's ZipFile library to the rescue! You can create each file, write it to a zip, then send the whole mess over the wire without ever touching the disk! (StringIO is pretty essential as well.)

Ahh, but wait: anyone unzipping your files will find each file marked unreadable/unwritable/unexecutable. And the Python ZipFile docs have zilch to say about it.

This turns out to be the requisite incantation, care of Stackoverflow:

info = zipfile.ZipInfo(name)
info.external_attr = 0777 << 16
zip.writestr(info, bytes)

Obvious, no? Of COURSE you need to bitshift the permissions by 16! (File perms clearly occupy the first 16b of an int, which might even be the standard on Unix, but that doesn't excuse the Python docs' utter silence on the matter.)

Thursday, November 19, 2009

One last thought on Wavelet image sharpening

The GIMP's plugin documentation for the Wavelet sharpen plugin is interesting:

"The wavelet decomposition of an image results in multiple images with different frequency content. When amplifying the high frequency parts the recomposed image appears to be sharper than the original one. That way the frequency which should be amplified most can also be selected and a given unsharpness in the original image can be taken into account."

That matches with Tom Crimi's description of Wavelets as vaguely similar to the uniquitous FFT.

Wednesday, November 18, 2009

Sharpening photos like they do in CSI

There's a recurring joke on nerd hang-outs like reddit about the "enhanced photo" plot device on dramas like CSI: a grainy, low-resolution image is zoomed and sharpened to bring out impossibly small details -- finding the reflection of the killer in a distant chrome hub-cap, that sort of thing. It's obviously stupid.

But after playing around with The GIMP's "Wavelet enhance" filter, I'm really impressed -- it really seems to bring out fuzzy details:




(My best effort at cleaning the image up with contrast & brightness on the left; the wavlet-enhanced version on the right.) Clearly, it can't restore missing detail, but it seems to do a good job of improving the contrast around edges / features.

Monday, November 16, 2009

Firefox can't print, and what to do about it.

Say you've got a big, information-rich, scroll-heavy page, with annotations and doodads located a long scroll off the screen. But say it works: you can scroll around and find what you need; it's a solid app.

Firefox won't be able to print it -- not usefully, at least, b/c Firefox will print only to the right edge of the visible page, thereby limiting you to the width of your physical screen. Creating a new paper-type, some monstrously wide fictional ur-sheet, won't help things either. So what's a boy to do?

Enter CutyCapt, a WebKit-based command-line tool that renders to PNG. (Or PDF, or whatever file format your heart desires.) Better still, it will execute any Javascript on the page, so pages that are essentially blank until the JS runs will render just file.

You'll need Xvfb, the X virtual framebuffer, if you want to run it on a headless box, but Xvfb is a little miracle in itself.

Tuesday, October 20, 2009

Microsoft Outlook 2003: The Worst Program of All Time

I'm forced to use Outlook 2003 by a clueless IT department -- forced, in fact, to sit an entire separate ThinkPad next to my iMac just for email, since a Mac can't possibly sit on the trusted network. It's a security threat, after all. Unlike Windows. Which is secure.

Having used GMail for years, I'm shocked at the sheer unfriendliness of Outlook:

Searching takes minutes, and only barely works.

Perhaps due to way Exchange is configured, Outlook won't auto-complete contacts by first name, only last, and even then it only auto-completes names from my personal contact list, not names in the Exchange directory.

Threading. THREADING. I had threading in 1998 using Mutt, a command-line email client. GMail has had threading since day 1. An email and it's replies form a dialog, obviously, and Outlook's failure to group those emails into a thread makes it about as useful to me as my Nokia 3510's SMS functionality was.

Friday, July 31, 2009

On technolgical improvements in lighting

Two out of three light bulbs burned out in our new lamp, so off to the store we went. We wound up with two different bulbs: an LED ($9), and a compact fluorescent ($7). This lamp has 1 incandescent bulb, 1 CFL, and 1 LED; would anyone care to guess which is which?


Wikipedia's entry on LEDs has an enlightening explanation of the temperature of such white LEDs; the phosphor that converts the LED's blue light into white light doesn't do a very good job of it, and much of the blue leaks out -- this graph captures the effect nicely.