Skip to content

Instantly share code, notes, and snippets.

@edwintcloud
Created May 7, 2019 17:48
Show Gist options
  • Save edwintcloud/30329bc644d005523622bf3faf14278b to your computer and use it in GitHub Desktop.
Save edwintcloud/30329bc644d005523622bf3faf14278b to your computer and use it in GitHub Desktop.
Circular Buffer in Python Part 1
class CircularBuffer(object):
def __init__(self, max_size=10):
"""Initialize the CircularBuffer with a max_size if set, otherwise
max_size will default to 10"""
self.buffer = [None] * max_size
self.head = 0
self.tail = 0
self.max_size = max_size
def __str__(self):
"""Return a formatted string representation of this CircularBuffer."""
items = ['{!r}'.format(item) for item in self.buffer]
return '[' + ', '.join(items) + ']'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment