Skip to content

Commit 5836909

Browse files
committed
add regex
Signed-off-by: Nitish Bharambe <[email protected]>
1 parent f64a963 commit 5836909

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

src/power_grid_model_io/utils/parsing.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import re
99
from typing import Dict
1010

11-
_TRAFO_CONNECTION_RE = re.compile(r"^(Y|YN|D|Z|ZN)(y|yn|d|z|zn)(\d|1[0-2])?$")
11+
_TRAFO_CONNECTION_RE = re.compile(r"^(Y|YN|D|Z|ZN)(y|yn|d|z|zn)(-?\d|1[0-2]|-1[0-2])?$")
1212

1313

1414
def parse_trafo_connection(string: str) -> Dict[str, str]:
@@ -17,11 +17,11 @@ def parse_trafo_connection(string: str) -> Dict[str, str]:
1717
Matches the following regular expression to the winding_from and winding_to codes.
1818
Optionally checks the clock number:
1919
20-
^ Start of the string
21-
(Y|YN|D|Z|ZN) From winding type
22-
(y|yn|d|z|zn) To winding type
23-
(\d|1[0-2])? Optional clock number (0-12)
24-
$ End of the string
20+
^ Start of the string
21+
(Y|YN|D|Z|ZN) From winding type
22+
(y|yn|d|z|zn) To winding type
23+
(-?\d|1[0-2]|-1[0-2])? Optional clock number (-12-12)
24+
$ End of the string
2525
2626
Args:
2727
string (str): The input string.
@@ -39,7 +39,9 @@ def parse_trafo_connection(string: str) -> Dict[str, str]:
3939
return {"winding_from": match.group(1), "winding_to": match.group(2), "clock": match.group(3)}
4040

4141

42-
_TRAFO3_CONNECTION_RE = re.compile(r"^(Y|YN|D|Z|ZN)(y|yn|d|z|zn)(\d|1[0-2])?(y|yn|d|z|zn)(\d|1[0-2])?$")
42+
_TRAFO3_CONNECTION_RE = re.compile(
43+
r"^(Y|YN|D|Z|ZN)(y|yn|d|z|zn)(-?\d|1[0-2]|-1[0-2])?(y|yn|d|z|zn)(-?\d|1[0-2]|-1[0-2])?$"
44+
)
4345

4446

4547
def parse_trafo3_connection(string: str) -> Dict[str, str]:
@@ -48,13 +50,13 @@ def parse_trafo3_connection(string: str) -> Dict[str, str]:
4850
Matches the following regular expression to the winding_1, winding_2 and winding_3 codes.
4951
Optionally checks the clock numbers:
5052
51-
^ Start of the string
52-
(Y|YN|D|Z|ZN) First winding type
53-
(y|yn|d|z|zn) Second winding type
54-
(\d|1[0-2]) Clock number (0-12)
55-
(y|yn|d|z|zn) Third winding type
56-
(\d|1[0-2]) Clock number (0-12)
57-
$ End of the string
53+
^ Start of the string
54+
(Y|YN|D|Z|ZN) First winding type
55+
(y|yn|d|z|zn) Second winding type
56+
(-?\d|1[0-2]|-1[0-2]) Clock number (-12-12)
57+
(y|yn|d|z|zn) Third winding type
58+
(-?\d|1[0-2]|-1[0-2]) Clock number (-12-12)
59+
$ End of the string
5860
5961
Args:
6062
string (str): The input string.

tests/unit/functions/test_phase_to_phase.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,16 @@ def test_get_winding_to__exception():
179179
[
180180
("YNd0", 0),
181181
("YNyn5", 5),
182-
("YNd12", 12),
182+
("YNd12", 0),
183+
("Dyn-1", 11),
184+
("Dd-4", 8),
183185
],
184186
)
185187
def test_get_clock(code: str, clock: int):
186188
assert get_clock(code) == clock
187189

188190

189-
@mark.parametrize("code", ["YNd-1", "YNd13"])
191+
@mark.parametrize("code", ["YNd-15", "YNd13"])
190192
def test_get_clock__exception(code):
191193
with raises(ValueError):
192194
get_clock(code)
@@ -344,7 +346,8 @@ def test_get_winding_3__exception():
344346
[
345347
("YNd0y4", 0),
346348
("YNyn5d3", 5),
347-
("YNd12d1", 12),
349+
("YNd12d1", 0),
350+
("YNd-5d1", 7),
348351
],
349352
)
350353
def test_get_clock_12(code: str, clock: int):
@@ -363,6 +366,7 @@ def test_get_clock_12__exception(code):
363366
("YNd0y4", 4),
364367
("YNyn5d3", 3),
365368
("YNd12d1", 1),
369+
("YNd12d-1", 11),
366370
],
367371
)
368372
def test_get_clock_13(code: str, clock: int):

tests/unit/utils/test_parsing.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ def test_parse_trafo_connection__neg():
3434
parse_trafo_connection("YNx")
3535
with pytest.raises(ValueError, match="Invalid transformer connection string: 'Dy13'"):
3636
parse_trafo_connection("Dy13")
37-
with pytest.raises(ValueError, match="Invalid transformer connection string: 'Dy-1'"):
38-
parse_trafo_connection("Dy-1")
3937

4038

4139
def test_parse_trafo3_connection__pos():
@@ -96,8 +94,6 @@ def test_parse_trafo3_connection__neg():
9694
parse_trafo3_connection("Dyd13")
9795
with pytest.raises(ValueError, match="Invalid three winding transformer connection string: 'DyD10'"):
9896
parse_trafo3_connection("DyD10")
99-
with pytest.raises(ValueError, match="Invalid three winding transformer connection string: 'Dynd-1'"):
100-
parse_trafo3_connection("Dynd-1")
10197
with pytest.raises(ValueError, match=re.escape("Invalid three winding transformer connection string: 'Dyn+5d-1'")):
10298
parse_trafo3_connection("Dyn+5d-1")
10399
with pytest.raises(ValueError, match="Invalid three winding transformer connection string: 'Dy1d13'"):

0 commit comments

Comments
 (0)