Skip to content

Commit 6d4516f

Browse files
committed
docs: Indent examples
This is needed to get them properly rendered and in a collapsable way. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 56668d1 commit 6d4516f

File tree

5 files changed

+111
-100
lines changed

5 files changed

+111
-100
lines changed

src/frequenz/channels/anycast.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,38 @@ class Anycast(Generic[T]):
2424
Uses an [deque][collections.deque] internally, so Anycast channels are not
2525
thread-safe.
2626
27+
When there are multiple channel receivers, they can be awaited
28+
simultaneously using [Select][frequenz.channels.Select],
29+
[Merge][frequenz.channels.Merge] or
30+
[MergeNamed][frequenz.channels.MergeNamed].
31+
2732
Example:
28-
``` python
29-
async def send(sender: channel.Sender) -> None:
30-
while True:
31-
next = random.randint(3, 17)
32-
print(f"sending: {next}")
33-
await sender.send(next)
33+
``` python
34+
async def send(sender: channel.Sender) -> None:
35+
while True:
36+
next = random.randint(3, 17)
37+
print(f"sending: {next}")
38+
await sender.send(next)
3439
3540
36-
async def recv(id: int, receiver: channel.Receiver) -> None:
37-
while True:
38-
next = await receiver.receive()
39-
print(f"receiver_{id} received {next}")
40-
await asyncio.sleep(0.1) # sleep (or work) with the data
41+
async def recv(id: int, receiver: channel.Receiver) -> None:
42+
while True:
43+
next = await receiver.receive()
44+
print(f"receiver_{id} received {next}")
45+
await asyncio.sleep(0.1) # sleep (or work) with the data
4146
4247
43-
acast = channel.Anycast()
48+
acast = channel.Anycast()
4449
45-
sender = acast.get_sender()
46-
receiver_1 = acast.get_receiver()
50+
sender = acast.get_sender()
51+
receiver_1 = acast.get_receiver()
4752
48-
asyncio.create_task(send(sender))
53+
asyncio.create_task(send(sender))
4954
50-
await recv(1, receiver_1)
51-
```
55+
await recv(1, receiver_1)
56+
```
5257
53-
Check the `tests` and `benchmarks` directories for more examples. When
54-
there are multiple channel receivers, they can be awaited simultaneously
55-
using `channel.Select` or `channel.Merge`.
58+
Check the `tests` and `benchmarks` directories for more examples.
5659
"""
5760

5861
def __init__(self, maxsize: int = 10) -> None:

src/frequenz/channels/broadcast.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,38 @@ class Broadcast(Generic[T]):
2828
append/pop operations on either side of a [deque][collections.deque], which
2929
are thread-safe. Because of this, `Broadcast` channels are thread-safe.
3030
31+
When there are multiple channel receivers, they can be awaited
32+
simultaneously using [Select][frequenz.channels.Select],
33+
[Merge][frequenz.channels.Merge] or
34+
[MergeNamed][frequenz.channels.MergeNamed].
35+
3136
Example:
32-
``` python
33-
async def send(sender: channel.Sender) -> None:
34-
while True:
35-
next = random.randint(3, 17)
36-
print(f"sending: {next}")
37-
await sender.send(next)
37+
``` python
38+
async def send(sender: channel.Sender) -> None:
39+
while True:
40+
next = random.randint(3, 17)
41+
print(f"sending: {next}")
42+
await sender.send(next)
3843
3944
40-
async def recv(id: int, receiver: channel.Receiver) -> None:
41-
while True:
42-
next = await receiver.receive()
43-
print(f"receiver_{id} received {next}")
44-
await asyncio.sleep(0.1) # sleep (or work) with the data
45+
async def recv(id: int, receiver: channel.Receiver) -> None:
46+
while True:
47+
next = await receiver.receive()
48+
print(f"receiver_{id} received {next}")
49+
await asyncio.sleep(0.1) # sleep (or work) with the data
4550
4651
47-
bcast = channel.Broadcast()
52+
bcast = channel.Broadcast()
4853
49-
sender = bcast.get_sender()
50-
receiver_1 = bcast.get_receiver()
54+
sender = bcast.get_sender()
55+
receiver_1 = bcast.get_receiver()
5156
52-
asyncio.create_task(send(sender))
57+
asyncio.create_task(send(sender))
5358
54-
await recv(1, receiver_1)
55-
```
59+
await recv(1, receiver_1)
60+
```
5661
57-
Check the `tests` and `benchmarks` directories for more examples. When
58-
there are multiple channel receivers, they can be awaited simultaneously
59-
using `channel.Select` or `channel.Merge`.
62+
Check the `tests` and `benchmarks` directories for more examples.
6063
"""
6164

6265
def __init__(self, name: str, resend_latest: bool = False) -> None:

