Skip to content

Commit 9ddc517

Browse files
committed
fixed from load types to general get_types function
1 parent 3b4b788 commit 9ddc517

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

pygridsim/configs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@
6666
"kW": defaults.INDUSTRIAL_GEN_KW,
6767
}
6868
}
69+
70+
NAME_TO_CONFIG = {
71+
"load": LOAD_CONFIGURATIONS,
72+
"source": SOURCE_CONFIGURATIONS,
73+
"generator": GENERATOR_CONFIGURATIONS,
74+
"line": LINE_CONFIGURATIONS
75+
}

pygridsim/core.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from altdss import altdss
33

4-
from pygridsim.configs import LOAD_CONFIGURATIONS
4+
from pygridsim.configs import NAME_TO_CONFIG
55
from pygridsim.enums import LoadType
66
from pygridsim.lines import _make_line
77
from pygridsim.parameters import _make_generator, _make_load_node, _make_pv, _make_source_node
@@ -207,10 +207,12 @@ def clear(self):
207207
for key in self.counts:
208208
self.counts[key] = 0
209209

210-
def get_load_types(self, show_ranges: bool = False):
210+
def get_types(self, component: str, show_ranges: bool = False):
211211
"""Provides list of all supported Load Types
212212
213213
Args:
214+
component (str):
215+
Which component to get, one of (one of "load", "source", "pv", "line")
214216
show_ranges (bool, optional):
215217
Whether to show all configuration ranges in output.
216218
@@ -219,12 +221,21 @@ def get_load_types(self, show_ranges: bool = False):
219221
A list containing all load types, if show_ranges False.
220222
A list of tuples showing all load types and configurations, if show_ranges True.
221223
"""
224+
component_simplified = component.lower().replace(" ", "")
225+
if (component_simplified[-1] == "s"):
226+
component_simplified = component_simplified[:-1]
227+
configuration = {}
228+
if component_simplified in NAME_TO_CONFIG:
229+
configuration = NAME_TO_CONFIG[component_simplified]
230+
else:
231+
raise KeyError(f"Invalid component input: expect one of {[name for name in NAME_TO_CONFIG]}")
232+
222233
if not show_ranges:
223-
return [load_type.value for load_type in LoadType]
234+
return [component_type.value for component_type in configuration]
224235

225-
load_types = []
226-
for load_type in LoadType:
227-
config_dict = LOAD_CONFIGURATIONS[load_type]
228-
load_types.append((load_type.value, config_dict))
236+
component_types = []
237+
for component_type in configuration:
238+
config_dict = configuration[component_type]
239+
component_types.append((component_type.value, config_dict))
229240

230-
return load_types
241+
return component_types

tests/test_circuit.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,12 @@ def tearDown(self):
256256

257257
def test_200_type_queries(self):
258258
circuit = PyGridSim()
259-
print(circuit.get_load_types())
260-
print(circuit.get_load_types(show_ranges=True))
259+
# should still work if plural, capitalized, spaces
260+
for component in ["load ", "sources", "Line", "GENERATOR"]:
261+
print(circuit.get_types(component))
262+
print(circuit.get_types(component, show_ranges=True))
263+
264+
def test_200_invalid_type_quer(self):
265+
circuit = PyGridSim()
266+
with self.assertRaises(KeyError):
267+
circuit.get_types("bad_component_name")

0 commit comments

Comments
 (0)