Skip to content

Commit ba42210

Browse files
committed
[ModelicaSystemCmd] improve arg_set()
* fix override values (string/bool/numbers)
1 parent abcae09 commit ba42210

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,37 @@ def arg_set(self, key: str, val: Optional[str | dict] = None) -> None:
131131
key : str
132132
val : str, None
133133
"""
134+
135+
def override2str(value: Any) -> str:
136+
"""
137+
Convert a value for 'override' to a string taking into account differences between Modelica and Python.
138+
"""
139+
if isinstance(value, str):
140+
return f"\"{value.strip()}\""
141+
if isinstance(value, bool):
142+
return 'true' if value else 'false'
143+
if isinstance(value, numbers.Number):
144+
return str(value)
145+
raise ModelicaSystemError(f"Invalid value type: {type(value)} for key {key}")
146+
134147
if not isinstance(key, str):
135148
raise ModelicaSystemError(f"Invalid argument key: {repr(key)} (type: {type(key)})")
136149
key = key.strip()
137-
if val is None:
138-
argval = None
139-
elif isinstance(val, str):
140-
argval = val.strip()
141-
elif isinstance(val, numbers.Number):
142-
argval = str(val)
143-
elif key == 'override' and isinstance(val, dict):
150+
151+
if key == 'override' and isinstance(val, dict):
144152
for okey in val:
145153
if not isinstance(okey, str) or not isinstance(val[okey], (str, numbers.Number)):
146154
raise ModelicaSystemError("Invalid argument for 'override': "
147155
f"{repr(okey)} = {repr(val[okey])}")
148156
self._arg_override[okey] = val[okey]
149157

150-
argval = ','.join([f"{okey}={str(self._arg_override[okey])}" for okey in self._arg_override])
158+
argval = ','.join([f"{okey}={override2str(self._arg_override[okey])}" for okey in self._arg_override])
159+
elif val is None:
160+
argval = None
161+
elif isinstance(val, str):
162+
argval = val.strip()
163+
elif isinstance(val, numbers.Number):
164+
argval = str(val)
151165
else:
152166
raise ModelicaSystemError(f"Invalid argument value for {repr(key)}: {repr(val)} (type: {type(val)})")
153167

0 commit comments

Comments
 (0)