Skip to content

Commit

Permalink
chore: add methods for making D2Connection hashable and comparable …
Browse files Browse the repository at this point in the history
…to easily ensure uniqueness if needed (#17)

chore: add methods for making `D2Connection` hashable and comparable to easily ensure uniqueness if needed
  • Loading branch information
netsatan authored May 3, 2024
1 parent f3cd132 commit 587f25f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/py_d2/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ def lines(self) -> List[str]:

def __repr__(self) -> str:
return "\n".join(self.lines())

def __hash__(self):
return hash((self.shape_1, self.shape_2, self.label, self.direction))

def __eq__(self, other) -> bool:
if ((self.shape_1, self.shape_2, self.direction, self.label) == (other.shape_1, other.shape_2, other.direction, other.label)):
return True
return False
6 changes: 6 additions & 0 deletions tests/test_py_d2/test_d2_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ def test_d2_connection_direction_both():
def test_d2_connection_direction_none():
connection = D2Connection(shape_1="a", shape_2="b", direction=Direction.NONE)
assert str(connection) == "a -- b"


def test_d2_connection_uniqueness():
connection = D2Connection(shape_1="a", shape_2="b", direction=Direction.TO)
connection2 = D2Connection(shape_1="a", shape_2="b", direction=Direction.TO)
assert connection == connection2

0 comments on commit 587f25f

Please sign in to comment.