Skip to content

Commit 4b4f391

Browse files
committed
Rename microgrid.battery_poolmicrogrid.new_battery_pool
This makes it explicit that each call to this function creates a new instance of the BatteryPool class, which would hopefully encourage them to hold on to references to the created objects, rather than create and discard after each use. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 4087414 commit 4b4f391

File tree

12 files changed

+41
-37
lines changed

12 files changed

+41
-37
lines changed

benchmarks/power_distribution/power_distributor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def send_requests(batteries: set[int], request_num: int) -> list[Result]:
5050
Returns:
5151
List of the results from the PowerDistributingActor.
5252
"""
53-
battery_pool = microgrid.battery_pool(priority=5, component_ids=batteries)
53+
battery_pool = microgrid.new_battery_pool(priority=5, component_ids=batteries)
5454
results_rx = battery_pool.power_status.new_receiver()
5555
result: list[Any] = []
5656
for _ in range(request_num):

examples/battery_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async def main() -> None:
3030
resampler_config=ResamplerConfig(resampling_period=timedelta(seconds=1.0)),
3131
)
3232

33-
battery_pool = microgrid.battery_pool(priority=5)
33+
battery_pool = microgrid.new_battery_pool(priority=5)
3434
receivers = [
3535
battery_pool.soc.new_receiver(limit=1),
3636
battery_pool.capacity.new_receiver(limit=1),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _add_system_bounds_tracker(self, component_ids: frozenset[int]) -> None:
171171
bounds_receiver: Receiver[SystemBounds]
172172
# pylint: disable=protected-access
173173
if self._component_category is ComponentCategory.BATTERY:
174-
battery_pool = microgrid.battery_pool(
174+
battery_pool = microgrid.new_battery_pool(
175175
priority=-sys.maxsize - 1, component_ids=component_ids
176176
)
177177
bounds_receiver = battery_pool._system_power_bounds.new_receiver()

src/frequenz/sdk/microgrid/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@
101101
102102
## Batteries
103103
104-
The total Battery power is available through
105-
[`battery_pool`][frequenz.sdk.microgrid.battery_pool]'s
104+
The total Battery power is available through the
105+
[`battery_pool`][frequenz.sdk.microgrid.new_battery_pool]'s
106106
[`power`][frequenz.sdk.timeseries.battery_pool.BatteryPool.power]. The battery pool by
107107
default uses all batteries available at a location, but battery pool instances can be
108108
created for subsets of batteries if necessary, by specifying the battery ids.
@@ -143,9 +143,9 @@
143143
The SDK provides a unified interface for interacting with sets of Batteries, EV
144144
chargers and PV arrays, through their corresponding `Pool`s.
145145
146-
* [Battery pool][frequenz.sdk.microgrid.battery_pool]
147146
* [EV charger pool][frequenz.sdk.microgrid.ev_charger_pool]
148147
* [PV pool][frequenz.sdk.microgrid.pv_pool]
148+
* [Battery pool][frequenz.sdk.microgrid.new_battery_pool]
149149
150150
All of them provide support for streaming aggregated data and for setting the
151151
power values of the components.
@@ -226,12 +226,12 @@
226226
from ..actor import ResamplerConfig
227227
from . import _data_pipeline, connection_manager
228228
from ._data_pipeline import (
229-
battery_pool,
230229
consumer,
231230
ev_charger_pool,
232231
frequency,
233232
grid,
234233
logical_meter,
234+
new_battery_pool,
235235
producer,
236236
pv_pool,
237237
voltage,
@@ -256,11 +256,11 @@ async def initialize(server_url: str, resampler_config: ResamplerConfig) -> None
256256
__all__ = [
257257
"initialize",
258258
"consumer",
259-
"battery_pool",
260259
"ev_charger_pool",
261260
"grid",
262261
"frequency",
263262
"logical_meter",
263+
"new_battery_pool",
264264
"producer",
265265
"pv_pool",
266266
"voltage",

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def pv_pool(
344344
set_operating_point=set_operating_point,
345345
)
346346

347-
def battery_pool(
347+
def new_battery_pool(
348348
self,
349349
*,
350350
priority: int,
@@ -571,7 +571,7 @@ def ev_charger_pool(
571571
)
572572

573573

574-
def battery_pool(
574+
def new_battery_pool(
575575
*,
576576
priority: int,
577577
component_ids: abc.Set[int] | None = None,
@@ -608,7 +608,7 @@ def battery_pool(
608608
Returns:
609609
A `BatteryPool` instance.
610610
"""
611-
return _get().battery_pool(
611+
return _get().new_battery_pool(
612612
priority=priority,
613613
component_ids=component_ids,
614614
name=name,

src/frequenz/sdk/timeseries/battery_pool/_battery_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def __init__(
6565
6666
!!! note
6767
`BatteryPool` instances are not meant to be created directly by users. Use
68-
the [`microgrid.battery_pool`][frequenz.sdk.microgrid.battery_pool] method
69-
for creating `BatteryPool` instances.
68+
the [`microgrid.new_battery_pool`][frequenz.sdk.microgrid.new_battery_pool]
69+
method for creating `BatteryPool` instances.
7070
7171
Args:
7272
pool_ref_store: The battery pool reference store instance.

src/frequenz/sdk/timeseries/battery_pool/_result_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def adjust_to_bounds(self, power: Power) -> tuple[Power | None, Power | None]:
6666
```python
6767
from frequenz.sdk import microgrid
6868
69-
power_status_rx = microgrid.battery_pool(
69+
power_status_rx = microgrid.new_battery_pool(
7070
priority=5,
7171
).power_status.new_receiver()
7272
power_status = await power_status_rx.receive()

src/frequenz/sdk/timeseries/formula_engine/_formula_engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class FormulaEngine(
253253
```python
254254
from frequenz.sdk import microgrid
255255
256-
battery_pool = microgrid.battery_pool(priority=5)
256+
battery_pool = microgrid.new_battery_pool(priority=5)
257257
258258
async for power in battery_pool.power.new_receiver():
259259
print(f"{power=}")
@@ -277,8 +277,8 @@ class FormulaEngine(
277277
from frequenz.sdk import microgrid
278278
279279
logical_meter = microgrid.logical_meter()
280-
battery_pool = microgrid.battery_pool(priority=5)
281280
ev_charger_pool = microgrid.ev_charger_pool(priority=5)
281+
battery_pool = microgrid.new_battery_pool(priority=5)
282282
grid = microgrid.grid()
283283
284284
# apply operations on formula engines to create a formula engine that would

tests/microgrid/test_datapipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def test_actors_started(
6868
)
6969
mock_client.initialize(mocker)
7070

71-
datapipeline.battery_pool(priority=5)
71+
datapipeline.new_battery_pool(priority=5)
7272

7373
assert datapipeline._battery_power_wrapper._power_distributing_actor is not None
7474
await asyncio.sleep(1)

tests/timeseries/_battery_pool/test_battery_pool.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ async def setup_all_batteries(mocker: MockerFixture) -> AsyncIterator[SetupArgs]
151151
# the scope of this tests. This tests should cover BatteryPool only.
152152
# We use our own battery status channel, where we easily control set of working
153153
# batteries.
154-
battery_pool = microgrid.battery_pool(priority=5)
154+
battery_pool = microgrid.new_battery_pool(priority=5)
155155

156156
dp = microgrid._data_pipeline._DATA_PIPELINE
157157
assert dp is not None
@@ -205,7 +205,7 @@ async def setup_batteries_pool(mocker: MockerFixture) -> AsyncIterator[SetupArgs
205205
# batteries.
206206
all_batteries = list(get_components(mock_microgrid, ComponentCategory.BATTERY))
207207

208-
battery_pool = microgrid.battery_pool(
208+
battery_pool = microgrid.new_battery_pool(
209209
priority=5, component_ids=set(all_batteries[:2])
210210
)
211211

@@ -543,7 +543,7 @@ async def test_batter_pool_power_no_batteries(mocker: MockerFixture) -> None:
543543
)
544544
)
545545
await mockgrid.start(mocker)
546-
battery_pool = microgrid.battery_pool(priority=5)
546+
battery_pool = microgrid.new_battery_pool(priority=5)
547547
power_receiver = battery_pool.power.new_receiver()
548548

549549
await mockgrid.mock_resampler.send_non_existing_component_value()
@@ -560,7 +560,7 @@ async def test_battery_pool_power_with_no_inverters(mocker: MockerFixture) -> No
560560
await mockgrid.start(mocker)
561561

562562
with pytest.raises(RuntimeError):
563-
microgrid.battery_pool(priority=5)
563+
microgrid.new_battery_pool(priority=5)
564564

565565

566566
async def test_battery_pool_power_incomplete_bat_request(mocker: MockerFixture) -> None:
@@ -582,7 +582,7 @@ async def test_battery_pool_power_incomplete_bat_request(mocker: MockerFixture)
582582

583583
with pytest.raises(FormulaGenerationError):
584584
# Request only two of the three batteries behind the inverters
585-
battery_pool = microgrid.battery_pool(
585+
battery_pool = microgrid.new_battery_pool(
586586
priority=5, component_ids=set([bats[1].component_id, bats[0].component_id])
587587
)
588588
power_receiver = battery_pool.power.new_receiver()
@@ -592,7 +592,7 @@ async def test_battery_pool_power_incomplete_bat_request(mocker: MockerFixture)
592592

593593
async def _test_battery_pool_power(mockgrid: MockMicrogrid) -> None:
594594
async with mockgrid:
595-
battery_pool = microgrid.battery_pool(priority=5)
595+
battery_pool = microgrid.new_battery_pool(priority=5)
596596
power_receiver = battery_pool.power.new_receiver()
597597

598598
await mockgrid.mock_resampler.send_bat_inverter_power([2.0, 3.0])

0 commit comments

Comments
 (0)