Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bag of small random fixes #339

Merged
merged 7 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/somersaultecu.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ class SomersaultSID(IntEnum):
value_raw="0", value_type=DataType.A_UINT32, interval_type=None),
short_label=None,
description=None,
internal_type=DataType.A_UINT32,
physical_type=DataType.A_UNICODE2STRING,
domain_type=DataType.A_UINT32,
range_type=DataType.A_UNICODE2STRING,
compu_inverse_value=None,
compu_rational_coeffs=None),
CompuScale(
Expand All @@ -455,8 +455,8 @@ class SomersaultSID(IntEnum):
value_raw="1", value_type=DataType.A_UINT32, interval_type=None),
short_label=None,
description=None,
internal_type=DataType.A_UINT32,
physical_type=DataType.A_UNICODE2STRING,
domain_type=DataType.A_UINT32,
range_type=DataType.A_UNICODE2STRING,
compu_inverse_value=None,
compu_rational_coeffs=None),
],
Expand Down
2 changes: 1 addition & 1 deletion odxtools/compumethods/compuinternaltophys.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def compu_internal_to_phys_from_et(et_element: ElementTree.Element,
physical_type: DataType) -> "CompuInternalToPhys":
compu_scales = [
CompuScale.compuscale_from_et(
cse, doc_frags, internal_type=internal_type, physical_type=physical_type)
cse, doc_frags, domain_type=internal_type, range_type=physical_type)
for cse in et_element.iterfind("COMPU-SCALES/COMPU-SCALE")
]

Expand Down
2 changes: 1 addition & 1 deletion odxtools/compumethods/compuphystointernal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def compu_phys_to_internal_from_et(et_element: ElementTree.Element,
physical_type: DataType) -> "CompuPhysToInternal":
compu_scales = [
CompuScale.compuscale_from_et(
cse, doc_frags, internal_type=internal_type, physical_type=physical_type)
cse, doc_frags, domain_type=physical_type, range_type=internal_type)
for cse in et_element.iterfind("COMPU-SCALES/COMPU-SCALE")
]

Expand Down
29 changes: 20 additions & 9 deletions odxtools/compumethods/compurationalcoeffs.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
# SPDX-License-Identifier: MIT
from dataclasses import dataclass
from typing import List
from typing import List, Union, cast
from xml.etree import ElementTree

from ..exceptions import odxrequire
from ..exceptions import odxassert, odxrequire
from ..odxlink import OdxDocFragment
from ..odxtypes import DataType


@dataclass
class CompuRationalCoeffs:
numerators: List[float]
denominators: List[float]
value_type: DataType

numerators: List[Union[int, float]]
denominators: List[Union[int, float]]

@staticmethod
def from_et(et_element: ElementTree.Element,
doc_frags: List[OdxDocFragment]) -> "CompuRationalCoeffs":
def coeffs_from_et(et_element: ElementTree.Element, doc_frags: List[OdxDocFragment], *,
value_type: DataType) -> "CompuRationalCoeffs":
odxassert(
value_type
in (DataType.A_UINT32, DataType.A_INT32, DataType.A_FLOAT32, DataType.A_FLOAT64),
"Rational coefficients must be of numeric type.")

numerators = [
float(odxrequire(elem.text)) for elem in et_element.iterfind("COMPU-NUMERATOR/V")
cast(float, value_type.from_string(odxrequire(elem.text)))
for elem in et_element.iterfind("COMPU-NUMERATOR/V")
]
denominators = [
float(odxrequire(elem.text)) for elem in et_element.iterfind("COMPU-DENOMINATOR/V")
cast(float, value_type.from_string(odxrequire(elem.text)))
for elem in et_element.iterfind("COMPU-DENOMINATOR/V")
]

return CompuRationalCoeffs(numerators=numerators, denominators=denominators)
return CompuRationalCoeffs(
value_type=value_type, numerators=numerators, denominators=denominators)
22 changes: 11 additions & 11 deletions odxtools/compumethods/compuscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,32 @@ class CompuScale:
# the following two attributes are not specified for COMPU-SCALE
# tags in the XML, but they are required to do anything useful
# with it.
internal_type: DataType
physical_type: DataType
domain_type: DataType
range_type: DataType
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document the properties, or their meaning gets lost in the commit message

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than this, LGTM

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. fixed, thanks: 1f39cf9


@staticmethod
def compuscale_from_et(et_element: ElementTree.Element, doc_frags: List[OdxDocFragment], *,
internal_type: DataType, physical_type: DataType) -> "CompuScale":
domain_type: DataType, range_type: DataType) -> "CompuScale":
short_label = et_element.findtext("SHORT-LABEL")
description = Description.from_et(et_element.find("DESC"), doc_frags)

lower_limit = Limit.limit_from_et(
et_element.find("LOWER-LIMIT"), doc_frags, value_type=internal_type)
et_element.find("LOWER-LIMIT"), doc_frags, value_type=domain_type)
upper_limit = Limit.limit_from_et(
et_element.find("UPPER-LIMIT"), doc_frags, value_type=internal_type)
et_element.find("UPPER-LIMIT"), doc_frags, value_type=domain_type)

compu_inverse_value = None
if (cive := et_element.find("COMPU-INVERSE-VALUE")) is not None:
compu_inverse_value = CompuInverseValue.compuvalue_from_et(
cive, data_type=internal_type)
compu_inverse_value = CompuInverseValue.compuvalue_from_et(cive, data_type=domain_type)

compu_const = None
if (cce := et_element.find("COMPU-CONST")) is not None:
compu_const = CompuConst.compuvalue_from_et(cce, data_type=physical_type)
compu_const = CompuConst.compuvalue_from_et(cce, data_type=range_type)

