China wrecked the Copenhagen deal

December 23, 2009
0 comments Politics

"Copenhagen was much worse than just another bad deal, because it illustrated a profound shift in global geopolitics. This is fast becoming China's century, yet its leadership has displayed that multilateral environmental governance is not only not a priority, but is viewed as a hindrance to the new superpower's freedom of action. I left Copenhagen more despondent than I have felt in a long time. After all the hope and all the hype, the mobilisation of thousands, a wave of optimism crashed against the rock of global power politics, fell back, and drained away."

From the brilliantly informed and well articulated article How do I know China wrecked the Copenhagen deal? I was in the room

Just so you know, I love China, but there's a lot of things I hate about it too. Admittedly, any country in their position would perhaps do the same as the force of people getting rich is more powerful than almost anything else. Please do take the time to read this article as it helps to give an interesting perspective on the post-Copenhagen-conference talks.

Migrating with South on a field that uses auto_now_add=True

December 16, 2009
5 comments Django

I have a Django model that looks something like this:


class MyModel(models.Model):
   modify_date = models.DateTimeField(auto_now=True)
   ...

Retroactively now I wanted to add a field called add_date which uses the auto_now_add=True trick. The migration used in this project is South which is great but doesn't work very well with the auto_now_add=True because the field doesn't have a straight forward default. So, first I changed the field to this:


class MyModel(models.Model):
   modify_date = models.DateTimeField(auto_now=True)
   add_date = models.DateTimeField(auto_now_add=True, null=True)
   ...

Notice the null=True which is important. Then I used startmigration to generate the code for the forward and backward to which I added a this stuff:


class Migration:

   def forwards(self, orm):

       db.add_column('myapp_mymodel', 'add_date', orm['myapp.mymodel:add_date'])
       for each in MyModel.objects.all():
           # since MyModel is referenced elsewhere I can work out the oldest date
           oldest_date = get_oldest_related_date(each, 
                              default=each.modify_date)
           each.add_date = oldest_date
           each.save()

That way all old records will have the date (not entirely accurate but good enough) and all new records will automatically get a date. Is there a better way? I bet, but I don't know how to do it.

Orphaned Land - Jewish Muslim Metal

December 8, 2009
9 comments Music

Orphaned Land - Jewish Muslim Metal Orphaned Land is a great metal band from Israel who I've known about for some time but never researched much.

"Israel's ORPHANED LAND is probably the only band from this country that has managed to succeed at building up a huge following among Muslims and Arabian people"

I've never taken the time to listen to their lyrics and figure out what they're singing about. It's not a surprise that they're not pro one side only but I didn't know that they appeal to both sides which is quite fantastic.

Comparing YUI Compressor and slimmer

November 17, 2009
5 comments Python

YUI Compressor apparently supports compressing CSS. Cool! I had to try it and what's even more cool is that it's the only other CSS minifier/compressor that doesn't choke on CSS hacks (the ones I tried). The only other CSS minifier/compressor is my very own slimmer. So, let's see what the difference is.

Running the YUI Compressor 10 times on a relatively small file takes 0.3 seconds on average. Running the same with python 2.6 and slimmer.css_slimmer takes 0.1 seconds on average. I think most of this time is spent loading the jar file than the actual time of running the compression.

Truncated! Read the rest by clicking the link below.

Those Crazy Chinese

November 16, 2009
0 comments Django

Those Crazy Chinese My friend Chris West has built a great new site called Those Crazy Chinese which describes itself like this:

"Chinese (Mandarin) is a beautiful and highly literal language - directly translated, many words have entertaining and occasionally logical meanings."

It's built in Django and it integrates to Twitter so if you're on Twitter just follow it there to get the latest additions of new interesting and amusing literal translations. This website is mainly geared towards people who are, like Chris, learning Mandarin.

Mocking os.stat in Python

November 8, 2009
5 comments Python

I have some code that checks that a file is treated differently if some time has passed. In other words, it reacts if the modification time is more than it was before. The code uses os.stat(filename)[stat.ST_MTIME] to do this. The challenge was to mock os.stat so that I can pretend some time has passed without having to wait. Here was my first solution which made running many tests sloooow:


def test_file_changed(self):
    filename = 'foo.txt'
    expect_timestamp = int(time.time())
    ...run the unit test with 'expect_timestamp'...
    time.sleep(1)
    expect_timestamp += 1
    ...run the unit test with 'expect_timestamp'...

So, here's how I mock os.stat to avoid having to do the time.sleep(1) in the test:


