From 2344851a9b7f44da71c5c629fedc81126001adb7 Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Wed, 25 Sep 2024 15:13:11 +0200 Subject: [PATCH] `DataType`: provide custom `__str__()` this method returns what the `DataType` object would be on the python side. as [at]kayoub5 points out, this is probably what is wanted 90% of the time such an object is printed. Also, this can now be directly used by `parameter_info()`. thanks to [at]kayoub5 for the insistence. Signed-off-by: Andreas Lauser Signed-off-by: Gerrit Ecke --- odxtools/odxtypes.py | 15 +++++++++++++++ odxtools/parameterinfo.py | 38 +++++++------------------------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/odxtools/odxtypes.py b/odxtools/odxtypes.py index cfcf694c..c5a18f70 100644 --- a/odxtools/odxtypes.py +++ b/odxtools/odxtypes.py @@ -241,3 +241,18 @@ def isinstance(self, value: Any) -> bool: return True else: return False + + def __str__(self) -> str: + if self == DataType.A_INT32: + return "int" + elif self == DataType.A_UINT32: + return "uint" + elif self in (DataType.A_FLOAT32, DataType.A_FLOAT64): + return "float" + elif self == DataType.A_BYTEFIELD: + return "bytefield" + elif self in (DataType.A_UNICODE2STRING, DataType.A_ASCIISTRING, DataType.A_UTF8STRING): + return "string" + else: + odxraise(f"Type info for type {self.value}", NotImplementedError) + return "" diff --git a/odxtools/parameterinfo.py b/odxtools/parameterinfo.py index fadc156c..dcc7692f 100644 --- a/odxtools/parameterinfo.py +++ b/odxtools/parameterinfo.py @@ -17,9 +17,8 @@ from .dtcdop import DtcDop from .dynamiclengthfield import DynamicLengthField from .endofpdufield import EndOfPduField -from .exceptions import odxraise, odxrequire +from .exceptions import odxrequire from .multiplexer import Multiplexer -from .odxtypes import DataType from .parameters.codedconstparameter import CodedConstParameter from .parameters.matchingrequestparameter import MatchingRequestParameter from .parameters.nrcconstparameter import NrcConstParameter @@ -33,22 +32,6 @@ from .staticfield import StaticField -def _get_type_info(odx_type: DataType) -> str: - if odx_type == DataType.A_INT32: - return "int" - elif odx_type == DataType.A_UINT32: - return "uint" - elif odx_type in (DataType.A_FLOAT32, DataType.A_FLOAT64): - return "float" - elif odx_type == DataType.A_BYTEFIELD: - return "bytefield" - elif odx_type in (DataType.A_UNICODE2STRING, DataType.A_ASCIISTRING, DataType.A_UTF8STRING): - return "string" - else: - odxraise(f"Type info for type {odx_type.value}", NotImplementedError) - return "" - - def _get_linear_segment_info(segment: LinearSegment) -> str: ll = segment.physical_lower_limit ul = segment.physical_upper_limit @@ -203,13 +186,10 @@ def parameter_info(param_list: Iterable[Parameter], quoted_names: bool = False) of.write(f" {v}\n") elif isinstance(cm, IdenticalCompuMethod): - of.write( - f"{q}{param.short_name}{q}: {_get_type_info(dop.physical_type.base_data_type)}\n" - ) + of.write(f"{q}{param.short_name}{q}: {dop.physical_type.base_data_type}\n") elif isinstance(cm, ScaleLinearCompuMethod): - of.write( - f"{q}{param.short_name}{q}: {_get_type_info(dop.physical_type.base_data_type)}") + of.write(f"{q}{param.short_name}{q}: {dop.physical_type.base_data_type}") seg_list = [_get_linear_segment_info(x) for x in cm.segments] of.write(f"; ranges = {{ {', '.join(seg_list)} }}") @@ -221,8 +201,7 @@ def parameter_info(param_list: Iterable[Parameter], quoted_names: bool = False) of.write("\n") elif isinstance(cm, LinearCompuMethod): - of.write( - f"{q}{param.short_name}{q}: {_get_type_info(dop.physical_type.base_data_type)}") + of.write(f"{q}{param.short_name}{q}: {dop.physical_type.base_data_type}") of.write(f"; range: {_get_linear_segment_info(cm.segment)}") unit = dop.unit @@ -233,8 +212,7 @@ def parameter_info(param_list: Iterable[Parameter], quoted_names: bool = False) of.write("\n") elif isinstance(cm, ScaleRatFuncCompuMethod): - of.write( - f"{q}{param.short_name}{q}: {_get_type_info(dop.physical_type.base_data_type)}") + of.write(f"{q}{param.short_name}{q}: {dop.physical_type.base_data_type}") if cm._phys_to_int_segments is None: of.write("") else: @@ -249,8 +227,7 @@ def parameter_info(param_list: Iterable[Parameter], quoted_names: bool = False) of.write("\n") elif isinstance(cm, RatFuncCompuMethod): - of.write( - f"{q}{param.short_name}{q}: {_get_type_info(dop.physical_type.base_data_type)}") + of.write(f"{q}{param.short_name}{q}: {dop.physical_type.base_data_type}") if cm._phys_to_int_segment is None: of.write("") else: @@ -264,8 +241,7 @@ def parameter_info(param_list: Iterable[Parameter], quoted_names: bool = False) of.write("\n") elif isinstance(cm, CompuCodeCompuMethod): - of.write( - f"{q}{param.short_name}{q}: {_get_type_info(dop.physical_type.base_data_type)}") + of.write(f"{q}{param.short_name}{q}: {dop.physical_type.base_data_type}") of.write(f"; ") of.write("\n")