Overwriting Ring Buffer

Keep the newest N items in a fixed-size circular buffer, return the evicted value on overflow, and read the latest slice.

PythonData Integrity

Code

pythonsnippet
61 lines
from collections.abc import Iterator
from typing import Generic, TypeVar

T = TypeVar("T")


class RingBuffer(Generic[T]):
    def __init__(self, capacity: int) -> None:
        if capacity < 1:
            raise ValueError("capacity must be positive")
        self._slots: list[T | None] = [None] * capacity
        self._head = 0
        self._count = 0

    @property
    def capacity(self) -> int:
        return len(self._slots)

    def __len__(self) -> int:
        return self._count

    def push(self, item: T) -> T | None:
        evicted: T | None = None
        if self._count == self.capacity:
            evicted = self._slots[self._head]
            self._head = (self._head + 1) % self.capacity
            index = (self._head + self._count - 1) % self.capacity
        else:
            index = (self._head + self._count) % self.capacity
        self._slots[index] = item
        if self._count < self.capacity:
            self._count += 1
        return evicted

    def items(self) -> Iterator[T]:
        for offset in range(self._count):
            value = self._slots[(self._head + offset) % self.capacity]
            if value is not None:
                yield value

    def latest(self, count: int) -> list[T]:
        if count < 0:
            raise ValueError("count must not be negative")
        if count == 0:
            return []
        return list(self.items())[-count:]


def main() -> None:
    buffer: RingBuffer[str] = RingBuffer(3)
    for event in ["a", "b", "c"]:
        print("push", event, "evicted", buffer.push(event))
    print("full:", list(buffer.items()))
    print("push d evicted:", buffer.push("d"))
    print("after:", list(buffer.items()))
    print("latest 2:", buffer.latest(2))
    print("length:", len(buffer))


if __name__ == "__main__":
    main()

What the code does

Overwriting Ring Buffer comes down to the class RingBuffer and the functions __init__, capacity, __len__ and push.

Iteration happens inside for offset in range(self._count) — everything outside it runs once. When the guard condition holds, ValueError is thrown rather than a bogus value returned.

The return value is newly constructed — inputs are never handed back.

What to watch for

  • The guard capacity < 1 short-circuits the flow with an early raise when its condition holds.

Applying it

The primary signature is def capacity(self) -> int

The demo output is labeled (push, full:, push d evicted:); each label names the call that printed it.

The demo flow inside main is push, items and latest — run the file to see it end to end.

Statements that matter

def __init__(self, capacity: int) -> None:
    if capacity < 1:
        raise ValueError("capacity must be positive")
    self._slots: list[T | None] = [None] * capacity
    self._head = 0
    self._count = 0
def latest(self, count: int) -> list[T]:
    if count < 0:
        raise ValueError("count must not be negative")
    if count == 0:
        return []
    return list(self.items())[-count:]
def items(self) -> Iterator[T]:
    for offset in range(self._count):
        value = self._slots[(self._head + offset) % self.capacity]
        if value is not None:
            yield value

Interactive Tools for This Topic

FAQ

Does `capacity` modify its input?
No mutation happens — the function reads its input and returns a new value.
Does the code validate its input?
The code throws `ValueError` — the guard `capacity < 1` rejects bad states before the main logic runs. Handle it at the call site or let it propagate deliberately.
What does Overwriting Ring Buffer print when run?
No setup needed: run the file and look for labeled lines like `push`, `full:`, `push d evicted:` in the output.