def test_file_changed(self):
    filename = 'foo.txt'
    expect_timestamp = int(time.time())
    ...run the unit test with 'expect_timestamp'...
    import os
    from posix import stat_result
    def fake_stat(arg):
        if arg == filename:
            faked = list(orig_os_stat(arg))
            faked[stat.ST_MTIME] = faked[stat.ST_MTIME] + 1
            return stat_result(faked)
        else:
            return orig_os_stat(arg)
    orig_os_stat = os.stat
    os.stat = fake_stat

    expect_timestamp += 1
    ...run the unit test with 'expect_timestamp'...

I hope this helps someone else who's trying to do the same. It took me some time to figure out that os.stat is used by lots of various sub routines in the os module so it's important to only mock the relevant argument otherwise you might get unexpected problems.

"Frank Zappa: The Biography" by Barry Miles

October 30, 2009
7 comments Books

Available on Amazon.com I've just read Frank Zappa: The Biography by Barry Miles. It's a detailed book on 380 pages about virtually every year of Frank Zappa's life. From his parents to his death.

I'm a huge Frank Zappa fan and have been for more than a decade. It's probably the most listened to artist ever in my life in terms of number of listened to songs. Actually not probably; definitely. I adore his music and his personality and this is the second book I read about him. The other book I read was Real Frank Zappa Book written by Peter Occhiogrosso based on biography interviews with Frank for the purpose of writing this book. That book was much more bland and emphasized particularly his early political work and also very much emphasizes on his work as a orchestral conductor/business man.

The detail work in this book is really fantastic. It's thanks to Barry's in-depth understanding of music and the music industry that you get deep down to the nitty-gritty details of Zappa's work. As always with books like this, it's not till you read about the lyrics that you fully understand the lyrics even if you have listened to them many a times. Some of these lyrics I'm actually kind of sad to have understood now as of reading about them in this book. For example, I now understand that the song We're Turning Again which is an up-yours to his old band members Mothers of Invention.

If anything bad can be said about the book it's that it sort of ends on a bad note (no pun intended), as it ends on the sad last few years when Frank was really sick and up to the point of his death. And also, I would have liked to find out more about Barry's own personal relationship with Frank because he couldn't possibly have written this book had he not admired the guy too.

There is no doubt in my mind that Frank Zappa is one of the most innovative and inspirational characters in twentieth-century music history. And probably show-biz too for that matter. Even though the book reveals some truths about Frank as a bit of "douche bag" I'm still firmly one of his biggest fans. If you wanna find out more about Frank Zappa this is most like the book to get.

Following is an extract from a quoted interview by Gail Sloatman who later became Gail Zappa:

"And I remember thinking, Oh my God! Here's this guy, I think he's extraordinary, it's such a different sensation! I know he hasn't taken a bath in four months and his moustache smells like peanut butter..."

She sums it up nicely in her own very personal words so well. There is something amazing about this guy beyond the less appealing facade.

iPhone push notifications for Twitter with Prowl

October 25, 2009
1 comment Web development

iPhone push notifications for Twitter with Prowl Bruno Renié has written a nifty app:iPhone push notifications for Twitter with Prowl

With the power of Prowl it pushes a notification to your iPhone when someone mentions you on Twitter. You first need to install the Prowl app on your iPhone and then go to their website to get your notification key. Then, go to Bruno's site, sign in with your Twitter account (OAuth, so no password give-away) and badabing! you get instant notifications on your phone, for free, when someone mentions you.

If you're one of those people who use Twitter instead of instant messaging and have lots of mentions this might be excessive for you but if not it can be very useful if you are like me who is very slow to spot that someone has replied or mentioned your name.

Great work Bruno!

What makes my website slow? DNS

October 23, 2009
14 comments This site, Linux

Pagetest web page performance test is a great tool for doing what Firebug does but not in your browser. Pagetest can do repeated tests to iron out any outliers. An alternative is Pingdom tools which has some nifty sorting functions but is generally the same thing.

So I ran the homepage of my website on it and concluded that: Wow! Half the time is spent on DNS lookup!

First Second Third

The server it sits on is located here in London, UK and the Pagetest test was made from a server also here in the UK. Needless to say, I was disappointed. Is there anything I can do about that? I've spent so much time configuring Squid, Varnish and Nginx and yet the biggest chunk is DNS lookup.

In a pseudo-optimistic fashion I'm hoping it's because I've made the site so fast that this is what's left when you've done all you can do. I'm hoping to learn some more about this "dilemma" without having to read any lengthy manuals. Pointers welcomed.