Skip to content

Commit a8710e5

Browse files
authored
Improves the documentation on Receiver.filter (frequenz-floss#313)
This also adds a new section to the documentation explaining how to filter to the user guide.
2 parents a017f01 + 490dd52 commit a8710e5

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

RELEASE_NOTES.md

+1-13
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`.

docs/_scripts/macros.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from typing import Any
77

88
import markdown as md
9-
from griffe import Object
10-
from griffe.collections import ModulesCollection
9+
from griffe import ModulesCollection, Object
1110
from markdown.extensions import toc
1211
from mkdocs_macros import plugin as macros
1312
from mkdocstrings_handlers.python.handler import PythonHandler

src/frequenz/channels/_receiver.py

+22-2
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)