samedi 22 avril 2017

What makes a good (object instance) Registry pattern (in Python)?

Records and their Registry. Here's the basic structure for a "Registry" class I'm using to store and manage a set of "Record" classes.

I'm using a nested dictionary (Vividict), because I like the readability of the indexing syntax. And, the Pickle module seems like the most obvious persistent storage solution for this script. I read there are several restrictions about what is Pickeable. Therefore, I moved all the methods out of the "Record" classes into the "Registry" class.

class Registry(object):
    """ Object Registry  """

    class Vividict(dict):
        def __missing__(self, key):
            value = self[key] = type(self)()
            return value

    def __init__(self, libname):
        """ Create an empty reportRegistry """
        self._libname = libname
        self._reg = self.Vividict()

    [... some other methods for managing the Registry ...]

    def saveRegistry(self):
        pickle.dump(self, open( self._libname + ".pickle", "wb" ) )

That's as far as I've gotten. In a sort of "Naive Python" way I've just put the defaultdict inside the registry class. Seems to work ok.

What makes a good object instance Registry pattern in Python?

Aucun commentaire:

Enregistrer un commentaire