Skip to content

Commit

Permalink
Second draft
Browse files Browse the repository at this point in the history
  • Loading branch information
igorapple committed Nov 20, 2024
1 parent 229fa6d commit f5b4f85
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions uds/database/text_data_record.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
"""Definition of TextDataRecord which is class for encode and decode values of data records."""
from typing import Optional, Tuple, Union
from typing import Optional, Tuple, Union, Dict
from uds.database.abstract_data_record import (AbstractDataRecord, DataRecordType, DataRecordPhysicalValueAlias,
DecodedDataRecord)


class TextDataRecord(AbstractDataRecord):
"""Implementation for Text Data Record."""
def __init__(self, name: str, text: str) -> None:
def __init__(self, name: str, length: int, mapping: Dict[int, str] = None) -> None:
"""Initialize Text Data Record.
:param name: Name to assign to this Data Record.
:raise TypeError: Provided value of name is not str type.
"""
super().__init__(name)
self.length = text
self.length = length
self.mapping = mapping

@property # noqa: F841
def length(self) -> int:
Expand Down Expand Up @@ -66,42 +67,40 @@ def contains(self) -> Tuple["AbstractDataRecord", ...]:
"""Get Data Records contained by this Data Record."""
# TODO

def decode(self, raw_value: Union[int, str]) -> DecodedDataRecord: # noqa: F841
def decode(self, physical_value: Union[int, str]) -> DecodedDataRecord: # noqa: F841
"""
Decode physical value for provided raw value.
:param raw_value: Raw (bit) value of Data Record.
:param physical_value: Raw (bit) value of Data Record.
:return: Dictionary with physical value for this Data Record.
"""
if isinstance(raw_value, int):
return DecodedDataRecord(name=self.name, raw_value=raw_value, physical_value=raw_value)
elif isinstance(raw_value, str):
decoded_value = int.from_bytes(raw_value.encode("utf-8"), byteorder="big")
return DecodedDataRecord(name=self.name, raw_value=decoded_value, physical_value=raw_value)
if isinstance(physical_value, int):
return DecodedDataRecord(name=self.name, raw_value=physical_value, physical_value=physical_value)

if self.mapping:
for k, v in self.mapping.items():
if v == physical_value:
return DecodedDataRecord(name=self.name, raw_value=k, physical_value=physical_value)
raise ValueError("physical_value not found in provided mapping.")
else:
raise TypeError("Raw_value must be int or str.")
raise TypeError("physical_value must be int or mapping must be provided.")

def encode(self, physical_value: DataRecordPhysicalValueAlias) -> Union[int, str]: # noqa: F841
def encode(self, physical_value: DataRecordPhysicalValueAlias) -> 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.
"""
def encode_value(value):
if isinstance(value, int):
return value
elif isinstance(value, str):
# TODO
elif isinstance(value, float):
# TODO

if isinstance(physical_value, (int, str, float)):
return encode_value(physical_value)
elif isinstance(physical_value, tuple):
if isinstance(physical_value, DecodedDataRecord):
return tuple(encode_value(elem["raw_value"]) for elem in physical_value)
else:
raise TypeError("Raw_value must be int or str.")
if isinstance(physical_value, int):
return physical_value
elif isinstance(physical_value, str):
if self.mapping:
for k, v in self.mapping.items():
if v == physical_value:
return k
raise ValueError("physical_value not found in provided mapping.")
else:
raise TypeError("During encoding of str mapping must be provided.")

0 comments on commit f5b4f85

Please sign in to comment.