How not to enable code customization

I had to implement custom object serialization in Python's memcached module. The way this is enabled is pretty horrific.

python-memcached supports custom pickling by passing an pickler and unpickler to the Client class:

import memcache
mc = memcache.Client(
        ['127.0.0.1:11211'], 
        pickler=pickle,
        unpickler=unpickle)

The problem thought is that these two are tied up to Python pickle module. And this means that one needs to implement the pickler in the following way:

class Pickler:

    def __init__(self, file, **kw):
        ..

    def dump(self, val):
        ...

I.e. it forces one to create a class and work with files in a very non-intuitive way.

A much better way to abstract this would be to see the pickler as a function that takes an object and returns data representing the object - - instead of doing a closely coupled tie to pickle module.

One thing I have learned when studying design patterns is that they follow these two simple principles:

Following these two basic principles one can really come far. And python-memcached has clearly broken the loosely coupled rule - - as their serialization is very tied up on the way pickle module works.

Code · Code improvement · Code rewrite · Python 21. Nov 2008
© Amir Salihefendic. Powered by Skeletonz.