Skip to content

Commit 3dc8024

Browse files
committed
Revert "Add a new predicate to skip messages that are equal to the previous one"
With the upcoming name change of `OnlyIfPrevious` to `WithPrevious`, it doesn't look like `filter(WithPrevious(lambda prev, new: prev != new))` is so ugly and verbose to justify having `ChangedOnly` as a special case. Specially since the name is a bit confusing, and not clear enough about what it does. The verbose alternative make it more clear that it's comparing to the previous message. This reverts commit 2b9541c. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 90091ed commit 3dc8024

File tree

4 files changed

+1
-183
lines changed

4 files changed

+1
-183
lines changed

RELEASE_NOTES.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55
### Experimental
66

77
- A new predicate, `OnlyIfPrevious`, to `filter()` messages based on the previous message.
8-
- A new special case of `OnlyIfPrevious`, `ChangedOnly`, to skip messages if they are equal to the previous message.

src/frequenz/channels/experimental/__init__.py

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

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

1616
__all__ = [
17-
"ChangedOnly",
1817
"OnlyIfPrevious",
1918
"Pipe",
2019
"RelaySender",

src/frequenz/channels/experimental/_predicates.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ class OnlyIfPrevious(Generic[ChannelMessageT]):
2828
process messages only if they satisfy a particular condition with respect to the
2929
previous message.
3030
31-
Tip:
32-
If you want to use `==` as predicate, you can use the
33-
[`ChangedOnly`][frequenz.channels.experimental.ChangedOnly] predicate.
34-
3531
Example: Receiving only messages that are not the same instance as the previous one.
3632
```python
3733
from frequenz.channels import Broadcast
@@ -127,58 +123,3 @@ def __str__(self) -> str:
127123
def __repr__(self) -> str:
128124
"""Return a string representation of this instance."""
129125
return f"<{type(self).__name__}: {self._predicate!r} first_is_true={self._first_is_true!r}>"
130-
131-
132-
class ChangedOnly(OnlyIfPrevious[object]):
133-
"""A predicate to check if a message is different from the previous one.
134-
135-
This predicate can be used to filter out messages that are the same as the previous
136-
one. This can be useful in cases where you want to avoid processing duplicate
137-
messages.
138-
139-
Warning:
140-
This predicate uses the `!=` operator to compare messages, which includes all
141-
the weirdnesses of Python's equality comparison (e.g., `1 == 1.0`, `True == 1`,
142-
`True == 1.0`, `False == 0` are all `True`).
143-
144-
If you need to use a different comparison, you can create a custom predicate
145-
using [`OnlyIfPrevious`][frequenz.channels.experimental.OnlyIfPrevious].
146-
147-
Example:
148-
```python
149-
from frequenz.channels import Broadcast
150-
from frequenz.channels.experimental import ChangedOnly
151-
152-
channel = Broadcast[int](name="skip_duplicates_test")
153-
receiver = channel.new_receiver().filter(ChangedOnly())
154-
sender = channel.new_sender()
155-
156-
# This message will be received as it is the first message.
157-
await sender.send(1)
158-
assert await receiver.receive() == 1
159-
160-
# This message will be skipped as it is the same as the previous one.
161-
await sender.send(1)
162-
163-
# This message will be received as it is different from the previous one.
164-
await sender.send(2)
165-
assert await receiver.receive() == 2
166-
```
167-
"""
168-
169-
def __init__(self, *, first_is_true: bool = True) -> None:
170-
"""Initialize this instance.
171-
172-
Args:
173-
first_is_true: Whether the first message should be considered as different
174-
from the previous one. Defaults to `True`.
175-
"""
176-
super().__init__(lambda old, new: old != new, first_is_true=first_is_true)
177-
178-
def __str__(self) -> str:
179-
"""Return a string representation of this instance."""
180-
return f"{type(self).__name__}"
181-
182-
def __repr__(self) -> str:
183-
"""Return a string representation of this instance."""
184-
return f"{type(self).__name__}(first_is_true={self._first_is_true!r})"

tests/experimental/test_predicates_changed_only.py

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)