From 3a5ab4b6a428fcd25285c6cea4799f2ea0e57f7a Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Wed, 25 Sep 2024 12:19:36 +0200 Subject: [PATCH 1/2] `UnitSpec` make the list attributes unconditionally `NamedItemList` specifying this as either an ordinary list or a named item list is probably an artifact from the early days of odxtools where things were not quite as fleshed out as they are today. Anyway, it is much more convenient to be able to also access the items of these lists by name... Signed-off-by: Andreas Lauser Signed-off-by: Christian Hackenbeck --- examples/somersaultecu.py | 6 +++--- odxtools/unitspec.py | 19 ++++++++++--------- tests/test_unit_spec.py | 10 +++++----- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/examples/somersaultecu.py b/examples/somersaultecu.py index 7bca9cb4..9c55f083 100755 --- a/examples/somersaultecu.py +++ b/examples/somersaultecu.py @@ -2098,9 +2098,9 @@ class SomersaultSID(IntEnum): admin_data=None, data_object_props=NamedItemList(somersault_dops.values()), unit_spec=UnitSpec( - unit_groups=list(somersault_unit_groups.values()), - units=list(somersault_units.values()), - physical_dimensions=list(somersault_physical_dimensions.values()), + unit_groups=NamedItemList(somersault_unit_groups.values()), + units=NamedItemList(somersault_units.values()), + physical_dimensions=NamedItemList(somersault_physical_dimensions.values()), sdgs=[], ), tables=NamedItemList(somersault_tables.values()), diff --git a/odxtools/unitspec.py b/odxtools/unitspec.py index cf41bc59..30ef4dea 100644 --- a/odxtools/unitspec.py +++ b/odxtools/unitspec.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: MIT from dataclasses import dataclass -from typing import Any, Dict, List, Union +from typing import Any, Dict, List from xml.etree import ElementTree from .nameditemlist import NamedItemList @@ -25,9 +25,9 @@ class UnitSpec: """ # TODO (?): Why are there type errors... - unit_groups: Union[NamedItemList[UnitGroup], List[UnitGroup]] - units: Union[NamedItemList[Unit], List[Unit]] - physical_dimensions: Union[NamedItemList[PhysicalDimension], List[PhysicalDimension]] + unit_groups: NamedItemList[UnitGroup] + units: NamedItemList[Unit] + physical_dimensions: NamedItemList[PhysicalDimension] sdgs: List[SpecialDataGroup] def __post_init__(self) -> None: @@ -38,14 +38,15 @@ def __post_init__(self) -> None: @staticmethod def from_et(et_element: ElementTree.Element, doc_frags: List[OdxDocFragment]) -> "UnitSpec": - unit_groups = [ + unit_groups = NamedItemList([ UnitGroup.from_et(el, doc_frags) for el in et_element.iterfind("UNIT-GROUPS/UNIT-GROUP") - ] - units = [Unit.from_et(el, doc_frags) for el in et_element.iterfind("UNITS/UNIT")] - physical_dimensions = [ + ]) + units = NamedItemList( + [Unit.from_et(el, doc_frags) for el in et_element.iterfind("UNITS/UNIT")]) + physical_dimensions = NamedItemList([ PhysicalDimension.from_et(el, doc_frags) for el in et_element.iterfind("PHYSICAL-DIMENSIONS/PHYSICAL-DIMENSION") - ] + ]) sdgs = [ SpecialDataGroup.from_et(sdge, doc_frags) for sdge in et_element.iterfind("SDGS/SDG") ] diff --git a/tests/test_unit_spec.py b/tests/test_unit_spec.py index f0293754..a0d14f1a 100644 --- a/tests/test_unit_spec.py +++ b/tests/test_unit_spec.py @@ -30,7 +30,7 @@ class TestUnitSpec(unittest.TestCase): def test_read_odx(self) -> None: expected = UnitSpec( - physical_dimensions=[ + physical_dimensions=NamedItemList([ PhysicalDimension( odx_id=OdxLinkId("ID.metre", doc_frags), short_name="metre", @@ -45,8 +45,8 @@ def test_read_odx(self) -> None: long_name=None, description=None, ) - ], - units=[ + ]), + units=NamedItemList([ Unit( odx_id=OdxLinkId("ID.kilometre", doc_frags), oid=None, @@ -58,7 +58,7 @@ def test_read_odx(self) -> None: factor_si_to_unit=1000, offset_si_to_unit=0, ) - ], + ]), unit_groups=NamedItemList(), sdgs=[], ) @@ -143,7 +143,7 @@ def test_resolve_odxlinks(self) -> None: admin_data=None, data_object_props=NamedItemList([dop]), unit_spec=UnitSpec( - units=[unit], + units=NamedItemList([unit]), physical_dimensions=NamedItemList(), unit_groups=NamedItemList(), sdgs=[], From 809ac72b6aa24a247834c2fd141d3cb95faae541 Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Wed, 25 Sep 2024 12:20:06 +0200 Subject: [PATCH 2/2] Audience: make `.*abled_audiences` `NamedItemList` This is probably also an artifact from the early days and/or an oversight... Signed-off-by: Andreas Lauser Signed-off-by: Christian Hackenbeck --- odxtools/audience.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/odxtools/audience.py b/odxtools/audience.py index 93c6cfcb..513975c8 100644 --- a/odxtools/audience.py +++ b/odxtools/audience.py @@ -4,6 +4,7 @@ from xml.etree import ElementTree from .additionalaudience import AdditionalAudience +from .nameditemlist import NamedItemList from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef from .odxtypes import odxstr_to_bool from .snrefcontext import SnRefContext @@ -45,11 +46,11 @@ def is_aftermarket(self) -> bool: return self.is_aftermarket_raw in [None, True] @property - def enabled_audiences(self) -> List[AdditionalAudience]: + def enabled_audiences(self) -> NamedItemList[AdditionalAudience]: return self._enabled_audiences @property - def disabled_audiences(self) -> List[AdditionalAudience]: + def disabled_audiences(self) -> NamedItemList[AdditionalAudience]: return self._disabled_audiences @staticmethod @@ -85,12 +86,10 @@ def _build_odxlinks(self) -> Dict[OdxLinkId, Any]: return {} def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None: - self._enabled_audiences = [ - odxlinks.resolve(ref, AdditionalAudience) for ref in self.enabled_audience_refs - ] - self._disabled_audiences = [ - odxlinks.resolve(ref, AdditionalAudience) for ref in self.disabled_audience_refs - ] + self._enabled_audiences = NamedItemList( + [odxlinks.resolve(ref, AdditionalAudience) for ref in self.enabled_audience_refs]) + self._disabled_audiences = NamedItemList( + [odxlinks.resolve(ref, AdditionalAudience) for ref in self.disabled_audience_refs]) def _resolve_snrefs(self, context: SnRefContext) -> None: pass