Productivity

I arrived in work on Monday morning (at a comfortable 11 o’clock as is my norm) to find that I was locked out of my Linux account. Another intern assured me that the administrators knew about the problem and were fixing it. So I settled into using Windows for the day. That slowed me to about a quarter of my usual work rate, particularly since that day’s work mostly involved pulling data from mailboxes and putting them into RDF. For the non-technical that means lots of text manipulation, and Linux has all of the text manipulation tools you could ever want. I actually did the work remotely on one of the servers so I could use nice things like grep and sed but the response was slow.

On Tuesday morning I stayed in there for a mere half an hour; long enough to find that the Linux login problem was unfixed and that the Windows policies are too strict and my home directory space too restrictive to let me work on Thunderbird under Windows. I wasted two hours on the bus to find this out. When I got home I emailed the administrator to make sure he knew about the problem. It turns out he didn’t, but he had it fixed within minutes and it only took him that long because he was at lunch when I emailed him. Gah. Oh well, guarantee it would work on Wednesday.

So today comes around and I’ve got much less done than I had hoped due to slow work on Monday and wasted time on Tuesday. So I stayed in later than usual, took a shorter lunch break—just long enough to eat—and things started to actually work. I didn’t get bogged down in stupid errors or dead ends or bad architecture decisions of the kind that have drained hours from my work thus far. This gave me a new motivation, a desire to get really stuck in coding. If it keeps up tomorrow I think I’ll have most of this week’s stuff done by Friday morning’s status check. Then I get a whole new pile of features to push through.

Hail to the Chimp

When idle channel hopping brings you to a video of a chimpanzee doing karate you know the universe is stacked in your favour. For those who have never seen this before, and those who thought they’d never see this again, here’s Karate Chimp.

Diduh-Diduh-Diddledih-Duuh

That title is meant to be the Countdown clock reaching zero, which it unfortunately did for presenter Richard Whitely earlier today. I’m not entirely sure what to say about this except that I’m surprisingly put out. Whitely’s humour was built out of bad puns; his presenting style was like your dad trying to be funny when he meets your friends for the first time; he laughed at his own jokes even when no-one else did. And yet for some reason we all liked him.

I used to watch Countdown every day before Start Trek. I was continually annoyed that I could only get a six when others got a seven, and occasionally chuffed at getting the numbers when Carol couldn’t. I remember the first time I got the conundrum. The word was ‘gladiator’. I guess I could make some poetic comparison of Richard Whitely and the gladiators of Rome, riding a chariot of literacy and fighting off the lions of mid-afternoon boredom. But really he was just a nice guy doing a job he enjoyed. Which I guess is enough.

Make Some Science

Take the MIT Weblog Survey If you have a blog, go take the MIT blog survey and help make some science. Also check out their nifty form interaction. This is online research by people who know what they’re doing.

Reading a Numbered Email from an Mbox in PHP

We’ve settled on the name Email Activity Assitant for the project I’m doing this summer. Inspired by the name, I’m going to try to find time to work a helpful paper-clip character into the interface. Like Clippit, only more dynamic and proactive.

Today I had to write a Web application that sends a series of pre-stored emails. We’ll use it to test our application’s ability to classify emails as they arrive. This will save us the embarrassment of ordering five hundred Lindsey Lohan Original CD Clocks, one for each time we have to test the mail grouping and activity-identifying algorithms.

We have a few sets of emails in a series of mbox files. Mbox is the mail storage format used by many mail applications, including Thunderbird. It’s exact specification can be found in the mbox man page but the gist is easy: Emails are stored one after another, each one preceded by a line beginning "From " (note the space; quotes not included, obviously) called From_ lines, and succeeded by a single blank line.

My application had to extract each email in turn, given only the path to the mbox file and the number of the required email. Here follows my solution. It’s somewhat broken, as I’ll explain below.

The basic idea is that we really don’t want to have to read the entire mbox file, split it into seperate emails, and then count up to the one we want. That’s just bad. Imagine trying to get the first email, a one liner asking if you’re free on Thursday, from an mbox representing the ensuing multi-week life-altering intellectual and spiritual debate about the nature of freedom and, for that matter, the nature of Thursday. So we only want to read up to the end of our email. PHP doesn’t have any built-in methods for reading a file a line at a time, so I’ve gone for a different approach.

We read in the file a chunk at a time; I’ve hardcoded the number 4096 bytes because it’s nice and round. If you don’t think that 4096 is a round number you probably shouldn’t be reading this. I chose 4096 since it’s not too large — I don’t want to read much more than I have to of the file — and it’s also not too small — being too small would be catastrophic for this code for reasons noted below. Anyway, once we’ve read the first chunk we split it on any occurance of /\nFrom [^\n]*\n/. I would have preferred /^From .*$/ but PHP’s preg_split doesn’t treat ^ and $ as beginning and end of line unfortunately. Instead it treats them as beginning and end of string, which is useless to us here.

If our required email hasn’t been reached yet — say we’re looking for the fourth and only three emails (or parts of emails) form our chunk — then we read the next chunk and continue as before. If our email, or part of it, is in the chunk we just read then we extract it. In this case we will have to read the next chunk only if we didn’t get the complete email from this one, ie. if it spans the boundary. On the next chunk we’ll just take the first part, up to the first From_ line if there is one, and append it to what we have. We keep doing this until we get a chunk with the beginning of another email or an end-of-file.

We now have our email. We also have some additional information we could use. If we hit and end-of-file we might want to do something to indicate there are no emails remaining. If we hit the EOF before finding our email we could throw a little hissy fit; alternatively we could show a helpful error.

Here’s an edited version of the code I used. $msgnum is the number of the email we want to retrieve. $mbox is the path to the mailbox.

