Skip to content

Instantly share code, notes, and snippets.

@jeffmylife
Created March 30, 2020 20:35
Show Gist options
  • Save jeffmylife/8f7a8b90c9c14ed787b449e41ee02ff3 to your computer and use it in GitHub Desktop.
Save jeffmylife/8f7a8b90c9c14ed787b449e41ee02ff3 to your computer and use it in GitHub Desktop.
List with the ability to forget values. Kind of like a real-time sliding window.
class memorySequence(list):
def __init__(self, lst, size=10):
super().__init__(lst)
self._desired_size = size
def append(self, item):
if len(self) + 1 > self._desired_size:
del self[0]
return super().append(item)
return super().append(item)
def testMemorySequence():
test = memorySequence([1,2,3,4,5,6,7,8,9,10], 10)
print(test)
test.append(11)
print(test)
testMemorySequence()
"""
out:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment