Created
January 27, 2011 08:05
-
-
Save saicologic/798225 to your computer and use it in GitHub Desktop.
ring_buffer.py
This file contains 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 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