Tip: creating a Xapian database in Python

January 19, 2010
5 comments Python

This cost me some hair-pulling today as I was trying to write a custom test runner for a Django project I'm working on that creates a test Xapian database just for running the tests. Basically, you can't do this:


os.mkdir(database_file_path)

Because if you do you end up getting these strange DatabaseOpeningError exceptions. So, here's how you do it:


import xapian
xapian.WritableDatabase(database_file_path,
                        xapian.DB_CREATE_OR_OPEN)

Hopefully by blogging about this some other poor coder will save some time.

Bookmarklet to replace the current domain with localhost:8000

January 17, 2010
1 comment Web development, Django

If you, like me, have various projects that do things like OAuth on Twitter or Google or you have a development site that goes to PayPal. So you're doing some Django development on http://localhost:8000/foo and click, for example, to do an OAuth on Twitter with an app you have there. Then Twitter will redirect you back to the live site with which you've set it up. But you're doing local development so you want to go back to http://localhost:8080/... instead.

Add this bookmarklet: to localhost:8000 to your browser Bookmarks toolbar and it does exactly that.

Here's its code in more verbose form:


(function() { 
   a = function(){
     location.href = window.location.href.replace(/http:\/\/[^\/]+\//,
            'http://localhost:8000/')
   };
   if (/Firefox/.test(navigator.userAgent)) { 
     setTimeout(a,0)
   } else {
      a()
   }
 })()

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.