-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathringbuffer.py
32 lines (26 loc) · 1.01 KB
/
ringbuffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import collections
class RingBuffer(collections.deque):
"""A ring buffer class based on collections.deque."""
def __init__(self, max_length, initial_state=[]):
"""Initializes self.
Parameters
----------
max_length : int
The length of the ring buffer. If more elements are added
than this, then the oldest elements will be overwritten.
initial_state=[] : list
Initially populates the buffer to be initial_state.
"""
self._max_length = max_length
super().__init__(initial_state, maxlen=max_length)
def __eq__(self, other):
"""Returns self == other. It supports other being a list,
RingBuffer, or collections.deque.
"""
return list(self) == list(other)
def __str__(self):
"""Returns a string of the form
<RingBuffer of length `max_length` and `list`>
"""
return (f"<RingBuffer of length {self._max_length} and "
f"{list(self)}>")