Last active
July 21, 2020 11:24
-
-
Save daguiam/a585032e21e31fc954450691e494c67f to your computer and use it in GitHub Desktop.
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
from collections import deque | |
class CircularBuffer (deque): | |
""" Creates a circular buffer class which inherits the deque collection | |
and implements extract functions""" | |
def extract(self,n): | |
""" Extracts n items from the right """ | |
return list([self.pop() for i in range(n)]) | |
def extractleft(self, n): | |
""" Extracts n items from the left """ | |
return list(reversed([self.popleft() for i in range(n)])) | |
buffer = CircularBuffer([0],maxlen=100) | |
buffer.extend([1,2,3,4,5]) | |
print(buffer) | |
a = buffer.extract(3) | |
print(a) | |
print(buffer) | |
a = buffer.extractleft(2) | |
print(a) | |
print(buffer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment