Skip to content

Commit 9ecbb40

Browse files
committed
add support for both list[str] and str
Signed-off-by: Thijs Baaijen <[email protected]>
1 parent a7bdd9d commit 9ecbb40

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/power_grid_model_ds/_core/model/grids/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,17 @@ def _from_pickle(cls, pickle_path: Path):
409409
return grid
410410

411411
@classmethod
412-
def from_txt(cls, txt_lines: list[str]):
412+
def from_txt(cls, txt_lines: list[str] | str):
413413
"""Build a grid from a list of strings
414414
415415
See the documentation for the expected format of the txt_lines
416416
417-
Example:
417+
Args:
418+
txt_lines (list[str] | str): The lines of the txt
419+
420+
Examples:
418421
>>> Grid.from_txt(["1 2", "2 3", "3 4 transformer", "4 5", "S1 6"])
422+
alternative: Grid.from_txt("1 2\n2 3\n3 4 transformer\n4 5\nS1 6")
419423
"""
420424
return TextSource(grid_class=cls).load_from_txt(txt_lines)
421425

tests/unit/model/grids/test_grid_base.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ def test_from_txt_lines(self):
293293
assert 1 == grid.transformer.size
294294
np.testing.assert_array_equal([14, 10, 11, 12, 13, 15, 16, 17], grid.branches.id)
295295

296+
def test_from_txt_string(self):
297+
txt_string = "S1 2\nS1 3 open\n2 7\n3 5\n3 6 transformer\n5 7\n7 8\n8 9"
298+
assert Grid.from_txt(txt_string)
299+
300+
def test_from_txt_string_with_spaces(self):
301+
txt_string = "S1 2 \nS1 3 open\n2 7\n3 5\n 3 6 transformer\n5 7\n7 8\n8 9"
302+
assert Grid.from_txt(txt_string)
303+
296304
def test_from_txt_with_branch_ids(self):
297305
txt_lines = [
298306
"S1 2 91",
@@ -312,25 +320,15 @@ def test_from_txt_with_branch_ids(self):
312320
np.testing.assert_array_equal([95, 91, 92, 93, 94, 96, 97, 98], grid.branches.id)
313321

314322
def test_from_txt_with_conflicting_ids(self):
315-
txt_lines = [
316-
"S1 2",
317-
"1 3",
318-
]
323+
with pytest.raises(ValueError):
324+
Grid.from_txt(["S1 2", "1 3"])
319325

326+
def test_from_txt_with_invalid_line(self):
320327
with pytest.raises(ValueError):
321-
Grid.from_txt(txt_lines)
328+
Grid.from_txt(["S1 2 arg3 arg4"])
322329

323330
def test_from_txt_with_unordered_node_ids(self):
324-
txt_lines = [
325-
"S1 2",
326-
"S1 10",
327-
"10 11",
328-
"2 5",
329-
"5 6",
330-
"3 4",
331-
"3 7",
332-
]
333-
grid = Grid.from_txt(txt_lines)
331+
grid = Grid.from_txt(["S1 2", "S1 10", "10 11", "2 5", "5 6", "3 4", "3 7"])
334332
assert 9 == grid.node.size
335333

336334
def test_from_txt_with_unordered_branch_ids(self):

0 commit comments

Comments
 (0)