Lesson learned: Unicodifying request variables in Zope

April 16, 2009
3 comments Zope

This cost me a good hour of debugging so I thought I'd share it in case anybody else stumbles across the same problem. In the end, to solve my problem I had to add debug statements to StringIO.py to be able to find out where in my Zope Page Template a non-unicode string with non-ascii characters appeared and messed things up.

The error I was getting was this, which I suspect several Zope developers have encountered before:


UnicodeDecodeError: \
'ascii' codec can't decode byte 0xc3 in position 46: ordinal not in range(128)

The traceback only mentions files in the innards of ZPT of which none you can really do anything about. We all know that the key to avoid Unicode error is to be consistent. You can do this:


>>> '\xc3' + 'string'
'\xc3string'
>>> u'\xc3' + u'string'
u'\xc3string'

But you can't do this:


>>> '\xc3' + u'string'
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: 
ordinal not in range(128)

So, how did I get these non-unicode strings into my application in first place. Simple, I have a search parameter q and end up with a URL like this:


/bla/bla?q=régime

And the template had this little innocent piece of code:


<input tal:attributes="value request/q"/>

That's what f'ed everything up. So, I ended up having to add this:


<input tal:attributes="value python:context.unicodify(request['q'])"/> 

With this little helper function in the base class:


def unicodify(self, s):
   if isinstance(s, str):
       return unicode(s, 'utf8')
   return s

So, hopefully by writing this it will help someone else making the same trivial mistake and not wasting their evening with sporadic print statements all over their frameworks code.

Fujitsu FLEPia

April 14, 2009
0 comments Books

Rumor has it's it's $thousands still if you can even buy one. The juicy thing about these is that they promise around 50 hours of battery life since it takes 0 power to show a screen. It only takes power to change the screen which makes it ideal for reading e-books.

More in this annoyingly narrated video

TypeRacer Best online game in years

April 11, 2009
0 comments Misc. links

TypeRacer Best online game in years Typeracer is a game where you're competing with other typers by trying to write a text as quick as possible. One race takes about 1 minute.

Not only is it very well done and extremely "educational" but it's also fun. Unlike other games of the same genre you're not judged by your speed and accuracy. In Typeracer it's all about speed. Your mistakes is your problem since they stop your speed. It's hard to explain but it just works great. Play it and you'll see.

Having played this I realise that my spelling is absolutely terrible. I've grown to depend on the spellchecker in Firefox and when I'm doing programming I don't have to spell particularly much because I use auto-complete. At the moment I average around 55 words per minute which means I win quite a few races but still far far away from the high scores which start on 100+. Gotto to practice more!

Google Calendar, iCalendar Validator but not bloody Apple iCal

April 9, 2009
2 comments Web development

I've got a public ical at: http://m.fwckungfu.com/calendar.ics

This works in Google Calendar and in the iCalendar Validator but when I try to import this as a file in Apple iCal I get this message:

"Error subscribing to the calendar

Data downloaded from http://m.fwckungfu.com/calendar is not valid."

WTF? Who's ass have I not kissed to get this working in Apple iCal? Does anybody know any reason why Apple iCal is being so trixy?

Simple interface for Crosstips

April 8, 2009
0 comments Web development

Simple interface for Crosstips I've now made a simple interface alternative (not AJAX) to Crosstips. This one doesn't do any fancy AJAX to look up works and you just type in the unknown letters as a space (or a _ or a . or a *) so you don't have to know how many letters it is.

Here's an example

It feels refreshing somehow to go back from AJAX back to plain old GET requests. The best thing about this is that it will work on a mobile phone too. The way I've wired the page is so that if you visit the site with a mobile device (not an iPhone though) it will load the extremely cut down version of the layout which is more suitable for mobile phones. This means that people will be able to get unstuck doing crosswords in bed.

Head-to-head movie voting

March 31, 2009
0 comments Film

Head-to-head movie voting There's really no good way to explain what The Great Movie Experiment is other than just testing it. It's quite addictive and you somehow feel like you're contributing to something great.

"The gist is that we're compiling a list of movies based on head to head votes, matching them up in a giant round-robin tournament."

It's not really what head-to-head voting is about but there are times I would like a third button when a film sucks. Many times you're confronted by one mediocre film and one film which is only ok; then you're "forced" to vote for the one that is only ok.

Great work Matthias!

British or American English or just English

March 18, 2009
4 comments Web development

British or American English or just English My play site Crosstips.org is available in British English and American English. Obviously the difference is small but it's important.

What I've done is that if you're located in, say, France and visit the site it offers you the following language choices:

  • Svenska [goes to krysstips.se]
  • English (GB) [goes to en-gb.crosstips.org]
  • English (US) [goes to en-us.crosstips.org]

But if you're located in, say, England it only offers you the following language choices:

  • Svenska [goes to krysstips.se]
  • English [goes to en-gb.crosstips.org]

And likewise, if you visit the site from US computer you just get two options and it uses the en-us.crosstips.org domain. As an American or a Brit why would you be interested in the other English? I think this is a really good usability trick. It reduces the noise by removing options.

Truncated! Read the rest by clicking the link below.

Nginx vs. Squid

March 17, 2009
3 comments Linux

We all know that Nginx is fast and very lightweight. We also know that Squid is very fast too. But which one is fastest?

In an insanely unscientific way I added some rewrite rules to my current Nginx -> Squid -> Zope stack so that for certain static content, Nginx could go straight to the filesystem (where the Zope product holds the static stuff) to bypass the proxy pass. Then I did a quick and simple benchmark with ab comparing how to get a 700 bytes GIF image:


squid: 2275.62 [#/sec] (mean)
nginx: 7059.45 [#/sec] (mean)

Truncated! Read the rest by clicking the link below.

Too much Python makes Peter a shit Javascript developer

March 13, 2009
0 comments JavaScript

This murdered a good half hour of my time splattered with lots of alert() statements to debug. Basically, in Firefox you can do this:


var word = "Peter";
alert(word[1]); // "e" in Firefox, undefined in IE

This is the wrong way to get to character in a string in Javascript. The correct way is to use charAt() like this:


var word = "Peter";
alert(word.charAt(1)); // "e" in Firefox and IE

I don't know about the other browsers but finally Crosstips.org now works in IE7 too. I haven't even looked at it in IE6 and don't intend to either.