$fHandle = fopen($mbox, r);
$currentMail = 0;
$mailContent = "";
while (!feof($fHandle) && $currentMail <= $msgnum)
{
	$chunk = fread($fHandle, 4096);
	$mails = preg_split("/\nFrom [^\n]*\n/", $chunk);
	if ($currentMail == $msgnum)
	{
		// we're in the middle of reading our mail so append the first part of the chunk:
		$mailContent .= $mails[0];
		$currentMail += count($mails) - 1;
	}
	else if ($currentMail + count($mails) < $msgnum)
	{
		// we haven't reached the mail we want yet so move on:
		$currentMail += count($mails) - 1;
	}
	else
	{
		// our mail starts (and might end) in this chunk:
		$offset = $msgnum - $currentMail;
		$mailContent = $mails[$offset];
		$currentMail += count($mails) - 1;
	}
}
if (feof($fHandle))
{
	// you might want to add some code here to be run if your email was the last one in the mbox
}
fclose($fHandle);
// $mailContent contains the contents of your email, if it was found

I promised I’d tell you why this method still sucks. You might have spotted it. The most important thing, the show-stopper if the bug is ever met, is that if a chunk contains some but not all of a From_ line, that line will be lost and the two emails it separates will be considered to be one. The chance of 20-ish character line being broken when we split into 4000+ character chunks is small but significant. This bug wouldn’t be acceptible in a live application; the only reason I let it slide is that it would be easier for me to just pad the offending mbox if we ever see this bug. It’ll show for, on average, one in 200 emails so we might well see it at some point in our testing.

The second bug, not so catastrophic, is that this method leads to a Shlemiel the painter’s algorithm. If we want the nth email we read the first n emails. So when we put this function into an application which goes through each email in turn, this is what happens: We read the first email, we read the first two emails, we read the first three emails, etc. It’s like a road painter whose work rate decreases dramatically as he gets further and further from the paint bucket. In this case the function calls are all made on seperate page views so there wasn’t much I could do short of saving a pointer to the last read chunk in a session variable. That would have been overkill, but it would be an important improvement if our mboxes got quite large. I tested on a 30+ message mbox, which looks like an extreme case for our purposes, and saw no noticeable slowdown on the later emails so I don’t anticipate this being a practical issue in this case.

Highly-Indebted Poor Countries Initiative

Under the deal, the World Bank, the International Monetary Fund and the African Development Fund will immediately write off 100% of the money owed to them by 18 countries – a total of $40bn.

—BBC story

Maybe someone will be along in a minute to explain to me why this isn’t as good as it sounds. Maybe most of the countries’ debts aren’t to those organisations; maybe there are countries that should be included but aren’t. But until such a person comes along to tell me that this isn’t great news, I’ll continue to think that it is.

Bob Geldof was on Friday Night with Jonathon Ross last night, promoting the Live8 concerts. I can’t see how anyone could fail to be inspired by him; he’s such an insightful speaker. While I generally consider national boundaries to be an artificial—though possibly necessary—divide, Sir Bob is one of few Irish people who would make me proud to be associated with them, if only by that tenuous link.

Your Ma’s Theorem

Fermat’s last theorem states that the equation xn + yn = zn has no solutions in integers x, y, z, and n where n > 2. Try this in a calculator: 178212 + 184112 = 192212.

That equation was generated by a program written by David X Cohen, at the time a writer for The Simpsons, subsequently executive producer of Futurama. He put it in the background of the three-dimensional realm in the episode Homer3. It’s not a counter-example to the theorem—which at the time hadn’t been completely proved, but had been proved up to a certain exponent (much higher than 12)—but it will look like one on any calculator that truncates numbers after ten or fewer digits.

Found in a mathematical interview with Cohen.

IBTS Wants You

The BBC is runnning a story about Irish hospitals cancelling non-emergency operations. Apparently new restrictions on who can give blood mean that the Irish Blood Transfusion Service is getting fewer donations. Here’s an idea. Stop being so fucking homophobic. No, the new regulations are nothing to do with homosexuality. But they find a huge number of new donors by removing this from their safety rules:

Never give blood if:

  • …
  • You are a male who has ever had anal or oral sex with another male, even if a condom or other form of protection was used.
  • …

Note that that same list doesn’t mention anything about having had unprotected sex with a person with HIV in the last fifteen minutes. As long as you’re not both men it’s fine, I guess. Actually, I’m pretty sure they do have a rule about that somewhere, but it says that you shouldn’t give blood for one year afterwards.

So yeah, long story short, I’m pretty disgusted with the IBTS. And I have been since the first time I gave blood. But here’s something to ponder: I still give blood. Why? Because it’s not the dumbasses running the service that really matter. It’s the people in hospital who will die if they don’t get some blood. I’m not going to sit on the sidelines making idealogical arguments when people’s lives are on the line. I’m not Damon Albarn.

South Park Did It

Aparently the The Simpsons movie is in pre-production. I feel like I’m sitting at the bedside of a dying friend as he slowly (and I mean slowly) fades away, a shadow of his old self. The Simpsons is the Terry Schiavo of television, and frankly Fox just needs to pull its feeding tube.

I’ll watch the movie. Because I have to. But afterwards I’ll watch South Park: Bigger, Longer and Uncut and Beavis and Butthead Do America and think of what could have been if The Simpsons movie had arrived eight years ago.

Water RAID

One of the biggest challenges to a geek is to try to explain inherently complex technological subjects in way that lay people will understand. When someone comes up with a truely illuminating description I think it’s worth spreading it. It’s in the spirit of allowing a wider audience to appreciate the utility of RAID that I refer you, via Howl @ The Moon, to Smurf Explains RAID Technology.