Recursive karatsuba multiplication in Python

I'm currently taking the class Algorithm: Design and Analysis of Stanford via Coursera and in the class the Karatsuba algorithm got mentioned. I went ahead and implemented it based on the lecture slides.

The Karatsuba algorithm is a fast multiplication algorithm. It's special because it was the first multiplication algorithm …

Continue reading »

Install PyQt in a virtualenv with Pip

Noticed that pip install pyqt isn't working? At least at the time of writing this blog it isn't..

This is the error that I got:
IOError: [Errno 2] No such file or directory:

It seems they didn't package it well and it's using to install. So …

Continue reading »

Only use worker when required on heroku with Django/Python

For a mobile project I required a background worker which sents an email with 300 generated QR codes zipped together as attachment. This costs quite some time so we need a background worker to execute this task.

I wanted to achieve the following result:

  • When the user wants to generate …

Continue reading »

Django Custom View Decorator with Arguments

To make my life easier I made a simple decorator which checks if the parameters are present in the request.POST or request.GET and returns a response if they are not.

It accepts a list of parameters in string format and will loop through each parameter to check if …

Continue reading »

Simple function to return json in Django

Just a small code snippet I use when I need to return json, comments are welcomed.

from django.http import HttpResponse  
from django.utils import simplejson as json

def json_response(dict_to_convert_to_json):  
    return HttpResponse(json.dumps(dict_to_convert_to_json),

To use it in your view you just do it this …

Continue reading »

Trying to install scrapy with pip lxml error: command 'gcc' failed with exit status 1

Just tried to install scrapy with pip / easy_install, but when it comes to installing lxml it will give the following error:

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c src/lxml/lxml.etree.c -o build/temp.linux-x86_64-2.7/src/lxml/lxml …

Continue reading »

How to install Setuptools to a specific python version

To install easy_install for a specific python version. I just installed from source and used the python version you want to install setuptools too. I used the following steps in Ubuntu 11.04 with Python 2.5 and Python 2.7 installed. The following commands will install setuptools to python …

Continue reading »