Skip to content

Commit c4a458e

Browse files
committed
Interface updates
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent c864519 commit c4a458e

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ dev-pylint = [
7373
"frequenz-microgrid-component-graph[dev-mkdocs,dev-noxfile,dev-pytest]",
7474
]
7575
dev-pytest = [
76+
"frequenz-microgrid-component-graph[microgrid]",
77+
"frequenz-microgrid-component-graph[assets]",
7678
"pytest == 8.4.2",
7779
"pylint == 4.0.2", # We need this to check for the examples
7880
"frequenz-repo-config[extra-lint-examples] == 0.13.6",

python/frequenz/microgrid_component_graph/__init__.pyi

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""A graph representation of the electrical components in a microgrid."""
77

88
from collections import abc
9-
from typing import AbstractSet, Generic, Protocol, TypeVar
9+
from typing import Generic, Protocol, TypeVar
1010

1111
class InvalidGraphError(Exception):
1212
"""Exception type that will be thrown if graph data is not valid."""
@@ -60,14 +60,15 @@ class ComponentGraphConfig:
6060

6161
ComponentT = TypeVar("ComponentT", bound=ComponentProtocol)
6262
ConnectionT = TypeVar("ConnectionT", bound=ConnectionProtocol)
63+
ComponentIdT = TypeVar("ComponentIdT", bound=ComponentIdProtocol)
6364

64-
class ComponentGraph(Generic[ComponentT, ConnectionT]):
65+
class ComponentGraph(Generic[ComponentT, ConnectionT, ComponentIdT]):
6566
"""A graph representation of the electrical components in a microgrid."""
6667

6768
def __init__(
6869
self,
69-
components: AbstractSet[ComponentT],
70-
connections: AbstractSet[ConnectionT],
70+
components: abc.Iterable[ComponentT],
71+
connections: abc.Iterable[ConnectionT],
7172
config: ComponentGraphConfig = ComponentGraphConfig(),
7273
) -> None:
7374
"""Initialize this instance.
@@ -78,7 +79,7 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
7879
config: The configuration for the component graph.
7980
"""
8081

81-
def component(self, component_id: ComponentIdProtocol) -> ComponentT:
82+
def component(self, component_id: ComponentIdT) -> ComponentT:
8283
"""Fetch the component with the specified `component_id`.
8384
8485
Args:
@@ -93,23 +94,23 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
9394

9495
def components(
9596
self,
96-
filter_by_ids: set[ComponentIdProtocol] | None = None,
97-
filter_by_types: set[type[ComponentProtocol]] | None = None,
97+
matching_ids: abc.Iterable[ComponentIdT] | None = None,
98+
matching_types: abc.Iterable[type[ComponentT]] | None = None,
9899
) -> set[ComponentT]:
99100
"""Fetch all components in this graph.
100101
101102
Returns:
102103
A set of all components in this graph.
103104
"""
104105

105-
def connections(self) -> set[ConnectionT]:
106+
def connections(self) -> abc.Set[ConnectionT]:
106107
"""Fetch all connections in this graph.
107108
108109
Returns:
109110
A set of all connections in this graph.
110111
"""
111112

112-
def predecessors(self, component_id: ComponentIdProtocol) -> set[ComponentT]:
113+
def predecessors(self, component_id: ComponentIdT) -> abc.Set[ComponentT]:
113114
"""Fetch all predecessors of the specified component ID.
114115
115116
Args:
@@ -122,7 +123,7 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
122123
ValueError: if no component exists with the given ID.
123124
"""
124125

125-
def successors(self, component_id: ComponentIdProtocol) -> set[ComponentT]:
126+
def successors(self, component_id: ComponentIdT) -> abc.Set[ComponentT]:
126127
"""Fetch all successors of the specified component ID.
127128
128129
Args:
@@ -135,7 +136,7 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
135136
ValueError: if no component exists with the given ID.
136137
"""
137138

138-
def is_pv_meter(self, component_id: ComponentIdProtocol) -> bool:
139+
def is_pv_meter(self, component_id: ComponentIdT) -> bool:
139140
"""Check if the specified component is a PV meter.
140141
141142
Args:
@@ -148,7 +149,7 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
148149
ValueError: if no component exists with the given ID.
149150
"""
150151

151-
def is_battery_meter(self, component_id: ComponentIdProtocol) -> bool:
152+
def is_battery_meter(self, component_id: ComponentIdT) -> bool:
152153
"""Check if the specified component is a battery meter.
153154
154155
Args:
@@ -161,7 +162,7 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
161162
ValueError: if no component exists with the given ID.
162163
"""
163164

164-
def is_ev_charger_meter(self, component_id: ComponentIdProtocol) -> bool:
165+
def is_ev_charger_meter(self, component_id: ComponentIdT) -> bool:
165166
"""Check if the specified component is an EV charger meter.
166167
167168
Args:
@@ -174,7 +175,7 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
174175
ValueError: if no component exists with the given ID.
175176
"""
176177

177-
def is_chp_meter(self, component_id: ComponentIdProtocol) -> bool:
178+
def is_chp_meter(self, component_id: ComponentIdT) -> bool:
178179
"""Check if the specified component is a CHP meter.
179180
180181
Args:
@@ -208,30 +209,28 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
208209
The grid formula as a string.
209210
"""
210211

211-
def pv_formula(self, pv_inverter_ids: abc.Set[ComponentIdProtocol] | None) -> str:
212+
def pv_formula(self, pv_inverter_ids: abc.Set[ComponentIdT] | None) -> str:
212213
"""Generate the PV formula for this component graph.
213214
214215
Returns:
215216
The PV formula as a string.
216217
"""
217218

218-
def battery_formula(self, battery_ids: abc.Set[ComponentIdProtocol] | None) -> str:
219+
def battery_formula(self, battery_ids: abc.Set[ComponentIdT] | None) -> str:
219220
"""Generate the battery formula for this component graph.
220221
221222
Returns:
222223
The battery formula as a string.
223224
"""
224225

225-
def chp_formula(self, chp_ids: abc.Set[ComponentIdProtocol] | None) -> str:
226+
def chp_formula(self, chp_ids: abc.Set[ComponentIdT] | None) -> str:
226227
"""Generate the CHP formula for this component graph.
227228
228229
Returns:
229230
The CHP formula as a string.
230231
"""
231232

232-
def ev_charger_formula(
233-
self, ev_charger_ids: abc.Set[ComponentIdProtocol] | None
234-
) -> str:
233+
def ev_charger_formula(self, ev_charger_ids: abc.Set[ComponentIdT] | None) -> str:
235234
"""Generate the EV charger formula for this component graph.
236235
237236
Returns:
@@ -246,17 +245,15 @@ class ComponentGraph(Generic[ComponentT, ConnectionT]):
246245
"""
247246

248247
def battery_coalesce_formula(
249-
self, battery_ids: abc.Set[ComponentIdProtocol] | None
248+
self, battery_ids: abc.Set[ComponentIdT] | None
250249
) -> str:
251250
"""Generate the battery coalesce formula for this component graph.
252251
253252
Returns:
254253
The battery coalesced formula as a string.
255254
"""
256255

257-
def pv_coalesce_formula(
258-
self, pv_inverter_ids: abc.Set[ComponentIdProtocol] | None
259-
) -> str:
256+
def pv_coalesce_formula(self, pv_inverter_ids: abc.Set[ComponentIdT] | None) -> str:
260257
"""Generate the PV coalesce formula for this component graph.
261258
262259
Returns:

src/graph.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,16 @@ impl ComponentGraph {
106106
cls.into()
107107
}
108108

109-
#[pyo3(signature = (filter_by_ids=None, filter_by_types=None))]
109+
#[pyo3(signature = (matching_ids=None, matching_types=None))]
110110
fn components(
111111
&self,
112-
filter_by_ids: Option<Bound<'_, PyAny>>,
113-
filter_by_types: Option<Bound<'_, PyAny>>,
112+
matching_ids: Option<Bound<'_, PyAny>>,
113+
matching_types: Option<Bound<'_, PyAny>>,
114114
) -> PyResult<Py<PySet>> {
115115
let iter = self.graph.components();
116116

117117
Python::attach(|py| {
118-
let components: Vec<_> = if let Some(component_categories) = filter_by_types {
118+
let components: Vec<_> = if let Some(component_categories) = matching_types {
119119
let categories: Vec<cg::ComponentCategory> = component_categories
120120
.try_iter()?
121121
.map(|c| category_from_python_component(py, &c?))
@@ -127,7 +127,7 @@ impl ComponentGraph {
127127
iter.collect()
128128
};
129129

130-
let components: Vec<_> = if let Some(ids) = filter_by_ids {
130+
let components: Vec<_> = if let Some(ids) = matching_ids {
131131
let ids_set: BTreeSet<u64> = ids
132132
.try_iter()?
133133
.map(|id| extract_int::<u64>(py, id?))

tests/test_microgrid_component_graph.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from frequenz.client.common.microgrid import MicrogridId
77
from frequenz.client.common.microgrid.components import ComponentId
88
from frequenz.client.microgrid.component import (
9+
Component,
910
ComponentConnection,
1011
GridConnectionPoint,
1112
Meter,
@@ -16,10 +17,14 @@
1617

1718
def test_loading() -> None:
1819
"""Test that the microgrid_component_graph module loads correctly."""
19-
graph = microgrid_component_graph.ComponentGraph(
20+
graph: microgrid_component_graph.ComponentGraph[
21+
Component, ComponentConnection, ComponentId
22+
] = microgrid_component_graph.ComponentGraph(
2023
components={
2124
GridConnectionPoint(
22-
id=ComponentId(1), microgrid_id=MicrogridId(1), rated_fuse_current=100
25+
id=ComponentId(1),
26+
microgrid_id=MicrogridId(1),
27+
rated_fuse_current=100,
2328
),
2429
Meter(id=ComponentId(2), microgrid_id=MicrogridId(1)),
2530
},

0 commit comments

Comments
 (0)