Skip to content

Commit 661ef69

Browse files
committed
Improves the documentation on Receiver.filter
This also adds a new section to the documentation explaining how to filter to the user guide. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent a017f01 commit 661ef69

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,4 @@
22

33
## Summary
44

5-
<!-- Here goes a general summary of what this release is about -->
6-
7-
## Upgrading
8-
9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
10-
11-
## New Features
12-
13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
14-
15-
## Bug Fixes
16-
17-
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
5+
This release improves the documentation on `Receiver.filter`.

src/frequenz/channels/_receiver.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@
5555
[`map()`][frequenz.channels.Receiver.map] returns a new full receiver, so you can
5656
use it in any of the ways described above.
5757
58+
# Message Filtering
59+
60+
If you need to filter the received messages, receivers provide a
61+
[`filter()`][frequenz.channels.Receiver.filter] method to easily do so:
62+
63+
```python show_lines="6:"
64+
from frequenz.channels import Anycast
65+
66+
channel = Anycast[int](name="test-channel")
67+
receiver = channel.new_receiver()
68+
69+
async for message in receiver.filter(lambda x: x % 2 == 0):
70+
print(message) # Only even numbers will be printed
71+
```
72+
73+
As with [`map()`][frequenz.channels.Receiver.map],
74+
[`filter()`][frequenz.channels.Receiver.filter] returns a new full receiver, so you can
75+
use it in any of the ways described above.
76+
5877
# Error Handling
5978
6079
!!! Tip inline end
@@ -254,10 +273,11 @@ def filter(
254273
original receiver and use that instead.
255274
256275
Args:
257-
filter_function: The function to be applied on incoming messages.
276+
filter_function: The function to be applied on incoming messages to
277+
determine if they should be received.
258278
259279
Returns:
260-
A new receiver that applies the function on the received messages.
280+
A new receiver that only receives messages that pass the filter.
261281
"""
262282
return _Filter(receiver=self, filter_function=filter_function)
263283

0 commit comments

Comments
 (0)