Skip to content

Commit 35863bb

Browse files
committed
Apply suggestions from code review
- Rename `_bounds_methods.py` -> `_bounds.py`. - Make methods in `_bounds.py` not private. - Move private attribute definitions in `Report` class to the end. - Improve docstring example for `check_exclusion_bounds_overlap` method. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 7da41bf commit 35863bb

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

src/frequenz/sdk/actor/_power_managing/_base_classes.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from ... import timeseries
1515
from ...timeseries import Power
16-
from ._bounds_methods import _clamp_to_bounds
16+
from . import _bounds
1717

1818
if typing.TYPE_CHECKING:
1919
from ...timeseries.battery_pool import PowerMetrics
@@ -50,6 +50,12 @@ class Report:
5050
target_power: Power | None
5151
"""The currently set power for the batteries."""
5252

53+
distribution_result: power_distributing.Result | None
54+
"""The result of the last power distribution.
55+
56+
This is `None` if no power distribution has been performed yet.
57+
"""
58+
5359
_inclusion_bounds: timeseries.Bounds[Power] | None
5460
"""The available inclusion bounds for the batteries, for the actor's priority.
5561
@@ -67,12 +73,6 @@ class Report:
6773
priorities.
6874
"""
6975

70-
distribution_result: power_distributing.Result | None
71-
"""The result of the last power distribution.
72-
73-
This is `None` if no power distribution has been performed yet.
74-
"""
75-
7676
@property
7777
def bounds(self) -> timeseries.Bounds[Power] | None:
7878
"""The bounds for the batteries.
@@ -140,7 +140,7 @@ def adjust_to_bounds(self, power: Power) -> tuple[Power | None, Power | None]:
140140
if self._inclusion_bounds is None:
141141
return None, None
142142

143-
return _clamp_to_bounds(
143+
return _bounds.clamp_to_bounds(
144144
power,
145145
self._inclusion_bounds.lower,
146146
self._inclusion_bounds.upper,

src/frequenz/sdk/actor/_power_managing/_bounds_methods.py renamed to src/frequenz/sdk/actor/_power_managing/_bounds.py

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

4-
"""Methods for checking and clamping bounds and power values to exclusion bounds."""
4+
"""Utilities for checking and clamping bounds and power values to exclusion bounds."""
55

66
from ...timeseries import Bounds, Power
77

88

9-
def _check_exclusion_bounds_overlap(
9+
def check_exclusion_bounds_overlap(
1010
lower_bound: Power,
1111
upper_bound: Power,
1212
exclusion_bounds: Bounds[Power] | None,
1313
) -> tuple[bool, bool]:
1414
"""Check if the given bounds overlap with the given exclusion bounds.
1515
16-
When only the upper bound overlaps with exclusion bounds, the usable range is
17-
between the lower bound and the lower exclusion bound, like below.
16+
Example:
1817
19-
===lb+++++++ex----ub-------ex===
18+
lower upper
19+
.----- exclusion zone -----.
20+
-----|✓✓✓✓✓✓✓✓✓✓✓✓|xxxxxxxxxxxxxxx|----------|----
21+
`-- usable --'-- exclusion --´
22+
| overlap |
23+
| |
24+
lower upper
25+
bound bound
26+
(inside the exclusion zone)
2027
21-
When only the lower bound overlaps with exclusion bounds, the usable range is
22-
between the upper exclusion bound and the upper bound.
23-
24-
===ex------lb------ex++++++ub===
25-
26-
Both bounds overlapping with exclusion bounds (or given bounds are fully contained
27-
within exclusion bounds). In this case, there is no usable range.
28-
29-
===ex------lb------ub------ex===
28+
Resulting in `(False, True)` because only the upper bound is inside the
29+
exclusion zone.
3030
3131
Args:
3232
lower_bound: The lower bound to check.
@@ -52,7 +52,7 @@ def _check_exclusion_bounds_overlap(
5252
return bounded_lower, bounded_upper
5353

5454

55-
def _adjust_exclusion_bounds(
55+
def adjust_exclusion_bounds(
5656
lower_bound: Power,
5757
upper_bound: Power,
5858
exclusion_bounds: Bounds[Power] | None,
@@ -75,7 +75,7 @@ def _adjust_exclusion_bounds(
7575
#
7676
# And if the given bounds overlap with the exclusion bounds on one side, then clamp
7777
# the given bounds on that side.
78-
match _check_exclusion_bounds_overlap(lower_bound, upper_bound, exclusion_bounds):
78+
match check_exclusion_bounds_overlap(lower_bound, upper_bound, exclusion_bounds):
7979
case (True, True):
8080
return Power.zero(), Power.zero()
8181
case (False, True):
@@ -87,7 +87,7 @@ def _adjust_exclusion_bounds(
8787

8888
# Just 20 lines of code in this function, but unfortunately 8 of those are return
8989
# statements, and that's too many for pylint.
90-
def _clamp_to_bounds( # pylint: disable=too-many-return-statements
90+
def clamp_to_bounds( # pylint: disable=too-many-return-statements
9191
value: Power,
9292
lower_bound: Power,
9393
upper_bound: Power,
@@ -116,7 +116,7 @@ def _clamp_to_bounds( # pylint: disable=too-many-return-statements
116116
# given power is in that overlap region, clamp it to the exclusion bounds on that
117117
# side.
118118
if exclusion_bounds is not None:
119-
match _check_exclusion_bounds_overlap(
119+
match check_exclusion_bounds_overlap(
120120
lower_bound, upper_bound, exclusion_bounds
121121
):
122122
case (True, True):

src/frequenz/sdk/actor/_power_managing/_matryoshka.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@
2626

2727
from ... import timeseries
2828
from ...timeseries import Power
29+
from . import _bounds
2930
from ._base_classes import BaseAlgorithm, Proposal, Report
30-
from ._bounds_methods import (
31-
_adjust_exclusion_bounds,
32-
_check_exclusion_bounds_overlap,
33-
_clamp_to_bounds,
34-
)
3531
from ._sorted_set import SortedSet
3632

3733
if typing.TYPE_CHECKING:
@@ -89,7 +85,7 @@ def _calc_target_power(
8985
if upper_bound < lower_bound:
9086
break
9187
if next_proposal.preferred_power:
92-
match _clamp_to_bounds(
88+
match _bounds.clamp_to_bounds(
9389
next_proposal.preferred_power,
9490
lower_bound,
9591
upper_bound,
@@ -111,14 +107,14 @@ def _calc_target_power(
111107
# If the bounds from the current proposal are fully within the exclusion
112108
# bounds, then don't use them to narrow the bounds further. This allows
113109
# subsequent proposals to not be blocked by the current proposal.
114-
match _check_exclusion_bounds_overlap(
110+
match _bounds.check_exclusion_bounds_overlap(
115111
proposal_lower, proposal_upper, exclusion_bounds
116112
):
117113
case (True, True):
118114
continue
119115
lower_bound = max(lower_bound, proposal_lower)
120116
upper_bound = min(upper_bound, proposal_upper)
121-
lower_bound, upper_bound = _adjust_exclusion_bounds(
117+
lower_bound, upper_bound = _bounds.adjust_exclusion_bounds(
122118
lower_bound, upper_bound, exclusion_bounds
123119
)
124120

@@ -248,15 +244,15 @@ def get_status(
248244
break
249245
proposal_lower = next_proposal.bounds.lower or lower_bound
250246
proposal_upper = next_proposal.bounds.upper or upper_bound
251-
match _check_exclusion_bounds_overlap(
247+
match _bounds.check_exclusion_bounds_overlap(
252248
proposal_lower, proposal_upper, exclusion_bounds
253249
):
254250
case (True, True):
255251
continue
256252
calc_lower_bound = max(lower_bound, proposal_lower)
257253
calc_upper_bound = min(upper_bound, proposal_upper)
258254
if calc_lower_bound <= calc_upper_bound:
259-
lower_bound, upper_bound = _adjust_exclusion_bounds(
255+
lower_bound, upper_bound = _bounds.adjust_exclusion_bounds(
260256
calc_lower_bound, calc_upper_bound, exclusion_bounds
261257
)
262258
else:

0 commit comments

Comments
 (0)