tags:

views:

68

answers:

1

I have an versioned document store which I want to access through an dict like interface. Common usage is to access the latest revision (get, set, del), but one should be able to access specific revisions too (keys are always str/unicode or int).

from UserDict import DictMixin
class VDict(DictMixin):   
    def __getitem__(self, key):
        if isinstance(key, tuple):
            docid, rev = key
        else:
            docid = key
            rev = None  # set to tip rev

        print docid, rev
        # return ...

In [1]: d = VDict()

In [2]: d[2]
2 None

In [3]: d[2, 1]
2 1

This solution is a little bit tricky and I'm not sure if it is a clean, understandable interface. Should I provide a function

def getrev(self, docid, rev):
   ...

instead?

+2  A: 

Yes, provide a different API for getting different versions. Either a single methodcall for doing a retrieval of a particular item of a particular revision, or a methodcall for getting a 'view' of a particular revision, which you could then access like a normal dict, depending on whether such a 'view' would see much use. Or both, considering the dict-view solution would need some way to get a particular revision's item anyway:

class RevisionView(object):
    def __init__(self, db, revid):
        self.db = db
        self.revid = revid
    def __getitem__(self, item):
        self.db.getrev(item, self.revid)
Thomas Wouters