Skip to content

Commit 92f5234

Browse files
committed
[ModelicaSystem] add _prepare_inputdata()
1 parent b89b9c7 commit 92f5234

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
@@ -985,6 +985,50 @@ def _strip_space(name):
985985

986986
raise ModelicaSystemError("Unhandled input for strip_space()")
987987

988+
def _prepare_inputdata(
989+
self,
990+
rawinput: str | list[str] | dict[str, str | int | float],
991+
) -> dict[str, str]:
992+
"""
993+
Convert raw input to a structured dictionary {'key1': 'value1', 'key2': 'value2'}.
994+
"""
995+
996+
def prepare_str(str_in: str) -> dict[str, str]:
997+
str_in = str_in.replace(" ", "")
998+
key_val_list: list[str] = str_in.split("=")
999+
if len(key_val_list) != 2:
1000+
raise ModelicaSystemError(f"Invalid 'key=value' pair: {str_in}")
1001+
1002+
inputdata = {key_val_list[0]: key_val_list[1]}
1003+
1004+
return inputdata
1005+
1006+
if isinstance(rawinput, str):
1007+
warnings.warn(message="The definition of values to set should use a dictionary, "
1008+
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1009+
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1010+
category=DeprecationWarning,
1011+
stacklevel=3)
1012+
return prepare_str(rawinput)
1013+
1014+
if isinstance(rawinput, list):
1015+
warnings.warn(message="The definition of values to set should use a dictionary, "
1016+
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1017+
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1018+
category=DeprecationWarning,
1019+
stacklevel=3)
1020+
1021+
inputdata: dict[str, str] = {}
1022+
for item in rawinput:
1023+
inputdata |= prepare_str(item)
1024+
1025+
return inputdata
1026+
1027+
if isinstance(rawinput, dict):
1028+
inputdata = {key: str(val) for key, val in rawinput.items()}
1029+
1030+
return inputdata
1031+
9881032
def setMethodHelper(
9891033
self,
9901034
inputdata: str | list[str] | dict[str, str | int | float],

0 commit comments

Comments
 (0)