Created
November 5, 2018 15:25
-
-
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)
This file contains hidden or 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
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