Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionary with memory of keys that were accessed?

I would like to create a data structure that behaves like a dictionary with one added functionality which is to keep track of which keys have been "consumed". Please note that I can't just pop the values as they are being reused.

The structure should support these three cases, i.e. mark the key as consumed when accessed as:

if key in d:
    ...
d[key]
d.get(key)

This is what I have written:

class DictWithMemory(dict):

    def __init__(self, *args, **kwargs):
        self.memory = set()
        return super(DictWithMemory, self).__init__(*args, **kwargs)

    def __getitem__(self, key):
        self.memory.add(key)
        return super(DictWithMemory, self).__getitem__(key)

    def __contains__(self, key):
        self.memory.add(key)
        return super(DictWithMemory, self).__contains__(key)

    def get(self, key, d=None):
        self.memory.add(key)
        return super(DictWithMemory, self).get(key, d)

    def unused_keys(self):
        """
        Returns the list of unused keys.
        """
        return set(self.keys()).difference(self.memory)

As I am not very familiar with the internals of dict, is there a better way to achieve this result?

like image 439
badzil Avatar asked May 25 '12 16:05

badzil


1 Answers

Here's a solution that abstracts everything away inside a metaclass. I'm not sure if this is really any more elegant, but it does provide some amount of encapsulation should you change your mind about how to store the used keys:

class KeyRememberer(type):

    def __new__(meta, classname, bases, classDict):
        cls = type.__new__(meta, classname, bases, classDict)

        # Define init that creates the set of remembered keys
        def __init__(self, *args, **kwargs):
            self.memory = set()
            return super(cls, self).__init__(*args, **kwargs)
        cls.__init__ = __init__

        # Decorator that stores a requested key in the cache
        def remember(f):
            def _(self, key, *args, **kwargs):
                self.memory.add(key)
                return f(self, key, *args, **kwargs)
            return _

        # Apply the decorator to each of the default implementations
        for method_name in [  '__getitem__', '__contains__', 'get' ]:
            m = getattr(cls, method_name)
            setattr(cls, method_name, remember(m))

        return cls


class DictWithMemory(dict):

    # A metaclass that ensures the object
    # has a set called 'memory' as an attribute,
    # which is updated on each call to __getitem__,
    # __contains__, or get.
    __metaclass__ = KeyRememberer

    def unused_keys(self):
        """
        Returns the list of unused keys.
        """
        print "Used", self.memory
        return list(set(super(DictWithMemory,
                              self).keys()).difference(self.memory))
like image 123
chepner Avatar answered Oct 16 '22 22:10

chepner