Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meaningful "representation" formatter to object classes #432

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 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,15 @@ 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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a docstring, as it is not obvious what qualname means and how it differs from name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Updated.

-- Having qualname on this object only hurts my eye a little bit, but its due to the special naming regime that pertains to this object only. Adding qualname to all the others will just be a do-nothing object for the purpose of duck typing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with having it only here. It's purely for internal use right now.

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}>"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you formatting with !r modifier? The attribute is already a simple string, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erlend-aasland perhaps you can chime in why !r is suitable here? I saw it as an opportunity to avoid using quotes in the f-string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For strings, the !r add quotes. If you don't want quotes, just remove !r. If you prefer the explicit style of adding quotes, go ahead with that; it's just a style preference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify:

>>> s = "thing"
>>> f"'{s}'"
"'thing'"
>>> f"{s!r}"
"'thing'"
>>> f"'{s}'" == f"{s!r}"
True


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
Loading