Skip to content

Commit bd720be

Browse files
committed
Split buckets updating into a separate method
Pylint complained about the method having too many branches, and even when using match intrinsically means too many branches, this small piece of code updating buckets might be more clear as a separate method. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent bba16d7 commit bd720be

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,7 @@ def calculate_target_power(
181181
if not self._validate_component_ids(component_ids, proposal, system_bounds):
182182
return None
183183

184-
if proposal is not None:
185-
bucket = self._component_buckets.setdefault(component_ids, set())
186-
if proposal in bucket:
187-
bucket.remove(proposal)
188-
if (
189-
proposal.preferred_power is not None
190-
or proposal.bounds.lower is not None
191-
or proposal.bounds.upper is not None
192-
):
193-
bucket.add(proposal)
194-
elif not bucket:
195-
del self._component_buckets[component_ids]
184+
self._update_buckets(component_ids, proposal)
196185

197186
# If there has not been any proposal for the given components, don't calculate a
198187
# target power and just return `None`.
@@ -223,6 +212,26 @@ def calculate_target_power(
223212

224213
return target_power
225214

215+
def _update_buckets(
216+
self, component_ids: frozenset[ComponentId], proposal: Proposal | None
217+
) -> None:
218+
"""Update the component buckets with the given proposal."""
219+
if proposal is None:
220+
return
221+
222+
if proposal is not None:
223+
bucket = self._component_buckets.setdefault(component_ids, set())
224+
if proposal in bucket:
225+
bucket.remove(proposal)
226+
if (
227+
proposal.preferred_power is not None
228+
or proposal.bounds.lower is not None
229+
or proposal.bounds.upper is not None
230+
):
231+
bucket.add(proposal)
232+
elif not bucket:
233+
del self._component_buckets[component_ids]
234+
226235
@override
227236
def get_status(
228237
self,

0 commit comments

Comments
 (0)