Created
February 26, 2014 06:28
-
-
Save rmcgibbo/9224580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bisect | |
import collections | |
class Configuration(collections.Mapping): | |
'''dict-like object that allows multiple values per key. each key is | |
associated with a 'priority' __getitem__ always return the entry with the | |
highest priority | |
''' | |
def __init__(self): | |
self._items = collections.defaultdict(lambda : []) | |
self._priorities = collections.defaultdict(lambda : []) | |
def __getitem__(self, key): | |
return self._items[key][-1] | |
def __contains__(self, key): | |
return key in self._items | |
def set(self, key, value, priority=0): | |
i = bisect.bisect_right(self._priorities[key], priority) | |
self._priorities[key].insert(i, priority) | |
self._items[key].insert(i, value) | |
def __len__(self): | |
return len(self._items) | |
def update(self, other): | |
if not isinstance(other, Configuration): | |
raise TypeError() | |
for k, v, p in other: | |
self.set(k, v, p) | |
def __iter__(self): | |
for k in self._items.keys(): | |
for v, p in zip(self._items[k], self._priorities[k]): | |
yield k, v, p | |
if __name__ == '__main__': | |
c = Configuration() | |
c.set('key', 'b', 1) | |
c.set('key', 'a', 0) | |
c.set('otherkey', 'o', 1) | |
assert c['key'] == 'b' | |
assert c['otherkey'] == 'o' | |
c2 = Configuration() | |
c2.set('key', 'c', 2) | |
c2.update(c) | |
expected = iter([('key', 'a', 0), ('key', 'b', 1), ('key', 'c', 2)]) | |
for i, (k, v, p) in enumerate(c2): | |
if k == 'key': | |
assert next(expected) == (k, v, p) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some code adapted from http://code.activestate.com/recipes/577197-sortedcollection/