Tag Archives: python

Singleton in Python

Singleton pattern should be the most known design pattern out there(maybe the most overused design pattern as well). The idea is to restrict the object creation to only one instance. The typical way of achieving this is to declare the Singleton class’s constructor as private and then provide a static method to do the object initiation. This method would create an object on it’s first call and then onward return the already created object. Since Python doesn’t have private constructors we have to go down a complete different path to achieve the same objective. Remember we have to somehow get between the object creation mechanism and make sure only one object is created at most. The right place to do this is __new__ which is where objects are created in Python. You can find a good introduction to __new__ here[1].

Here is an example Singleton class. What it does is check whether an object instance is already created and return it. If an object is not created yet it would call the super class’s __new__ where the object would be created and returned.

 class Singleton(object):
     __instance = None
     def __new__(cls, *args, **kwargs):
         if not cls.__instance:
             cls.__instance = super(Singleton, cls).__new__(cls,
*args, **kwargs)
         return cls.__instance

The downside of the above design is there is no simple way to subclass a singleton class.

Another popular pattern to achieve the same effect as the singleton pattern is the Borg pattern. This allows multiple object creation but forces each object to share the same state.

Here is an example Borg pattern.

class Borg(object):
    __state = {}
    def __init__(self):
        self.__dict__ = self.__state

[1] http://agiliq.com/blog/2012/06/__new__-python/


Django cronjobs

In web applications sometimes you want to run processes that are seperated from the Request/Response cycle, like scheduled jobs that run at a specific time of a day. Scheduling such jobs in a unix environment is very easy with cronjobs. You just need to edit the crontab file and define a time and a command(a script to be run in this case) to execute at that time.

This post is about how to schedule such Django programs, that is python programs that import and use the Django framework.

This should be trivial because a Django program is another python program. But there is an important step to be done in about for Django modules to be discovered and run. That is to specify the Django settings file.

Let’s say there is a Django project called mysite and a Django app called myapp that has a method called runJob() in a python module called jobs.py that needs to be called every 30 mins.

let’s write a python script called thescript.py that will run every 30 minutes as a cronjob that will in turn call the runjob method.


import os

os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘settings’

from myapp.jobs import runJob



For easiness save the ‘thescript.py’ in the mysite folder.

Then open a console and enter crontab -e to edit your cron file. Enter the following line.

*/30 * * * * python /path to mysite/thescript.py > logfile 2>&1

This will run the ‘thescript.py’ file every 30 minutes and any output would be directed to the logfile specified and the last ‘2>&1’ tells that no email should be sent to the administrator. For more information on how to write the crontab file have a look at this.

That is how easy it is to schedule a Django cronjob in an Unix environment.


Django-resolvetime is a Django filter to convert a python datetime object into a format that specifies the time difference between the time of the datetime and now. I developed this for a personal project of mine and decided to opensource since I could not find a filter available to get the job done.

The code is hosted at http://code.google.com/p/django-resolvetime/. Installation and usage of the filter is straight forward. Instructions are given in the README file.

Configuring PyDev to work with Django

Django is the most widely used if not the most popular web framework among those available for python. Have a look at this report by Jacob Kaplan-Moss the co-creator of Django, to get an idea about the current standing of Django among the community.

One disadvantage for Django is the lack of IDE support it has. Here it holds a clear disadvantage against it’s rival web frameworks such as Ruby on Rails. Wing IDE looks impressive but is not free. I wasn’t much impressed with Komodo and it is not free either. The best bet seams to be Netbeans 7 which promises to have good python support. Hopefully it would have satisfactory Django support as well. Currently netbeans is having a python plugin that is still in beta stage. The best alternative for me as of now is PyDev which I have been using with python for sometime now. Here I will explain how to configure PyDev to work with Django.

1.) First download and install Django
2.) Add Django source folder to PYTHONPATH in pydev

  • Go to windows > preferences
  • In the opended dialog choose interpreter-python
  • Here under python interpreter a python interpreter should be available. If not add one by clicking new
  • In System PYTHONPATH add the downloaded source folder of Django by clicking New Folder

3.) Create a new pydev project.

  • Go to file > new > pydev project
  • Enter project name, path and importantly make sure the option ‘create default src folder and add it to PYTHONPATH?’ is choosen. This is easiest way or you can manually add the source folder of the new project to PYTHONPATH by going to File > properties > PYTHONPATH

4.) Start a new Django project

  • Open a new console from the Operating System(console support inside pydev is avialable but is not very satisfactory)
  • Navigate to the newly create src folder inside the pydev project
  • type django-admin.py startproject ‘sitename’ (sitename should be the name of your project). This how a new project is started in Django

If your projects name is mysite the folder structure should be as follows

Now pydev should be configured to work with Django. All the python feature that pydev supports should be now working with Django as well.

The standard features of a modern IDE works beautifully with pydev for Django but there are some major drawbacks. Django template tags are not supported in pydev. Also the console support inside pydev is not impressive. So for now I am forced to use pydev for Django coding, the OS console for giving Django commands and nothing for Django templates.

Debugging in Python

Python has a simple yet powerful builtin module for debugging called pdb. Using this module you can debug your python code easily even without any IDE.

1. First step is to import the pdb module

2. Next step is to mark the entry point where the debugger would be called in. We use the set_trace() method for that.

3. When the program is run the execution would stop at the set_trace() method and would wait for debugging commands.

4. To run the next statment enter ‘n’ or ‘next’

5. To print a variable enter ‘pp variable_name’

6. To run until exit enter ‘c’ or ‘continue’

7. To display the current location enter ‘l’ or ‘list’
This would display an appropiate part of the source code with a pointer to the current statement.

8. To step into a method enter ‘s’ or ‘step’ when debugger is met with a method

9. To insert a break point enter ‘b filename:linenumber’

10. You can also dynamically assign values into variables.
Let’s assume there is a variable called num initialized to 10. You want to see what would happen when that variable is initialized to 20. You just do this. (pdb) num = 20 and then enter ‘n’ to execute next statement or enter ‘c’ to execute program until the end.

An example would look like this

import pdb

def method():
i = 10
name = ‘kasun’
last_letter = name[-1]

last_num = ord(last_letter)

print ‘last_num is’, last_num
if __name__ == ‘__main__’:

Journey into Python

As I said earlier I am in the process of moving into python. I am in love with the beast at the moment and thought I would write down what I find interesting in python. Pros and cons, both. This is my journey into Python.

The first thing that I found of interest is the python interactive shell. It allows you to run python statements and expressions in the console. This is really handy when you are learning python or testing a python module. How many times have you skipped checking out some feature while learning a language like Java because writing a whole class and then running it to test a small feature is not worth all the trouble? I do it all the time. That is where the python interactive shell comes in.

python interactive shell

python interactive shell

Importing modules, instantiating variables and running statements can be done right in the console by using the python interactive shell.

I am selected for summer of code 2009

I am so happy to announce that I am selected for Google Summer of Code 2009. Over the next 4 months close to 1000 students from over 70 countries would work with close to 150 open source projects to make the world a little better place.

I am selected to work with Maemo and will be using Python. After programming all this time with Java I was planning to move to Python. This will be a great chance for me to learn Python while contributing to an open source project and on the process getting paid by Google.

Thank you very much Google for this wonderful program.