How to avoid SELECT sql generated by Django model’s save method?


Django framework is designed for novice users in mind and so they try to keep things simple at the cost of performance. Once such is the Django model’s save method. Every save fires first a SELECT statement to verify if a record exists with the provided “id” and then does update if exists else insert. As a good framework django does provide the flexibility to control this. You can pass the force_update=True to save method if you know for sure that the id exists already and the call to django model save avoids the SELECT sql before insert.
Same goes for insert too. You set the force_insert=True in that case.

Django leaks memory in debug mode.


If you find your program is leaking memory when using Django model then it is most probably because you are in running in debug mode. Django keeps track of the sql statement generated by the model save in debug mode which tends to be the reason behind leak. It is not a problem unless you are in tight memory scenario which are normally enforced by hosting providers like webfaction. I learnt of this in a hard way but Django doc does mention this. See here

How to find a python library’s version?


Often times we need to check the version of a python library installed. I see there is no standard attribute convention for it. Most often it is  __version__ or Version or version or ver or Ver etc.,  So I use the dir command on the module to find out the exact attribute name for the version.

For example after installing pymongo…

1. Launch python shell
2. import pymongo
3. dir(pymongo)

This would provide all the methods, functions and attributes of pymongo module
If you know of a better way let me know.

Steps to install Scrapy.


The following steps assumes that you already have python installed. To make your installation easy the steps recommend using easy_install tool.

Install Scrapy

1. Download and install easy_install tool from cheeseshop .
2. Easy install “zope.interface”
3. Download and install Twisted 10.2.0 from Twisted site
4. Easy install “lxml”
5. Easy install “pyOpenSSL”
6. Easy install “Scrapy”

Test your Scrapy installation

1. Within python interpreter, import the following batteries and there should not be any errors

1. import zope.interface
2. import twisted
3. import lxml
4. import OpenSSL
5. import scrapy

2. If you need to find the version of a particular battery then use module attribute .version or .__version__ as below

print twisted.version ==> [twisted, version 10.2.0]

Combine items in two list to form dictionary(map)


People love python because it has solutions built in for problems most frequently faced by developers. Python’s zip is one such thing.


You would most probably need this in scenarios like csv file reading where the first row in the file is list of column names and subsequent are values. You know well that it is better to use map to read/write the values in a row like row[colName] = x or x = row[colName] rather than index like row[1] etc.,

Note: In case of csv file reading/writing, python does have a out-of-the-box solution: DictReader and so you don’t even have to do” even” this one liner 🙂 above…

Python objects and types


I’m one of the guy who needs to know the internals of a machine before using it. I can’t help it but that is my very nature. The following article that I refer to is not for faint heart (meaning beginners to programming or python) and is by far the best i’ve found ever that describes the internals of python(at least 2.x) and i’ve to admit that i never ever used python 3 or found a need for using 3.x.

If you are curious about how python types, object or descriptors or attributes or methods work internally then read this .

My recommendation is to save the pdf version since this guy never wrote a book (which would have been an excellent one for experts or intermediates) and not sure how long his/her site will be up.

Strategy pattern in python


Here is an example of strategy pattern in python. The alogrithm that varies in this example is the conversion of csv file to xml, html and any other formats. The client is the CsvUtil class. As you can see we can change the conversion at run time and also we can apply all the alogrithm if we wish to which we can’t achieve when we go for the regular inheritance model.  Please do note that an abstract class is equivalent to interface when there is no implementation.