Skip to content

Commit

Permalink
DataType: provide custom __str__()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Gerrit Ecke <[email protected]>
  • Loading branch information
andlaus committed Sep 25, 2024
1 parent 6503e1c commit 2344851
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
15 changes: 15 additions & 0 deletions odxtools/odxtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<unknown type>"
38 changes: 7 additions & 31 deletions odxtools/parameterinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "<unknown type>"


def _get_linear_segment_info(segment: LinearSegment) -> str:
ll = segment.physical_lower_limit
ul = segment.physical_upper_limit
Expand Down Expand Up @@ -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)} }}")

Expand All @@ -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
Expand All @@ -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("<NOT ENCODABLE>")
else:
Expand All @@ -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("<NOT ENCODABLE>")
else:
Expand All @@ -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"; <programmatic translation>")

of.write("\n")
Expand Down

0 comments on commit 2344851

Please sign in to comment.