Skip to content

Commit c21c58f

Browse files
committed
[ModelicaSystem] add _prepare_inputdata()
1 parent cda6d1c commit c21c58f

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,50 @@ def _strip_space(name):
10011001

10021002
raise ModelicaSystemError("Unhandled input for strip_space()")
10031003

1004+
def _prepare_inputdata(
1005+
self,
1006+
rawinput: str | list[str] | dict[str, str | int | float],
1007+
) -> dict[str, str]:
1008+
"""
1009+
Convert raw input to a structured dictionary {'key1': 'value1', 'key2': 'value2'}.
1010+
"""
1011+
1012+
def prepare_str(str_in: str) -> dict[str, str]:
1013+
str_in = str_in.replace(" ", "")
1014+
key_val_list: list[str] = str_in.split("=")
1015+
if len(key_val_list) != 2:
1016+
raise ModelicaSystemError(f"Invalid 'key=value' pair: {str_in}")
1017+
1018+
inputdata = {key_val_list[0]: key_val_list[1]}
1019+
1020+
return inputdata
1021+
1022+
if isinstance(rawinput, str):
1023+
warnings.warn(message="The definition of values to set should use a dictionary, "
1024+
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1025+
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1026+
category=DeprecationWarning,
1027+
stacklevel=3)
1028+
return prepare_str(rawinput)
1029+
1030+
if isinstance(rawinput, list):
1031+
warnings.warn(message="The definition of values to set should use a dictionary, "
1032+
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1033+
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1034+
category=DeprecationWarning,
1035+
stacklevel=3)
1036+
1037+
inputdata: dict[str, str] = {}
1038+
for item in rawinput:
1039+
inputdata |= prepare_str(item)
1040+
1041+
return inputdata
1042+
1043+
if isinstance(rawinput, dict):
1044+
inputdata = {key: str(val) for key, val in rawinput.items()}
1045+
1046+
return inputdata
1047+
10041048
def setMethodHelper(
10051049
self,
10061050
inputdata: str | list[str] | dict[str, str | int | float],

0 commit comments

Comments
 (0)