From f919811936a9dc018c3f06973743288f718d9885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20D=C4=85browski?= Date: Thu, 19 Dec 2024 11:17:03 +0100 Subject: [PATCH] save --- .../data_record/abstract_data_record.py | 26 ++++++++--------- .../data_record/container_data_record.py | 28 +++++++++++++++++-- uds/database/data_record/raw_data_record.py | 18 +++--------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/uds/database/data_record/abstract_data_record.py b/uds/database/data_record/abstract_data_record.py index e93e33f..d5bed4d 100644 --- a/uds/database/data_record/abstract_data_record.py +++ b/uds/database/data_record/abstract_data_record.py @@ -35,7 +35,7 @@ int, # physical value is the same as raw value float, # physical value calculated through formula str, # decoded text value - Tuple[Tuple["DecodedDataRecord", ...], ...] # decoded container value, each element is another record + Tuple[Tuple["DecodedDataRecord", ...], ...] # decoded container value, each element is another entry ] """ Alias of Data Records' physical value. @@ -59,7 +59,12 @@ class DecodedDataRecord(TypedDict): class AbstractDataRecord(ABC): - """Common implementation and interface for all Data Records.""" + """ + Common implementation and interface for all Data Records. + + Data Records are fragments of diagnostic messages. + Each Data Record defines how to interpret a single element/signal. + """ def __init__(self, name: str) -> None: """ @@ -92,26 +97,21 @@ def max_raw_value(self): """ return (1 << self.length) - 1 - @property # noqa: F841 @abstractmethod def is_reoccurring(self) -> bool: """ Whether this Data Record might occur multiple times. Values meaning: - - False - exactly one occurrence in every diagnostic message - - True - number of occurrences might vary + - True - number of occurrences might vary + - False - constant number of occurrences in every diagnostic message """ + return self.min_occurrences != self.max_occurrences @property # noqa: F841 @abstractmethod def min_occurrences(self) -> int: - """ - Minimal number of this Data Record occurrences. - - .. note:: Relevant only if :attr:`~uds.database.abstract_data_record.AbstractDataRecord.is_reoccurring` - equals True. - """ + """Minimal number of this Data Record occurrences.""" @property # noqa: F841 @abstractmethod @@ -119,8 +119,6 @@ def max_occurrences(self) -> Optional[int]: """ Maximal number of this Data Record occurrences. - .. note:: Relevant only if :attr:`~uds.database.abstract_data_record.AbstractDataRecord.is_reoccurring` - equals True. .. warning:: No maximal number (infinite number of occurrences) is represented by None value. """ @@ -140,7 +138,7 @@ def decode(self, raw_value: int) -> DecodedDataRecord: # noqa: F841 """ @abstractmethod - def encode(self, physical_value: DataRecordPhysicalValueAlias) -> int: # noqa: F841 + def encode(self, physical_value: DataRecordValueAlias) -> int: # noqa: F841 """ Encode raw value for provided physical value. diff --git a/uds/database/data_record/container_data_record.py b/uds/database/data_record/container_data_record.py index 87f6336..ef12446 100644 --- a/uds/database/data_record/container_data_record.py +++ b/uds/database/data_record/container_data_record.py @@ -1,7 +1,7 @@ __all__ = ["ContainerDataRecord"] -from typing import Sequence, Optional, Tuple -from .abstract_data_record import AbstractDataRecord, DecodedDataRecord +from typing import Sequence, Optional, Tuple, Dict +from .abstract_data_record import AbstractDataRecord, DecodedDataRecord, DataRecordValueAlias from uds.utilities import InconsistentArgumentsError @@ -104,4 +104,26 @@ def decode(self, raw_value: int) -> DecodedDataRecord: :return: Dictionary with physical value for this Data Record. """ - # TODO \ No newline at end of file + # TODO + + def encode(self, physical_value: DataRecordValueAlias) -> int: # noqa: F841 + """ + Encode raw value for provided physical value. + + :param physical_value: Physical (meaningful e.g. float, str type) value of this Data Record. + + :return: Raw Value of this Data Record. + """ + if not isinstance(physical_value, Sequence): + raise TypeError + combined_raw_value = 0 + for single_record_value in physical_value: + if isinstance(single_record_value, int): + if 0 <= single_record_value <= self.max_raw_value: + entry_raw_value = single_record_value + elif isinstance(single_record_value, dict): + entry_raw_value = ... # TODO + else: + raise ValueError + combined_raw_value = (combined_raw_value << self.length) + entry_raw_value + return combined_raw_value \ No newline at end of file diff --git a/uds/database/data_record/raw_data_record.py b/uds/database/data_record/raw_data_record.py index 5da65b8..288357d 100644 --- a/uds/database/data_record/raw_data_record.py +++ b/uds/database/data_record/raw_data_record.py @@ -4,7 +4,7 @@ from typing import Optional, Tuple -from .abstract_data_record import AbstractDataRecord, DataRecordPhysicalValueAlias, DecodedDataRecord +from .abstract_data_record import AbstractDataRecord, DataRecordValueAlias, DecodedDataRecord class RawDataRecord(AbstractDataRecord): @@ -44,17 +44,6 @@ def length(self, value: int) -> None: raise ValueError("Length must be a positive integer.") self.__length = value - @property # noqa: F841 - def is_reoccurring(self) -> bool: - """ - Whether this Data Record might occur multiple times. - - Values meaning: - - False - exactly one occurrence in every diagnostic message - - True - number of occurrences might vary - """ - return False - @property # noqa: F841 def min_occurrences(self) -> int: """ @@ -102,11 +91,12 @@ def decode(self, raw_value: int) -> DecodedDataRecord: ) return DecodedDataRecord(name=self.name, raw_value=raw_value, physical_value=raw_value) - def encode(self, physical_value: DataRecordPhysicalValueAlias) -> int: + def encode(self, physical_value: DataRecordValueAlias) -> int: """ Encode raw value for provided physical value. - :param physical_value: Physical (meaningful e.g. float, str type) value of this Data Record. + :param physical_value: Physical value of this Data Record. + For this Data Record type, it is the same as raw value. :return: Raw Value of this Data Record. """