Skip to content

Instantly share code, notes, and snippets.

@hadware
Created November 5, 2018 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadware/f495211209a1794d483dc3d4ab4a0b1f to your computer and use it in GitHub Desktop.
Save hadware/f495211209a1794d483dc3d4ab4a0b1f to your computer and use it in GitHub Desktop.
An ordered dictionnary kind of like a Deque: adding or updating a key (re)-appends it to the end of the dictionary, and if the dictionary is full, the oldest elements are pop'ed (in a FIFO fashion)
class OrderedDequeDict(OrderedDict):
def __init__(self, size=100, *args, **kwargs):
super().__init__(*args, **kwargs)
self.size = size
def __setitem__(self, key, value):
if key in self:
del self[key]
elif len(self) == self.size:
self.popitem(last=False)
OrderedDict.__setitem__(self, key, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment