66"""A graph representation of the electrical components in a microgrid."""
77
88from collections import abc
9- from typing import AbstractSet , Generic , Protocol , TypeVar
9+ from typing import Generic , Protocol , TypeVar
1010
1111class InvalidGraphError (Exception ):
1212 """Exception type that will be thrown if graph data is not valid."""
@@ -60,14 +60,15 @@ class ComponentGraphConfig:
6060
6161ComponentT = TypeVar ("ComponentT" , bound = ComponentProtocol )
6262ConnectionT = 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:
0 commit comments