Skip to content

Instantly share code, notes, and snippets.

@thepaul
Created February 10, 2012 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thepaul/1792171 to your computer and use it in GitHub Desktop.
Save thepaul/1792171 to your computer and use it in GitHub Desktop.
import collections
import Queue
class MaxSizeDict(collections.MutableMapping):
def __init__(self, maxsize):
self.maxsize = maxsize
self.valdict = {}
self.keyqueue = Queue.Queue()
def __setitem__(self, key, value):
self.valdict[key] = value
self.keyqueue.put(key)
while len(self.valdict) > self.maxsize:
del self.valdict[self.keyqueue.get()]
def __contains__(self, key):
return key in self.valdict
def __iter__(self):
return iter(self.valdict)
def __len__(self):
return len(self.valdict)
def __delitem__(self, k):
raise NotImplemented
def __getitem__(self, k):
return self.valdict[k]
def __str__(self):
return str(self.valdict)
def __repr__(self):
return repr(self.valdict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment