Skip to content

Commit 7e87989

Browse files
committed
Use at method in getitem magic of moving window
The incomplete implementation of single element access in the getitem magic is replaced by using the `at` method. Signed-off-by: cwasicki <[email protected]>
1 parent ed39f82 commit 7e87989

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
- In `OrderedRingBuffer` and `MovingWindow`:
2929
- Support for integer indices is added.
3030
- Add `count_covered` method to count the number of elements covered by the used time range.
31-
31+
- Add `at` method to `MovingWindow` to access a single element and use it in `__getitem__` magic to fully support single element access.
3232

3333

3434

3535
## Bug Fixes
3636

3737
- Fix rendering of diagrams in the documentation.
3838
- The `__getitem__` magic of the `MovingWindow` is fixed to support the same functionality that the `window` method provides.
39+
- Fixes incorrect implementation of single element access in `__getitem__` magic of `MovingWindow`.

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,11 @@ def __getitem__(self, key: SupportsIndex | datetime | slice) -> float | ArrayLik
418418
raise ValueError("Slicing with a step other than 1 is not supported.")
419419
return self.window(key.start, key.stop)
420420

421-
if self._buffer.count_valid() == 0:
422-
raise IndexError("The buffer is empty.")
423-
424421
if isinstance(key, datetime):
425-
_logger.debug("Returning value at time %s ", key)
426-
return self._buffer[self._buffer.to_internal_index(key)]
422+
return self.at(key)
423+
427424
if isinstance(key, SupportsIndex):
428-
_logger.debug("Returning value at index %s ", key)
429-
return self._buffer[key]
425+
return self.at(key.__index__())
430426

431427
raise TypeError(
432428
"Key has to be either a timestamp or an integer "

tests/timeseries/test_moving_window.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ async def test_access_window_by_index() -> None:
8383
window, sender = init_moving_window(timedelta(seconds=2))
8484
async with window:
8585
await push_logical_meter_data(sender, [1, 2, 3])
86-
assert np.array_equal(window[0], 3.0) # bug: should be 2
87-
assert np.array_equal(window[1], 2.0) # bug: should be 3
88-
assert np.array_equal(window[-1], 2.0) # bug: should be 3
89-
assert np.array_equal(window[-2], 3.0) # bug: should be 2
86+
assert np.array_equal(window[0], 2.0)
87+
assert np.array_equal(window[1], 3.0)
88+
assert np.array_equal(window[-1], 3.0)
89+
assert np.array_equal(window[-2], 2.0)
9090
with pytest.raises(IndexError):
9191
_ = window[3]
9292
with pytest.raises(IndexError):
@@ -102,13 +102,12 @@ async def test_access_window_by_timestamp() -> None:
102102
assert np.array_equal(window.at(dt(1)), 1.0)
103103
assert np.array_equal(window[dt(2)], 2.0)
104104
assert np.array_equal(window.at(dt(2)), 2.0)
105-
assert np.array_equal(window[dt(3)], 1.0) # bug: should raise
106105
with pytest.raises(IndexError):
107106
_ = window[dt(0)]
108107
with pytest.raises(IndexError):
109108
_ = window.at(dt(0))
110109
with pytest.raises(IndexError):
111-
_ = window[dt(4)]
110+
_ = window[dt(3)]
112111
with pytest.raises(IndexError):
113112
_ = window.at(dt(3))
114113

0 commit comments

Comments
 (0)