luft·mensch - recipes

Named bitmap

Every now and then, one needs to keep track of a family of flags (boolean values), which you naturally would store in a numerical field which saves a lot of space and is database-friendly. Although the bit algrebra is straightforward, one often wishes to abstract that a little bit and ...

more ...

How to transpose a matrix

Python is such an expressive language. For instance, you can transpose a matrix in one line:

def transpose(matrix):
    return zip(*matrix)

>>> m = [(1, 2, 3), (4, 5, 6)]
>>> transpose(m)
[(1, 4), (2, 5), (3, 6)]
more ...

The Singleton pattern

In this post I am going to discuss the Singleton pattern in the Python context. Singleton is a design pattern which addresses the problem that one often would like to have a single instance of some class, say, of a database connection pool or a logging facility. This instance is ...

more ...