Example for singleton decorator pattern in python

I know there is not very common in Python to use the singleton pattern, but I found a nice implementation of this pattern in Python 3 Patterns, Recipes and Idioms book. Starting with that example I implemented an equivalent of the well known PHP getConnection example.

You have the code below:

This is the class that implements the Singleton pattern.

class Singleton:
  def __init__(self, klass):
    self.klass = klass
    self.instance = None
  def __call__(self, *args, **kwds):
    if self.instance == None:
      self.instance = self.klass(*args, **kwds)
    return self.instance

Now, we create a class and we decorate it with the Singleton class. Let’s import also MySQLdb module *.

import MySQLdb
class Database:
  connection = None
  def get_connection(self):
    if self.connection is None:
      self.connection = MySQLdb.connect(host="localhost", user="root", passwd="razvan", db="mydatabase")
    return self.connection

Let’s test this:

db1 = Database().get_connection()
db2 = Database().get_connection()
print (db2)
print (db1)

You will see something like:

<_mysql.connection open to 'localhost' at 16b4800>
<_mysql.connection open to 'localhost' at 16b4800>

As you can see there is only one object.

For fun, let’s remove the line “@Singleton” and re-run the example. This time you will see different objects:

<_mysql.connection open to 'localhost' at c91e20>
<_mysql.connection open to 'localhost' at bccba0>

You can find the fully example here.

* If you don’t know how to install MySQLdb, you can check the previous post.

Install MySQLdb for Python 2.x

If you want to install MySQLdb for Python 2.x, without using the package manager of your Linux distro (Ubuntu in our case), you can do it through pip like this:

First you must to have installed libmysqlclient-dev (and, of course pip)

sudo apt-get install libmysqlclient-dev

After that execute this two commands:

sudo easy_install -U distribute
sudo pip install mysql-python

Test if is working:

Type in the command line:


and in the python console:

import MySQLdb

That’s all. Enjoy!

Delete Google Analytics Profile/Website

It took me a while to figure out how to delete a Property (web site) from Google Analytics, mainly because is not so obvious and also Google Help… actually doesn’t help too much.

In your Google Analytics account:

1. Click on the Property you want to delete (eg: razvantudorica.com).

Continue reading Delete Google Analytics Profile/Website

Vagrant broken with VirtualBox 4.2.14

Today I had this issue with Vagrant and VirtualBox 4.2.14 (virtualbox-4.2_4.2.14-86644~Ubuntu~raring_amd64) on an Ubuntu 13.04 host.

vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'ubuntu12_04_v1'...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["import", "/home/razvan/.vagrant.d/boxes/cdn-ubuntu12_04_v1/virtualbox/box.ovf"]
Stderr: 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Interpreting /home/razvan/.vagrant.d/boxes/ubuntu12_04_v1/virtualbox/box.ovf...
Progress object failure: NS_ERROR_CALL_FAILED

The solution is even to downgrade your VirtualBox to 4.2.12 or to upgrade to 4.2.15 from VirtualBox website (the link is possible to be broken soon).