ringbuffer

class pymisclib.ringbuffer.BufferEmptyError

Raised when a buffer is empty and an attempt is made to read from it.

class pymisclib.ringbuffer.BufferFullError

Raised when a buffer is full and an attempt is made to write to it.

class pymisclib.ringbuffer.RingBuffer(capacity: int, logger: ~logging.Logger = <Logger pymisclib.ringbuffer (WARNING)>)

A fixed-size ringbuffer.

New entries are added to the end of the buffer, the oldest entry is read first. Once the buffer has reached full capacity, the newest entry to be added will overwrite the oldest.

The capacity should be chosen carefully to ensure that adding new entries never overtakes reading old ones (this will raise an exception). Simply opting for the largest possible buffer is not usually an optimal strategy because the entries are never freed (i.e. the buffer never shrinks in size), potentially consuming large amounts of memory.

__init__(capacity: int, logger: ~logging.Logger = <Logger pymisclib.ringbuffer (WARNING)>)
Parameters:
  • capacity (int) – Maximum number of entries the buffer can hold.

  • logger (logging.Logger) – Logger instance for diagnostics.

add(entry: Any)

Add an entry to the buffer.

Raises:

BufferFullError – if the buffer is full of unread entries.

get() Any

Read the oldest unread entry from the buffer.

Raises:

BufferEmptyError – if the buffer contains no unread entries.

get_unread() list[Any]

Read all unread entries from the buffer.

Returns list[Any]:

A list of all unread entries. Empty list if the buffer is empty.

property capacity: int

Capacity of the buffer.

property num_unread: int

Number of entries that may be read from the buffer.

property size: int

Current number of entries in the buffer.