src/frequenz/channels/merge.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
class Merge(Receiver[T]):
1414
"""Merge messages coming from multiple channels into a single stream.
1515
16-
For example, if there are two channel receivers with the same type, they
17-
can be awaited together, and their results merged into a single stream, by
18-
using `Merge` like this:
16+
Example:
17+
For example, if there are two channel receivers with the same type,
18+
they can be awaited together, and their results merged into a single
19+
stream, by using `Merge` like this:
1920
20-
```python
21-
merge = Merge(receiver1, receiver2)
22-
while msg := await merge.receive():
23-
# do something with msg
24-
pass
25-
```
21+
```python
22+
merge = Merge(receiver1, receiver2)
23+
while msg := await merge.receive():
24+
# do something with msg
25+
pass
26+
```
2627
"""
2728

2829
def __init__(self, *args: Receiver[T]) -> None:

src/frequenz/channels/select.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,31 @@ class _Selected:
3131
class Select:
3232
"""Select the next available message from a group of AsyncIterators.
3333
34-
For example, if there are two async iterators that you want to
35-
simultaneously wait on, this can be done with:
36-
37-
```python
38-
select = Select(name1 = receiver1, name2 = receiver2)
39-
while await select.ready():
40-
if msg := select.name1:
41-
if val := msg.inner:
42-
# do something with `val`
43-
pass
44-
else:
45-
# handle closure of receiver.
46-
pass
47-
elif msg := select.name2:
48-
# do something with `msg.inner`
49-
pass
50-
```
51-
5234
If `Select` was created with more `AsyncIterator` than what are read in
5335
the if-chain after each call to [ready()][frequenz.channels.Select.ready],
5436
messages coming in the additional async iterators are dropped, and
5537
a warning message is logged.
5638
5739
[Receiver][frequenz.channels.Receiver]s also function as AsyncIterator.
40+
41+
Example:
42+
For example, if there are two async iterators that you want to
43+
simultaneously wait on, this can be done with:
44+
45+
```python
46+
select = Select(name1 = receiver1, name2 = receiver2)
47+
while await select.ready():
48+
if msg := select.name1:
49+
if val := msg.inner:
50+
# do something with `val`
51+
pass
52+
else:
53+
# handle closure of receiver.
54+
pass
55+
elif msg := select.name2:
56+
# do something with `msg.inner`
57+
pass
58+
```
5859
"""
5960

6061
def __init__(self, **kwargs: AsyncIterator[Any]) -> None:

src/frequenz/channels/utils/timer.py

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,42 @@
1313
class Timer(Receiver[datetime]):
1414
"""A timer receiver that returns the timestamp every `interval` seconds.
1515
16-
Primarily for use with `channel.Select` calls.
17-
18-
When you want something to happen with a fixed period:
19-
```python
20-
timer = channel.Timer(30.0)
21-
select = Select(bat_1 = receiver1, timer = timer)
22-
while await select.ready():
23-
if msg := select.bat_1:
24-
if val := msg.inner:
25-
process_data(val)
26-
else:
27-
logging.warn("battery channel closed")
28-
elif ts := select.timer:
29-
# something to do once every 30 seconds
30-
pass
31-
```
32-
33-
When you want something to happen when nothing else has happened in a
34-
certain interval:
35-
```python
36-
timer = channel.Timer(30.0)
37-
select = Select(bat_1 = receiver1, timer = timer)
38-
while await select.ready():
39-
timer.reset()
40-
if msg := select.bat_1:
41-
if val := msg.inner:
42-
process_data(val)
43-
else:
44-
logging.warn("battery channel closed")
45-
elif ts := select.timer:
46-
# something to do if there's no battery data for 30 seconds
47-
pass
48-
```
16+
Primarily for use with [Select][frequenz.channels.Select].
17+
18+
Example:
19+
When you want something to happen with a fixed period:
20+
21+
```python
22+
timer = channel.Timer(30.0)
23+
select = Select(bat_1 = receiver1, timer = timer)
24+
while await select.ready():
25+
if msg := select.bat_1:
26+
if val := msg.inner:
27+
process_data(val)
28+
else:
29+
logging.warn("battery channel closed")
30+
if ts := select.timer:
31+
# something to do once every 30 seconds
32+
pass
33+
```
34+
35+
When you want something to happen when nothing else has happened in a
36+
certain interval:
37+
38+
```python
39+
timer = channel.Timer(30.0)
40+
select = Select(bat_1 = receiver1, timer = timer)
41+
while await select.ready():
42+
timer.reset()
43+
if msg := select.bat_1:
44+
if val := msg.inner:
45+
process_data(val)
46+
else:
47+
logging.warn("battery channel closed")
48+
if ts := select.timer:
49+
# something to do if there's no battery data for 30 seconds
50+
pass
51+
```
4952
"""
5053

5154
def __init__(self, interval: float) -> None:

0 commit comments

Comments
 (0)