From ee5b6e7a0c50d73d4fe4d28d33ffe0f0bcefcb58 Mon Sep 17 00:00:00 2001 From: Brian G <62308289+bdg221@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:07:38 -0700 Subject: [PATCH] feat: Custom repr for connection and port (#54) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add custom repr for port and connection classes * Forgot a character in the repr output * Prepend class name to the repr output * Update to pass black formatter * Handle when parent is None * Remove quotes when size is None --------- Co-authored-by: Michał Stęchły --- src/bartiq/_routine.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/bartiq/_routine.py b/src/bartiq/_routine.py index 1030b1e..8524677 100644 --- a/src/bartiq/_routine.py +++ b/src/bartiq/_routine.py @@ -380,6 +380,11 @@ class Port(BaseModel): size: Optional[AnnotatedValue] meta: Optional[dict[str, Any]] = Field(default_factory=dict) + def __repr__(self): + parent_name = "none" if self.parent is None else self.parent.name + size_value = "None" if self.size is None else f'"{self.size}"' + return f"{self.__class__.__name__}({parent_name}.#{self.name}, size={size_value}, {self.direction})" + @property def absolute_path(self) -> str: """Returns a path from root.""" @@ -406,6 +411,10 @@ class Connection(BaseModel): target: Port parent: Optional[Routine] = Field(exclude=True, default=None) + def __repr__(self): + parent_name = "none" if self.parent is None else self.parent.name + return f"{self.__class__.__name__}({parent_name}.#{self.source.name} -> {parent_name}.#{self.target.name})" + @field_serializer("source", "target") def _serialize_port(self, port): return port.name if port.parent is self.parent else f"{port.parent.name}.{port.name}"