Skip to content

Instantly share code, notes, and snippets.

@saicologic
Created January 27, 2011 08:05
Show Gist options
  • Save saicologic/798225 to your computer and use it in GitHub Desktop.
Save saicologic/798225 to your computer and use it in GitHub Desktop.
ring_buffer.py
from collections import deque
class RingBuffer(deque):
def __init__(self, size):
deque.__init__(self)
self.size = size
def append(self, value):
if len(self) == self.size:
self.popleft()
deque.append(self, value)
def get(self, index=None):
if index == None:
return list(self)
else:
return list(self)[index]
if __name__ == '__main__':
size = 5
ring = RingBuffer(size)
for x in range(10):
ring.append(x)
print ring.get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment