Skip to content

Commit 82ec35e

Browse files
committed
Merge branch 'feat/add-from-txt' into feature/downstream_node_performance-V2
2 parents b5713b0 + 0018e2f commit 82ec35e

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0
1+
1.1

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"""Create a grid from text a text file"""
66

77
import logging
8-
import re
98
from typing import TYPE_CHECKING
109

1110
from power_grid_model_ds._core.model.enums.nodes import NodeType
@@ -49,19 +48,17 @@ def load_from_txt(self, *args: str) -> "Grid":
4948
@staticmethod
5049
def read_txt(txt_lines: list[str]) -> tuple[set, dict]:
5150
"""Extract assets from text"""
51+
5252
txt_nodes = set()
5353
txt_branches = {}
5454
for text_line in txt_lines:
5555
if not text_line.strip() or text_line.startswith("#"):
5656
continue # skip empty lines and comments
57-
58-
pattern = re.compile(r"^\s*(\S+)\s+(\S+)(?:\s+(\S+))?\s*$")
59-
match = pattern.match(text_line)
60-
if not match:
61-
raise ValueError(f"Text line '{text_line}' is invalid. Skipping...")
62-
63-
from_node_str, to_node_str, comment = match.groups()
64-
comments = comment.split(",") if comment else []
57+
try:
58+
from_node_str, to_node_str, *comments = text_line.strip().split()
59+
except ValueError as err:
60+
raise ValueError(f"Text line '{text_line}' is invalid. Skipping...") from err
61+
comments = comments[0].split(",") if comments else []
6562

6663
txt_nodes |= {from_node_str, to_node_str}
6764
txt_branches[(from_node_str, to_node_str)] = comments

tests/unit/model/grids/test_grid_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def test_from_txt_with_conflicting_ids(self):
327327

328328
def test_from_txt_with_invalid_line(self):
329329
with pytest.raises(ValueError):
330-
Grid.from_txt("S1 2 arg3 arg4")
330+
Grid.from_txt("S1")
331331

332332
def test_from_txt_with_unordered_node_ids(self):
333333
grid = Grid.from_txt("S1 2", "S1 10", "10 11", "2 5", "5 6", "3 4", "3 7")

0 commit comments

Comments
 (0)