Skip to content

Instantly share code, notes, and snippets.

@daguiam
Last active July 21, 2020 11:24
Show Gist options
  • Save daguiam/a585032e21e31fc954450691e494c67f to your computer and use it in GitHub Desktop.
Save daguiam/a585032e21e31fc954450691e494c67f to your computer and use it in GitHub Desktop.
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