Skip to content

Commit

Permalink
feat: Custom repr for connection and port (#54)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
bdg221 and mstechly authored Jun 11, 2024
1 parent 8645d90 commit ee5b6e7
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/bartiq/_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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}"

0 comments on commit ee5b6e7

Please sign in to comment.