Skip to content

Instantly share code, notes, and snippets.

@daguiam
Created July 21, 2020 11:22
Show Gist options
  • Save daguiam/57c564e972040a6d64f282d960ced905 to your computer and use it in GitHub Desktop.
Save daguiam/57c564e972040a6d64f282d960ced905 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(reversed([self.pop() for i in range(n)]))
def extractleft(self, n):
""" Extracts n items from the left """
return list(reversed([self.pop() 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment