Author Archives


23
Jul 10

Singleton pattern in Python

Just came up with a simple pattern for singletons in Python using a class decorator:

def singleton(cls):
    instances = {}
    def instance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return instance

Little demo:

>>> @singleton
... class Foo(object):
...     pass
...
>>> f1 = Foo()
>>> print id(f1)
3077430124
>>> f2 = Foo()
>>> print ...

23
Jul 10

href.be services ported to GAE

The short URL service at http://href.be/ now runs on Google App Engine. It uses the latest version of Django and thanks to the App Engine it uses BigTable as its storage backend.

This migration will surely result in a better availability and responsiveness of the service!

While we ...


22
Jul 10

href.be tools

Just pushed some tools to the Python Package Index to integrate the http://href.be/ short URL service with your Python and/or Django software:

Also, the service API now offers click statistics for your shortened URL’s, so you can keep track of your outgoing links.

In ...


22
Jul 10

Python ternary operation

Python has no ternary operation, or has it? At least not directly, we had a discussion about possible solutions in the #python IRC channel on FreeNode and after some filddling I came up with this hack abusing the Python slicing magic:

class Ternary(object):
    '''
    Ternary-ish emulation, it looks like C-style ...

13
Jul 10

We're in this together

A song by Nine Inch Nails, called We’re In This Together. It reminds me of current times, so this one is for my lovely fiancĂ©:

I've become impossible
Holding on to when
When everything seemed to matter more
The two of us
All used and beaten up
Watching ...

3
Jun 10

Django chroot, securing your web application hosting

For me the best way to run Django applications has proven to be mod_wsgi and Apache. This setup allows for scalable application pools to run as a seperate process, featuring threading and extra privilege separation, which is great.

To make your application even more hardened, you can use the mod_wsgi ...


27
May 10

Slicing an ext3 disk for Time Machine

Since a while I wanted to upgrade my Mac to OSX 10.6 Snow Leopard, but I never got around to do it because I needed disk space for my backup first. I have a 1TB USB2 disk containing data, but I didn’t want to wipe the disk contents ...


14
May 10

inode usage count in Linux

Filesystems like extfs use inodes to allocate blocks of (file) data. One can not only run out of disk space, but also out of inodes. You can use this to determine the number of inodes used in a directory:

~% find $HOME -type d -exec stat --format '%h' {} \; | awk '{s+=$1 ...

8
Apr 10

Pixels: 8 bit attack

A nice music video clip showing an 8 bit attack on a city.


2
Apr 10

Python MIME Magic

The default Python mimetypes implementation is very basic, and limited to guessing mimetypes based on the file extension. This renders the module pretty useless to determine filetypes for uploads and files from other untrusted sources.

>>> import mimetypes
>>> mimetypes.guess_type(‘/etc/services’)
(None, None)
>>> mimetypes.guess_type(‘/usr/bin/who’)
(None, None ...