Skip to content

Commit 0d97116

Browse files
committed
Add support for complex entries
1 parent 2176653 commit 0d97116

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/hsd/dict.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,49 @@
1313
QUOTING_CHARS, SPECIAL_CHARS
1414
from hsd.eventhandler import HsdEventHandler, HsdEventPrinter
1515

16-
_ItemType = Union[float, int, bool, str]
16+
_ItemType = Union[float, complex, int, bool, str]
1717

1818
_DataType = Union[_ItemType, List[_ItemType]]
1919

2020
# Pattern to transform HSD string values into actual Python data types
2121
_TOKEN_PATTERN = re.compile(r"""
2222
# Integer
23-
(?:\s*(?:^|(?<=\s))(?P<int>[+-]?[0-9]+)(?:\s*$|\s+))
23+
(?:
24+
\s*(?:^|(?<=\s))
25+
(?P<int>[+-]?[0-9]+)
26+
(?:\s*$|\s+)
27+
)
2428
|
2529
# Floating point
26-
(?:\s*(?:^|(?<=\s))
27-
(?P<float>[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)(?:$|(?=\s+)))
30+
(?:
31+
\s*(?:^|(?<=\s))
32+
(?P<float>[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)
33+
(?:$|(?=\s+))
34+
)
35+
|
36+
# Complex value
37+
(?:
38+
\s*(?:^|(?<=\s))
39+
(?P<complex>
40+
[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?i
41+
|
42+
[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?[-+][0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?i
43+
)
44+
(?:$|(?=\s+))
45+
)
2846
|
2947
# Logical (Yes/No)
30-
(?:\s*(?:^|(?<=\s))(?P<logical>[Yy][Ee][Ss]|[Nn][Oo])(?:$|(?=\s+)))
48+
(?:
49+
\s*(?:^|(?<=\s))
50+
(?P<logical>[Yy][Ee][Ss]|[Nn][Oo])
51+
(?:$|(?=\s+))
52+
)
3153
|
3254
# Quoted string
33-
(?:\s*(?:(?P<qstr>(?P<quote>['"]).*?(?P=quote))
55+
(?:
56+
\s*
57+
(?:(?P<qstr>(?P<quote>['"]).*?(?P=quote)
58+
)
3459
|
3560
# Unquoted string
3661
(?P<str>.+?))(?:$|\s+))
@@ -148,6 +173,9 @@ def _text_to_data(self, txt: str) -> _DataType:
148173
linedata.append(int(match.group("int")))
149174
elif match.group("float"):
150175
linedata.append(float(match.group("float")))
176+
elif match.group("complex"):
177+
pycmplx = match.group("complex").replace("i", "j", 1)
178+
linedata.append(complex(pycmplx))
151179
elif match.group("logical"):
152180
lowlog = match.group("logical").lower()
153181
linedata.append(lowlog == "yes")
@@ -246,6 +274,8 @@ def _item_to_hsd(item):
246274
return "Yes" if item else "No"
247275
if isinstance(item, (int, float)):
248276
return str(item)
277+
if isinstance(item, complex):
278+
return f"{item.real}{item.imag:+}i"
249279
if isinstance(item, str):
250280
return _str_to_hsd(item)
251281
msg = "Data type {} can not be converted to HSD string"\

test/test_dict.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@
7878
}
7979
)
8080
),
81+
(
82+
"Complex entries", (
83+
"ComplexNums = 4.0+9.0i 2.1e-12-4.5e-12i 0.32+0.45i -0.01-1.0i\n",
84+
{
85+
"ComplexNums.hsdattrib": {_HSD_EQUAL: True, _HSD_LINE: 0},
86+
"ComplexNums": [4.0+9.0j, 2.1e-12-4.5e-12j, 3.2e-1+4.5e-1j, -0.01-1.0j]
87+
}
88+
)
89+
),
8190
(
8291
"Duplicate node", (
8392
"a {\n b = 1\n}\na {\n b = 2\n}\n",

0 commit comments

Comments
 (0)