Skip to content

Instantly share code, notes, and snippets.

@derricw
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derricw/c288930547ab82b12549 to your computer and use it in GitHub Desktop.
Save derricw/c288930547ab82b12549 to your computer and use it in GitHub Desktop.
simple ring buffer
from collections import deque as dq
class RingBuffer(dq):
"""
Simple ring buffer.
Args:
size (int): number of items
"""
def __init__(self, size):
dq.__init__(self)
self.size = size
self.full = False
def append(self,item):
dq.append(self,item)
if len(self) == self.size:
self.append = self.full_append
self.full = True
def full_append(self,item):
dq.append(self,item)
self.popleft()
def get(self):
return list(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment