|
13 | 13 | QUOTING_CHARS, SPECIAL_CHARS |
14 | 14 | from hsd.eventhandler import HsdEventHandler, HsdEventPrinter |
15 | 15 |
|
16 | | -_ItemType = Union[float, int, bool, str] |
| 16 | +_ItemType = Union[float, complex, int, bool, str] |
17 | 17 |
|
18 | 18 | _DataType = Union[_ItemType, List[_ItemType]] |
19 | 19 |
|
20 | 20 | # Pattern to transform HSD string values into actual Python data types |
21 | 21 | _TOKEN_PATTERN = re.compile(r""" |
22 | 22 | # Integer |
23 | | -(?:\s*(?:^|(?<=\s))(?P<int>[+-]?[0-9]+)(?:\s*$|\s+)) |
| 23 | +(?: |
| 24 | + \s*(?:^|(?<=\s)) |
| 25 | + (?P<int>[+-]?[0-9]+) |
| 26 | + (?:\s*$|\s+) |
| 27 | +) |
24 | 28 | | |
25 | 29 | # 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 | +) |
28 | 46 | | |
29 | 47 | # 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 | +) |
31 | 53 | | |
32 | 54 | # Quoted string |
33 | | -(?:\s*(?:(?P<qstr>(?P<quote>['"]).*?(?P=quote)) |
| 55 | +(?: |
| 56 | + \s* |
| 57 | + (?:(?P<qstr>(?P<quote>['"]).*?(?P=quote) |
| 58 | +) |
34 | 59 | | |
35 | 60 | # Unquoted string |
36 | 61 | (?P<str>.+?))(?:$|\s+)) |
@@ -148,6 +173,9 @@ def _text_to_data(self, txt: str) -> _DataType: |
148 | 173 | linedata.append(int(match.group("int"))) |
149 | 174 | elif match.group("float"): |
150 | 175 | 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)) |
151 | 179 | elif match.group("logical"): |
152 | 180 | lowlog = match.group("logical").lower() |
153 | 181 | linedata.append(lowlog == "yes") |
@@ -246,6 +274,8 @@ def _item_to_hsd(item): |
246 | 274 | return "Yes" if item else "No" |
247 | 275 | if isinstance(item, (int, float)): |
248 | 276 | return str(item) |
| 277 | + if isinstance(item, complex): |
| 278 | + return f"{item.real}{item.imag:+}i" |
249 | 279 | if isinstance(item, str): |
250 | 280 | return _str_to_hsd(item) |
251 | 281 | msg = "Data type {} can not be converted to HSD string"\ |
|
0 commit comments