Skip to content

Commit 3c1d86c

Browse files
authored
Add meaningful "representation" formatter to object classes (#432)
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.
1 parent 6817066 commit 3c1d86c

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

canopen/objectdictionary/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ def __init__(self, name: str, index: int):
176176
self.subindices = {}
177177
self.names = {}
178178

179+
def __repr__(self) -> str:
180+
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04X}>"
181+
179182
def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
180183
item = self.names.get(subindex) or self.subindices.get(subindex)
181184
if item is None:
@@ -232,6 +235,9 @@ def __init__(self, name: str, index: int):
232235
self.subindices = {}
233236
self.names = {}
234237

238+
def __repr__(self) -> str:
239+
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04X}>"
240+
235241
def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
236242
var = self.names.get(subindex) or self.subindices.get(subindex)
237243
if var is not None:
@@ -327,6 +333,17 @@ def __init__(self, name: str, index: int, subindex: int = 0):
327333
#: Can this variable be mapped to a PDO
328334
self.pdo_mappable = False
329335

336+
def __repr__(self) -> str:
337+
suffix = f":{self.subindex:02X}" if isinstance(self.parent, (ODRecord, ODArray)) else ""
338+
return f"<{type(self).__qualname__} {self.qualname!r} at 0x{self.index:04X}{suffix}>"
339+
340+
@property
341+
def qualname(self) -> str:
342+
"""Fully qualified name of the variable. If the variable is a subindex
343+
of a record or array, the name will be prefixed with the parent's name."""
344+
if isinstance(self.parent, (ODRecord, ODArray)):
345+
return f"{self.parent.name}.{self.name}"
346+
return self.name
330347

331348
def __eq__(self, other: "ODVariable") -> bool:
332349
return (self.index == other.index and

canopen/pdo/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ def __init__(self, pdo_node, com_record, map_array):
191191
self.is_received: bool = False
192192
self._task = None
193193

194+
def __repr__(self) -> str:
195+
return f"<{type(self).__qualname__} {self.name!r} at COB-ID 0x{self.cob_id:X}>"
196+
194197
def __getitem_by_index(self, value):
195198
valid_values = []
196199
for var in self.map:

canopen/sdo/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def __init__(self, sdo_node: SdoBase, od: ObjectDictionary):
9696
self.sdo_node = sdo_node
9797
self.od = od
9898

99+
def __repr__(self) -> str:
100+
return f"<{type(self).__qualname__} {self.od.name!r} at 0x{self.od.index:04X}>"
101+
99102
def __getitem__(self, subindex: Union[int, str]) -> "SdoVariable":
100103
return SdoVariable(self.sdo_node, self.od[subindex])
101104

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

121+
def __repr__(self) -> str:
122+
return f"<{type(self).__qualname__} {self.od.name!r} at 0x{self.od.index:04X}>"
123+
118124
def __getitem__(self, subindex: Union[int, str]) -> "SdoVariable":
119125
return SdoVariable(self.sdo_node, self.od[subindex])
120126

canopen/variable.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def __init__(self, od: objectdictionary.ODVariable):
2222
#: Holds a local, overridable copy of the Object Subindex
2323
self.subindex = od.subindex
2424

25+
def __repr__(self) -> str:
26+
suffix = f":{self.subindex:02X}" if isinstance(self.od.parent,
27+
(objectdictionary.ODRecord, objectdictionary.ODArray)
28+
) else ""
29+
return f"<{type(self).__qualname__} {self.name!r} at 0x{self.index:04X}{suffix}>"
30+
2531
def get_data(self) -> bytes:
2632
raise NotImplementedError("Variable is not readable")
2733

0 commit comments

Comments
 (0)