Skip to content

Commit e733173

Browse files
committed
add prefixes to avoid hard coding, error naming changes, fixed bug in add_pvsystem
1 parent 2a904aa commit e733173

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

pygridsim/core.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pygridsim.lines import _make_line
55
from pygridsim.parameters import _make_generator, _make_load_node, _make_pv, _make_source_node
66
from pygridsim.results import _export_results, _query_solution
7+
from pygridsim.defaults import RESERVED_PREFIXES
78

89
"""Main module."""
910

@@ -21,6 +22,7 @@ def __init__(self):
2122
num_transformers (int): Number of transformers in circuit so far.
2223
num_pv (int): Number of PV systems in circuit so far.
2324
num_generators (int): Number generators in circuit so far.
25+
nickname_to_name (dict[str, str]): Map containing nicknames to their internal names.
2426
"""
2527
self.num_loads = 0
2628
self.num_lines = 0
@@ -33,10 +35,9 @@ def __init__(self):
3335

3436
def _check_naming(self, name):
3537
if name in self.nickname_to_name:
36-
raise NameError("Provided name already assigned to a node")
37-
if name.startswith("load") or name.startswith(
38-
"generator") or name == "source" or name.startswith("pv"):
39-
raise NameError(
38+
raise ValueError("Provided name already assigned to a node")
39+
if any(name.startswith(prefix) for prefix in RESERVED_PREFIXES):
40+
raise ValueError(
4041
"Cannot name nodes of the format 'component + __', ambiguity with internal names")
4142

4243
def add_load_nodes(self,
@@ -127,7 +128,7 @@ def add_PVSystems(self, load_nodes: list[str],
127128
PV_nodes = []
128129
for load in load_nodes:
129130
if (load in self.nickname_to_name):
130-
self.nickname_to_name[load]
131+
load = self.nickname_to_name[load]
131132

132133
PV_nodes.append(_make_pv(load, params, num_panels, self.num_pv))
133134
self.num_pv += 1

pygridsim/defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@
7575
VALID_LINE_TRANSFORMER_PARAMS = ["length", "XHL", "Conns"]
7676
VALID_PV_PARAMS = ["kV", "phases"]
7777
VALID_GENERATOR_PARAMS = ["kV", "kW", "phases"]
78+
79+
RESERVED_PREFIXES = ["load", "generator", "pv", "source"]

tests/test_circuit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ def test_012_node_naming(self):
174174
# Can use original or abbreviated name
175175
circuit.add_lines(connections=[("G0", "0"), ("generator1", "load1")])
176176

177-
with self.assertRaises(NameError):
177+
with self.assertRaises(ValueError):
178178
# Tries to assign an already assigned name
179179
circuit.add_load_nodes(num=1, names=["G1"])
180180

181-
with self.assertRaises(NameError):
181+
with self.assertRaises(ValueError):
182182
# Tries to assign name to internal name load1, errors because adds ambiguity
183183
circuit.add_load_nodes(num=1, names=["load1"])
184184

0 commit comments

Comments
 (0)