Skip to content

Commit

Permalink
Merge pull request #185 from gdsfactory/182-fix-name-in-cellports
Browse files Browse the repository at this point in the history
Added __contains__ to port and __eq__
  • Loading branch information
sebastian-goeldi authored Sep 25, 2023
2 parents 49366a8 + 1ed56fe commit f21991d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/182.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added __contains__ to port and __eq__
24 changes: 24 additions & 0 deletions src/kfactory/kcell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,20 @@ def from_yaml(cls: type[Port], constructor, node) -> Port: # type: ignore
d = dict(constructor.construct_pairs(node))
return cls(**d)

def __eq__(self, other: object) -> bool:
if isinstance(other, Port):
if (
self.width == other.width
and self._trans == other._trans
and self._dcplx_trans == other._dcplx_trans
and self.name == other.name
and self.layer == other.layer
and self.port_type == other.port_type
and self.info == other.info
):
return True
return False

def copy(self, trans: kdb.Trans | kdb.DCplxTrans = kdb.Trans.R0) -> Port:
"""Get a copy of a port.
Expand Down Expand Up @@ -3540,6 +3554,16 @@ def __iter__(self) -> Iterator[Port]:
"""Iterator, that allows for loops etc to directly access the object."""
yield from self._ports

def __contains__(self, port: str | Port) -> bool:
"""Check whether a port is in this port collection."""
if isinstance(port, Port):
return port in self._ports
else:
for _port in self._ports:
if _port.name == port:
return True
return False

def add_port(
self, port: Port, name: str | None = None, keep_mirror: bool = False
) -> Port:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,10 @@ def test_addports_keep_mirror(LAYER):
t2_mirr.mirror = not t2_mirr.is_mirror()

assert t1 == t2_mirr


def test_contains(LAYER: type[kf.LayerEnum]) -> None:
s = kf.cells.straight.straight(width=1, length=10, layer=LAYER.WG)
assert "o1" in s.ports
assert s.ports["o1"] in s.ports
assert s.ports["o1"].copy() in s.ports

0 comments on commit f21991d

Please sign in to comment.