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

Improve __repr__ of collection types #392

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,10 @@ def __eq__(self, other: Any) -> Any:
return list(self) == other

def __repr__(self) -> str:
entries = ", ".join(f"{item}" for item in self._object_list)
return f"{self._parent_object.name} - {self._name}({entries})"
return (
f"<{self.__class__.__name__}[{self._object_type.__name__}] "
+ f"with parent {self._parent_object!r}, entries {self._object_list!r}>"
)


def define_edge_property_list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ def sort(
def __eq__(self, other: Any) -> Any:
return list(self) == other

def __repr__(self) -> str:
return f"<LinkedObjectList([{', '.join(repr(val) for val in self)}])>"


ChildT = TypeVar("ChildT", bound=CreatableTreeObject)

Expand Down
10 changes: 10 additions & 0 deletions src/ansys/acp/core/_tree_objects/_grpc_helpers/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ def get(self, key: str, default: ValueT | None = None) -> ValueT | None:
except KeyError:
return default

def __repr__(self) -> str:
try:
from ..object_registry import object_registry

collection_label = self._collection_path.value.rsplit("/", 1)[-1]
value_type = object_registry[collection_label]
return f"<{self.__class__.__name__}[{value_type.__name__}] with keys {list(self)}>"
except:
return super().__repr__()


class MutableMapping(Mapping[CreatableValueT]):
"""Mutable mapping interface for collections of TreeObjects."""
Expand Down
Loading