Interface to versioned dictionary

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?


Asked by: Miranda742 | Posted: 01-10-2021






Answer 1

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)

Answered by: Brad677 | Posted: 02-11-2021



Similar questions

user interface - Python loop dictionary items through a tkinter GUI using a button

I am currently making a menu test for my restaurant. I have made the GUI and am happy with its basic format (for now), but I am stuck on how to do my next step. I have multiple checkboxes set up (different ingredients) and the plan is for different menu items to loop through (appear on the screen), the employee then checks the appropriate ingredients, clicks a submit and continue button that I have made and then th...


user interface - Display dictionary contents in a label in python tkinter

Ive been trying for a while now but i cant seem to display the contents of a dictionary in a label. I want to hit the display button and when i do, i want all the contents in my dictionary to display, see the code below: import sys from tkinter import * from tkinter import ttk import time from datetime import datetime now= datetime.now() x = [] d = dict() def quit(): print("Have a great day! Goodbye :)...


How can I send a python dictionary to a QML interface with a Signal?

I want to send dictionaries, containing data that I need to use to dynamically create qml objects, from a PySide2 class to a QML interface and since I need to do it in response to certain events, I need to use signals and slots. Since I've just started to use QML and python I tried to create a simple project just to play around (as you can see from the code) QML: import QtQuick 2.10 import Q...


sorting - In Python, how can you easily retrieve sorted items from a dictionary?

Dictionaries unlike lists are not ordered (and do not have the 'sort' attribute). Therefore, you can not rely on getting the items in the same order when first added. What is the easiest way to loop through a dictionary containing strings as the key value and retrieving them in ascending order by key? For example, you had this: d = {'b' : 'this is b', 'a': 'this is a' , 'c' : 'this is c'}


Python dictionary from an object's fields

Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this: >>> class Foo: ... bar = 'hello' ... baz = 'world' ... >>> f = Foo() >>> props(f) { 'bar' : 'hello', 'baz' : 'world' } NOTE: It should not include methods. Only fields.


python - How do you retrieve items from a dictionary in the order that they're inserted?

Is it possible to retrieve items from a Python dictionary in the order that they were inserted?


python - How can I make a dictionary from separate lists of keys and values?

I want to combine these: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] Into a single dictionary: {'name': 'Monty', 'age': 42, 'food': 'spam'}


python - Dictionary or If statements, Jython

I am writing a script at the moment that will grab certain information from HTML using dom4j. Since Python/Jython does not have a native switch statement I decided to use a whole bunch of if statements that call the appropriate method, like below: if type == 'extractTitle': extractTitle(dom) if type == 'extractMetaTags': extractMetaTags(dom)


Is a Python dictionary an example of a hash table?

One of the basic data structures in Python is the dictionary, which allows one to record "keys" for looking up "values" of any type. Is this implemented internally as a hash table? If not, what is it?


python - Is there a "one-liner" way to get a list of keys from a dictionary in sorted order?

The list sort() method is a modifier function that returns None. So if I want to iterate through all of the keys in a dictionary I cannot do: for k in somedictionary.keys().sort(): dosomething() Instead, I must: keys = somedictionary.keys() keys.sort() for k in keys: dosomething() Is there a pretty way to iterate t...


python - List all words in a dictionary that start with <user input>

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string? Ex: User: "abd" Program:abdicate, abdomen, abduct... Thanks! Edit: I'm using python, but I assume that this is a fairly language-independent problem.


python - Check if a given key already exists in a dictionary and increment it

How do I find out if a key in a dictionary has already been set to a non-None value? I want to increment the value if there's already one there, or set it to 1 otherwise: my_dict = {} if my_dict[key] is not None: my_dict[key] = 1 else: my_dict[key] += 1


Given a list of variable names in Python, how do I a create a dictionary with the variable names as keys (to the variables' values)?

I have a list of variable names, like this: ['foo', 'bar', 'baz'] (I originally asked how I convert a list of variables. See Greg Hewgill's answer below.) How do I convert this to a dictionary where the keys are the variable names (as strings) and the values are the values of the variables? {'foo': foo, 'bar': bar, 'baz': baz} Now that I'm re-aski...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top