Skip to content

Commit

Permalink
draft of formula data record
Browse files Browse the repository at this point in the history
  • Loading branch information
n135c10r committed Jan 15, 2025
1 parent a2ee482 commit 4afc9b4
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
4 changes: 3 additions & 1 deletion uds/database/data_record/abstract_data_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__all__ = ["AbstractDataRecord", "DataRecordPhysicalValueAlias", "DecodedDataRecord", "DataRecordValueAlias"]

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Dict, Optional, Sequence, Tuple, TypedDict, Union

DataRecordValueAlias = Union[
Expand Down Expand Up @@ -50,7 +51,8 @@
"""


class DecodedDataRecord(TypedDict):
@dataclass
class DecodedDataRecord:
"""Structure of decoded Data Record."""

name: str
Expand Down
121 changes: 121 additions & 0 deletions uds/database/data_record/formula_data_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""Definition of FormulaDataRecord."""

from typing import Callable, Optional, Tuple

Check warning on line 3 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L3

Added line #L3 was not covered by tests

from .abstract_data_record import AbstractDataRecord, DataRecordPhysicalValueAlias, DecodedDataRecord
from .raw_data_record import RawDataRecord

Check warning on line 6 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L5-L6

Added lines #L5 - L6 were not covered by tests


class CustomFormulaDataRecord(AbstractDataRecord):
def __init__(

Check warning on line 10 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L9-L10

Added lines #L9 - L10 were not covered by tests
self,
name: str,
length: int,
encode_function: Callable,
decode_function: Callable,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
is_reoccurring: bool = False,
min_occurrences: int = 1,
max_occurrences: Optional[int] = None,
) -> None:
super().__init__(name)
self._length = length
self._encode_function = encode_function
self._decode_function = decode_function
self._min_value = min_value
self._max_value = max_value
self._is_reoccurring = is_reoccurring
self._min_occurrences = min_occurrences
self._max_occurrences = max_occurrences

Check warning on line 30 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L22-L30

Added lines #L22 - L30 were not covered by tests

@property
def length(self) -> int:
return self._length

Check warning on line 34 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L32-L34

Added lines #L32 - L34 were not covered by tests

@property
def is_reoccurring(self) -> bool:
return self._is_reoccurring

Check warning on line 38 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L36-L38

Added lines #L36 - L38 were not covered by tests

@property
def min_occurrences(self) -> int:
return self._min_occurrences

Check warning on line 42 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L40-L42

Added lines #L40 - L42 were not covered by tests

@property
def max_occurrences(self) -> Optional[int]:
return self._max_occurrences

Check warning on line 46 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L44-L46

Added lines #L44 - L46 were not covered by tests

@property
def contains(self) -> Tuple["AbstractDataRecord", ...]:
return ()

Check warning on line 50 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L48-L50

Added lines #L48 - L50 were not covered by tests

def decode(self, raw_value: int) -> float:
raw_value = super().decode(raw_value)
return self._decode_function(raw_value)

Check warning on line 54 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L52-L54

Added lines #L52 - L54 were not covered by tests

def encode(self, physical_value: float) -> int:
physical_value = super().encode(physical_value)
return self._encode_function(physical_value)

Check warning on line 58 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L56-L58

Added lines #L56 - L58 were not covered by tests


class LinearFormulaDataRecord(RawDataRecord):
def __init__(

Check warning on line 62 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L61-L62

Added lines #L61 - L62 were not covered by tests
self,
name: str,
length: int,
factor: float,
offset: float,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
is_reoccurring: bool = False,
min_occurrences: int = 1,
max_occurrences: Optional[int] = 1
) -> None:
super().__init__(

Check warning on line 74 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L74

Added line #L74 was not covered by tests
name=name,
)
self._length = length
self._factor = factor
self._offset = offset
self._min_value = min_value
self._max_value = max_value
self._is_reoccurring = is_reoccurring
self._min_occurrences = min_occurrences
self._max_occurrences = max_occurrences

Check warning on line 84 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L77-L84

Added lines #L77 - L84 were not covered by tests

@property
def length(self) -> int:
return self._length

Check warning on line 88 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L86-L88

Added lines #L86 - L88 were not covered by tests

@property
def is_reoccurring(self) -> bool:
return self._is_reoccurring

Check warning on line 92 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L90-L92

Added lines #L90 - L92 were not covered by tests

@property
def min_occurrences(self) -> int:
return self._min_occurrences

Check warning on line 96 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L94-L96

Added lines #L94 - L96 were not covered by tests

@property
def max_occurrences(self) -> Optional[int]:
return self._max_occurrences

Check warning on line 100 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L98-L100

Added lines #L98 - L100 were not covered by tests

@property
def contains(self) -> Tuple["AbstractDataRecord", ...]:
return ()

Check warning on line 104 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L102-L104

Added lines #L102 - L104 were not covered by tests

def decode(self, raw_value: int) -> DecodedDataRecord:
decoded_data_record: DecodedDataRecord = super().decode(raw_value)
physical_value = (decoded_data_record.raw_value / self._factor) + self._offset

Check warning on line 108 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L106-L108

Added lines #L106 - L108 were not covered by tests
if (self._min_value is not None and physical_value < self._min_value) or \
(self._max_value is not None and physical_value > self._max_value):
raise ValueError("Decoded physical value out of expected range.")
decoded_data_record.physical_value = physical_value
return decoded_data_record

Check warning on line 113 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L111-L113

Added lines #L111 - L113 were not covered by tests

def encode(self, physical_value: DataRecordPhysicalValueAlias) -> int:
physical_value = super().encode(physical_value)

Check warning on line 116 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L115-L116

Added lines #L115 - L116 were not covered by tests
if (self._min_value is not None and physical_value < self._min_value) or \
(self._max_value is not None and physical_value > self._max_value):
raise ValueError("Provided physical value is out of expected range.")
raw_value = int((physical_value - self._offset) * self._factor)
return raw_value

Check warning on line 121 in uds/database/data_record/formula_data_record.py

View check run for this annotation

Codecov / codecov/patch

uds/database/data_record/formula_data_record.py#L119-L121

Added lines #L119 - L121 were not covered by tests

0 comments on commit 4afc9b4

Please sign in to comment.