Skip to content

Commit

Permalink
Add meaningful "representation" formatter to object classes (#432)
Browse files Browse the repository at this point in the history
Add __repr__() method for the object classes *Variable(), *Array() and *Record().  Extend ODVariable with a ".qualname" attribute which prepends the parent's name with a dot.
  • Loading branch information
sveinse committed May 16, 2024
1 parent 6817066 commit 3c1d86c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ def __init__(self, name: str, index: int):
self.subindices = {}
self.names = {}

def __repr__(self) -> str:
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04X}>"

def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
item = self.names.get(subindex) or self.subindices.get(subindex)
if item is None:
Expand Down Expand Up @@ -232,6 +235,9 @@ def __init__(self, name: str, index: int):
self.subindices = {}
self.names = {}

def __repr__(self) -> str:
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04X}>"

def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
var = self.names.get(subindex) or self.subindices.get(subindex)
if var is not None:
Expand Down Expand Up @@ -327,6 +333,17 @@ def __init__(self, name: str, index: int, subindex: int = 0):
#: Can this variable be mapped to a PDO
self.pdo_mappable = False

def __repr__(self) -> str:
suffix = f":{self.subindex:02X}" if isinstance(self.parent, (ODRecord, ODArray)) else ""
return f"<{type(self).__qualname__} {self.qualname!r} at 0x{self.index:04X}{suffix}>"

@property
def qualname(self) -> str:
"""Fully qualified name of the variable. If the variable is a subindex
of a record or array, the name will be prefixed with the parent's name."""
if isinstance(self.parent, (ODRecord, ODArray)):
return f"{self.parent.name}.{self.name}"
return self.name

def __eq__(self, other: "ODVariable") -> bool:
return (self.index == other.index and
Expand Down
3 changes: 3 additions & 0 deletions canopen/pdo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ def __init__(self, pdo_node, com_record, map_array):
self.is_received: bool = False
self._task = None

def __repr__(self) -> str:
return f"<{type(self).__qualname__} {self.name!r} at COB-ID 0x{self.cob_id:X}>"

def __getitem_by_index(self, value):
valid_values = []
for var in self.map:
Expand Down
6 changes: 6 additions & 0 deletions canopen/sdo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def __init__(self, sdo_node: SdoBase, od: ObjectDictionary):
self.sdo_node = sdo_node
self.od = od

def __repr__(self) -> str:
return f"<{type(self).__qualname__} {self.od.name!r} at 0x{self.od.index:04X}>"

def __getitem__(self, subindex: Union[int, str]) -> "SdoVariable":
return SdoVariable(self.sdo_node, self.od[subindex])

Expand All @@ -115,6 +118,9 @@ def __init__(self, sdo_node: SdoBase, od: ObjectDictionary):
self.sdo_node = sdo_node
self.od = od

def __repr__(self) -> str:
return f"<{type(self).__qualname__} {self.od.name!r} at 0x{self.od.index:04X}>"

def __getitem__(self, subindex: Union[int, str]) -> "SdoVariable":
return SdoVariable(self.sdo_node, self.od[subindex])

Expand Down
6 changes: 6 additions & 0 deletions canopen/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def __init__(self, od: objectdictionary.ODVariable):
#: Holds a local, overridable copy of the Object Subindex
self.subindex = od.subindex

def __repr__(self) -> str:
suffix = f":{self.subindex:02X}" if isinstance(self.od.parent,
(objectdictionary.ODRecord, objectdictionary.ODArray)
) else ""
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04X}{suffix}>"

def get_data(self) -> bytes:
raise NotImplementedError("Variable is not readable")

Expand Down

0 comments on commit 3c1d86c

Please sign in to comment.