compu_rational_coeffs: Optional[CompuRationalCoeffs] = None
if (crc_elem := et_element.find("COMPU-RATIONAL-COEFFS")) is not None:
compu_rational_coeffs = CompuRationalCoeffs.from_et(crc_elem, doc_frags)
compu_rational_coeffs = CompuRationalCoeffs.coeffs_from_et(
crc_elem, doc_frags, value_type=range_type)

return CompuScale(
short_label=short_label,
Expand All @@ -63,8 +63,8 @@ def compuscale_from_et(et_element: ElementTree.Element, doc_frags: List[OdxDocFr
compu_inverse_value=compu_inverse_value,
compu_const=compu_const,
compu_rational_coeffs=compu_rational_coeffs,
internal_type=internal_type,
physical_type=physical_type)
domain_type=domain_type,
range_type=range_type)

def applies(self, internal_value: AtomicOdxType) -> bool:

Expand Down
11 changes: 5 additions & 6 deletions odxtools/compumethods/linearsegment.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ def __compute_physical_limits(self) -> None:
This method is called by `__post_init__()`.
"""

def convert_internal_to_physical_limit(internal_limit: Optional[Limit],
is_upper_limit: bool) -> Optional[Limit]:
def convert_internal_to_physical_limit(internal_limit: Optional[Limit]) -> Optional[Limit]:
"""Helper method to convert a single internal limit
"""
if internal_limit is None or internal_limit.value_raw is None:
Expand All @@ -137,16 +136,16 @@ def convert_internal_to_physical_limit(internal_limit: Optional[Limit],

if self.factor >= 0:
self._physical_lower_limit = convert_internal_to_physical_limit(
self.internal_lower_limit, False)
self.internal_lower_limit)
self._physical_upper_limit = convert_internal_to_physical_limit(
self.internal_upper_limit, True)
self.internal_upper_limit)
else:
# If the scaling factor is negative, the lower and upper
# limit are swapped
self._physical_lower_limit = convert_internal_to_physical_limit(
self.internal_upper_limit, True)
self.internal_upper_limit)
self._physical_upper_limit = convert_internal_to_physical_limit(
self.internal_lower_limit, False)
self.internal_lower_limit)

def physical_applies(self, physical_value: AtomicOdxType) -> bool:
"""Returns True iff the segment is applicable to a given physical value"""
Expand Down
3 changes: 1 addition & 2 deletions odxtools/nameditemlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ def __getitem__(self, key: str) -> T:
def __getitem__(self, key: slice) -> List[T]:
...

def __getitem__( # type: ignore
self, key: Union[SupportsIndex, str, slice]) -> Union[T, List[T]]:
def __getitem__(self, key: Union[SupportsIndex, str, slice]) -> Union[T, List[T]]:
if isinstance(key, (SupportsIndex, slice)):
return super().__getitem__(key)
else:
Expand Down
52 changes: 30 additions & 22 deletions tests/test_compu_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ def _get_jinja_environment() -> jinja2.environment.Environment:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[0, 1],
denominators=[3600],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down Expand Up @@ -145,11 +146,12 @@ def test_linear_compu_method_type_denom_not_one(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[0, 1],
denominators=[3600],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand All @@ -173,11 +175,12 @@ def test_linear_compu_method_type_int_int(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[1, 3],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down Expand Up @@ -206,11 +209,12 @@ def test_linear_compu_method_type_int_float(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[1, 3],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_FLOAT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_FLOAT32),
],
prog_code=None,
compu_default_value=None),
Expand Down Expand Up @@ -239,11 +243,12 @@ def test_linear_compu_method_type_float_int(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[1, 3],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down Expand Up @@ -272,11 +277,12 @@ def test_linear_compu_method_type_string(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[1, 3],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand All @@ -299,11 +305,12 @@ def test_linear_compu_method_limits(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[1, 5],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down Expand Up @@ -347,11 +354,12 @@ def test_linear_compu_method_physical_limits(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[1, -5],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down Expand Up @@ -434,8 +442,8 @@ def _get_jinja_environment() -> jinja2.environment.Environment:
compu_inverse_value=None,
compu_const=CompuConst(v="-1", vt=None, data_type=DataType.A_INT32),
compu_rational_coeffs=None,
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
CompuScale(
short_label=None,
description=None,
Expand All @@ -445,8 +453,8 @@ def _get_jinja_environment() -> jinja2.environment.Environment:
compu_inverse_value=None,
compu_const=CompuConst(v="1", vt=None, data_type=DataType.A_INT32),
compu_rational_coeffs=None,
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
CompuScale(
short_label=None,
description=None,
Expand All @@ -456,8 +464,8 @@ def _get_jinja_environment() -> jinja2.environment.Environment:
compu_inverse_value=None,
compu_const=CompuConst(v="2", vt=None, data_type=DataType.A_INT32),
compu_rational_coeffs=None,
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down
10 changes: 6 additions & 4 deletions tests/test_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -1992,11 +1992,12 @@ def test_decode_request_linear_compu_method(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[1, 5],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down Expand Up @@ -2631,11 +2632,12 @@ def test_physical_constant_parameter(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[offset, 1],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down
5 changes: 3 additions & 2 deletions tests/test_diag_coded_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,12 @@ def test_end_to_end(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[0, 8],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down
5 changes: 3 additions & 2 deletions tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ def test_encode_linear(self) -> None:
compu_inverse_value=None,
compu_const=None,
compu_rational_coeffs=CompuRationalCoeffs(
value_type=DataType.A_INT32,
numerators=[8, 2],
denominators=[1],
),
internal_type=DataType.A_INT32,
physical_type=DataType.A_INT32),
domain_type=DataType.A_INT32,
range_type=DataType.A_INT32),
],
prog_code=None,
compu_default_value=None),
Expand Down
Loading