Skip to content

Commit

Permalink
Merge branch 'fix_cachedict'
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Bailey authored and Stephen Bailey committed Mar 24, 2015
2 parents c28addb + d2f0372 commit df0d094
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions py/util/cachedict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,39 @@
"""

class CacheDict(dict):
def __init__(self, n):
"""
A dictionary that keeps only the last n items added
"""
def __init__(self, n, d=None):
"""
Create CacheDict with n keys cached.
If d is a dict, use that to initialize the key/value pairs
"""
self._keys = [None,]*n
self._current = -1
self._n = n

if type(d) == dict:
for key, value in d.items():
self[key] = value

#- Needed by pickle to reconstruct the object.
#- Pickle tries to reconstruct the dictionary via __setitem__ before
#- filling in _keys, _current, _n. This allows it to create a new
#- object first to get those right, before calling __setitem__
def __reduce__(self):
return type(self), (self._n, dict(self))

def __setitem__(self, key, value):
"""Sets the key/value, possibly dropping an earlier cached key/value"""
if key in self:
return

i = self._current = (self._current + 1) % self._n
if self._keys[i] is not None:
del self[self._keys[i]]

self._keys[i] = key
dict.__setitem__(self, key, value)


0 comments on commit df0d094

Please sign in to comment.