My first YSlow Grade A (100)!

December 17, 2008
6 comments Django

My first YSlow Grade A (100)! Just put in the remaining necessary cache headers on my kungfu club mobile site and I ran a YSlow! test on it and it got a 100! I've never had that before.

Admittedly, this is like cheating since the site is for mobile use it's kept very very simple and has no external dependencies so no need for a CDN. Getting a Grade A on YSlow! is very hard if the site needs to really blink. This one is built dedicated for small mobile phone screens on slow connections. I think now the bottleneck isn't in the rendering but in the connection latency for the remote database.

It also got a 4.9 (max 5.0) on ready.mobi which is the highest I've ever achieved there.

Truncated! Read the rest by clicking the link below.

Wing IDE versus Jed

December 11, 2008
4 comments Linux

For the impatient: Jed and Wing IDE are programming editors I use for my Python, Javascript, HTML, CSS editing. One is ultra-light, fast and simple. The other one is very feature full, commercial and slow (in comparison to Jed).

I've been using Jed now for several years on Linux. It's an "Emacs clone" 1 in that almost the same key bindings you have in Emacs work in Jed. A few weeks ago I started using Wing IDE 3.1 instead to see if I could learn to love it. I got a professional license as a gift for participating in the PyCon 2008 sprint by Wingware (the company behind Wing IDE). As of yesterday I've gone back to Jed but I haven't uninstalled Wing yet. Here are what I've learned from using both quite a bit. Note, I'm not comparing things that they both do equally well such as macros, community support and block indentation.

Truncated! Read the rest by clicking the link below.

Nasty surprise of Django cache

December 9, 2008
10 comments Django

Caching in Django absolutely rocks. Much so because of its simplicity which lowers the threshold to the height of a snowflake. However the simplicity can cause some unexpected surprises.

I ran my site and clicked around until I found a bug I wanted to fix. Then I wrote the test and ran the testrunner but I was getting some really weird behavior. The view looked like this:


@never_cache
def club_page(request, clubname):
   club = _find_club(clubname)
   if club is None:
       raise Http404('Could not find the club')

   classes = cache.get('club_page_classes')
   if classes is None:
       classes = ClubClass.objects.filter(club=club).order_by('start_time')
       cache.set('club_page_classes', classes, CACHE_TIMEOUT)
   ...

What happened (and what took me a while to figure out) was that the memcache was still active and being used when running the tests since it's only within the hour that I started running the tests with completely different data. Here's how I solved the problem:


class ViewsTestCase(unittest.TestCase):
    ...
    def test_club_page(self):
       """ test rendering the club page """
       cache.delete('club_page_classes')
       ...

There must be an easier way to patch the testrunner to reset all cache before running the tests. Anybody?

bool is instance of int in Python

December 5, 2008
15 comments Python

I lost about half an hour just moments ago debugging this and pulling out a fair amount of hair. I had some code that looked like this:


result = []
for key, value in data.items():
   if isinstance(value, int):
       result.append(dict(name=key, value=value, type='int'))
   elif isinstance(value, float):
       result.append(dict(name=key, value=value, type='float'))
   elif isinstance(value, bool):
       result.append(dict(name=key, type='bool',
                          value=value and 'true' or 'false'))
...

It looked so simple but further up the tree I never got any entries with type="bool" even though I knew there were boolean values in the dictionary.

The pitfall I fell into was this:


>>> isinstance(True, bool)
True
>>> isinstance(False, bool)
True
>>> isinstance(True, int)
True
>>> isinstance(False, int)
True

Not entirely obvious if you ask me. The solution in my case was just to change the order of the if and the elif so that bool is tested first.

Finally got rid of the system beep

November 22, 2008
2 comments Linux

I have a Thinkpad T60p which is working really well for me. On it I've run various flavors of Ubuntu and as of a couple of weeks ago I put on Ubuntu 8.10 which has been working very well too except that I didn't get any options in the Preferences menu to switch off the damn loud system beep. The beep comes through the speakers and at a much much louder volume than any other sound or music.

I tried changing thins in BIOS and I tried installing various packages hoping one of them will give me the options. Finally I've found out how to disable it:


$ sudo modprobe -r pcspkr

This tip page showed me how to put it into /etc so it's applied all the time. Thanks Andy!

domstripper - A lxml.html test project

November 20, 2008
1 comment Python

I'm just playing with the impressive lxml.html package. It makes it possible to easily work with HTML trees and manipulate them.

I had this crazy idea of a "DOM stripper" that removes all but specified elements from an HTML file. For example you want to keep the contents of the <head> tag intact but you just want to keep the <div id="content">...</div> tag thus omitting <div id="banner">...</div> and <div id="nav">...</div>. domstripper now does that. This can be used for example as a naive proxy that tranforms a bloated HTML page into a more stripped down smaller version suitable for say mobile web browsers. It's more a proof of concept that anything else.

To test you just need a virtual python environment and the right system libs to needed to install lxml. This worked for me:


$ sudo apt-get install cython libxslt1-dev zlib1g-dev libxml2-dev
$ cd /tmp
$ virtualenv --no-site-packages testenv
$ cd testenv
$ source bin/activate
$ easy_install domstripper

Now you can use it like this:


>>> from domstripper import domstripper
>>> help(domstripper)
...
>>> domstripper('bloat.html', ['#content', 'h1.header'])
<!DOCTYPE...
...

Best to just play with it and see if makes sense. I'm not saying this is an amazing package but it goes to show what can be done with lxml.html and the extremely user friendly CSS selectors.

How to unit test the innards of a Django view function

November 15, 2008
3 comments Django

Seconds ago I got this running and haven't yet fully understood what I've just done but the results are exactly what I need and this is going to be great.

Basically, in Django you have views like this:


def _render(template, data, request):
   return render_to_response(template, data,
             context_instance=RequestContext(request))

@login_required
def club_page(request, year):
   variable1 = year / 4
   variable2 = variable1 * 2
   return _render("foo.html", locals(), request)

Now in my unit tests I don't want to have to call the view function and then have to dissect the resulting HTML just to figure out if the view function prepared the correct variables. So, here's my solution to this problem:

Truncated! Read the rest by clicking the link below.