Skip to content

Commit 3e0bfb6

Browse files
committed
Add count_valid method to ring buffer
The current len magic returns the count of samples in the ring buffer that are determined valid. This definition is not unambiguously obvious to the caller. The current logic is therefore moved to its own more descriptive method which in the first step is called by the len magic. This prepares removal of the len magic in subsequent changes. Signed-off-by: cwasicki <[email protected]>
1 parent 910bd9c commit 3e0bfb6

File tree

1 file changed

+7
-3
lines changed
  • src/frequenz/sdk/timeseries/_ringbuffer

1 file changed

+7
-3
lines changed

src/frequenz/sdk/timeseries/_ringbuffer/buffer.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,11 @@ def __getitem__(self, index_or_slice: SupportsIndex | slice) -> float | FloatArr
594594
"""
595595
return self._buffer.__getitem__(index_or_slice)
596596

597-
def __len__(self) -> int:
598-
"""Return the amount of items that this container currently holds.
597+
def count_valid(self) -> int:
598+
"""Count the number of valid items that this buffer currently holds.
599599
600600
Returns:
601-
The length.
601+
The number of valid items in this buffer.
602602
"""
603603
if self._datetime_newest == self._DATETIME_MIN:
604604
return 0
@@ -624,3 +624,7 @@ def __len__(self) -> int:
624624
return len(self._buffer) - start_index + end_index + 1 - sum_missing_entries
625625

626626
return end_index + 1 - start_index - sum_missing_entries
627+
628+
def __len__(self) -> int:
629+
"""Return the count of valid items that this container currently holds."""
630+
return self.count_valid()

0 commit comments

Comments
 (0)