Source code for cantools.database.can.formats.dbc.dbc_attribute

from typing import Generic

from .dbc_attribute_definition import (
    DbcAttributeDefinition,
    DbcAttributeValueTypeVar,
)


[docs] class DbcAttribute(Generic[DbcAttributeValueTypeVar]): """An attribute that can be associated with nodes/messages/signals. """ def __init__(self, value: DbcAttributeValueTypeVar, definition: DbcAttributeDefinition[DbcAttributeValueTypeVar]) -> None: self._value: DbcAttributeValueTypeVar = value self._definition: DbcAttributeDefinition[DbcAttributeValueTypeVar] = definition @property def name(self) -> str: """The attribute name as a string. """ return self._definition.name @property def value(self) -> DbcAttributeValueTypeVar: """The value that this attribute has. """ return self._value @value.setter def value(self, value: DbcAttributeValueTypeVar) -> None: self._value = value @property def formatted_value(self) -> str: """The value of the attribute as it appears in the DBC file """ if self.definition.type_name == "STRING": return f'"{self._value}"' return f'{self._value}' @property def definition(self) -> DbcAttributeDefinition[DbcAttributeValueTypeVar]: """The attribute definition. """ return self._definition def __repr__(self) -> str: return f"attribute('{self.name}', {self.value})"
DbcAttributeType = DbcAttribute[str] | DbcAttribute[int] | DbcAttribute[float]