Skip to content

Commit 23912e9

Browse files
committed
Rename OnlyIfPrevious to WithPrevious
The new name makes is more clear that this predicate is adding an extra condition to the message filtering based on the previous message. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 3dc8024 commit 23912e9

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
### Experimental
66

7-
- A new predicate, `OnlyIfPrevious`, to `filter()` messages based on the previous message.
7+
- A new composable predicate, `WithPrevious`, to filter messages based on the previous message.

src/frequenz/channels/experimental/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"""
1111

1212
from ._pipe import Pipe
13-
from ._predicates import OnlyIfPrevious
13+
from ._predicates import WithPrevious
1414
from ._relay_sender import RelaySender
1515

1616
__all__ = [
17-
"OnlyIfPrevious",
17+
"WithPrevious",
1818
"Pipe",
1919
"RelaySender",
2020
]

src/frequenz/channels/experimental/_predicates.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __str__(self) -> str:
2020
_SENTINEL: Final[_Sentinel] = _Sentinel()
2121

2222

23-
class OnlyIfPrevious(Generic[ChannelMessageT]):
23+
class WithPrevious(Generic[ChannelMessageT]):
2424
"""A predicate to check if a message has a particular relationship with the previous one.
2525
2626
This predicate can be used to filter out messages based on a custom condition on the
@@ -31,10 +31,10 @@ class OnlyIfPrevious(Generic[ChannelMessageT]):
3131
Example: Receiving only messages that are not the same instance as the previous one.
3232
```python
3333
from frequenz.channels import Broadcast
34-
from frequenz.channels.experimental import OnlyIfPrevious
34+
from frequenz.channels.experimental import WithPrevious
3535
3636
channel = Broadcast[int | bool](name="example")
37-
receiver = channel.new_receiver().filter(OnlyIfPrevious(lambda old, new: old is not new))
37+
receiver = channel.new_receiver().filter(WithPrevious(lambda old, new: old is not new))
3838
sender = channel.new_sender()
3939
4040
# This message will be received as it is the first message.
@@ -53,11 +53,11 @@ class OnlyIfPrevious(Generic[ChannelMessageT]):
5353
Example: Receiving only messages if they are bigger than the previous one.
5454
```python
5555
from frequenz.channels import Broadcast
56-
from frequenz.channels.experimental import OnlyIfPrevious
56+
from frequenz.channels.experimental import WithPrevious
5757
5858
channel = Broadcast[int](name="example")
5959
receiver = channel.new_receiver().filter(
60-
OnlyIfPrevious(lambda old, new: new > old, first_is_true=False)
60+
WithPrevious(lambda old, new: new > old, first_is_true=False)
6161
)
6262
sender = channel.new_sender()
6363

tests/experimental/test_predicates_only_if_previous.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# License: MIT
22
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
33

4-
"""Tests for the OnlyIfPrevious implementation."""
4+
"""Tests for the WithPrevious implementation."""
55

66
from dataclasses import dataclass
77
from typing import Callable, Generic, TypeVar
88

99
import pytest
1010

11-
from frequenz.channels.experimental import OnlyIfPrevious
11+
from frequenz.channels.experimental import WithPrevious
1212

1313
_T = TypeVar("_T")
1414

1515

1616
@dataclass(frozen=True, kw_only=True)
1717
class PredicateTestCase(Generic[_T]):
18-
"""Test case for testing OnlyIfPrevious behavior with different predicates."""
18+
"""Test case for testing WithPrevious behavior with different predicates."""
1919

2020
title: str
2121
messages: list[_T]
@@ -113,12 +113,12 @@ def is_not_same_instance(old: object, new: object) -> bool:
113113
ids=lambda test_case: test_case.title,
114114
)
115115
def test_only_if_previous(test_case: PredicateTestCase[_T]) -> None:
116-
"""Test the OnlyIfPrevious with different predicates and sequences.
116+
"""Test the WithPrevious with different predicates and sequences.
117117
118118
Args:
119119
test_case: The test case containing the input values and expected results.
120120
"""
121-
only_if_previous = OnlyIfPrevious(
121+
only_if_previous = WithPrevious(
122122
test_case.predicate,
123123
first_is_true=test_case.first_is_true,
124124
)
@@ -127,9 +127,9 @@ def test_only_if_previous(test_case: PredicateTestCase[_T]) -> None:
127127

128128

129129
def test_only_if_previous_state_independence() -> None:
130-
"""Test that multiple OnlyIfPrevious instances maintain independent state."""
131-
only_if_previous1 = OnlyIfPrevious(is_greater)
132-
only_if_previous2 = OnlyIfPrevious(is_greater)
130+
"""Test that multiple WithPrevious instances maintain independent state."""
131+
only_if_previous1 = WithPrevious(is_greater)
132+
only_if_previous2 = WithPrevious(is_greater)
133133

134134
# First message should be accepted (first_is_true default is True)
135135
assert only_if_previous1(1) is True
@@ -141,17 +141,17 @@ def test_only_if_previous_state_independence() -> None:
141141

142142

143143
def test_only_if_previous_str_representation() -> None:
144-
"""Test the string representation of OnlyIfPrevious."""
145-
only_if_previous = OnlyIfPrevious(is_greater)
146-
assert str(only_if_previous) == "OnlyIfPrevious:is_greater"
144+
"""Test the string representation of WithPrevious."""
145+
only_if_previous = WithPrevious(is_greater)
146+
assert str(only_if_previous) == "WithPrevious:is_greater"
147147
assert (
148-
repr(only_if_previous) == f"<OnlyIfPrevious: {is_greater!r} first_is_true=True>"
148+
repr(only_if_previous) == f"<WithPrevious: {is_greater!r} first_is_true=True>"
149149
)
150150

151151

152152
def test_only_if_previous_sentinel_str() -> None:
153153
"""Test the string representation of the sentinel value."""
154-
only_if_previous = OnlyIfPrevious(always_true)
154+
only_if_previous = WithPrevious(always_true)
155155

156156
# Access the private attribute for testing purposes
157157
# pylint: disable=protected-access

0 commit comments

Comments
 (0)