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

Add concrete iter for InstancePorts and DInstancePorts #597

Merged
merged 1 commit into from
Feb 1, 2025
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
11 changes: 10 additions & 1 deletion src/kfactory/instance_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def __getitem__(
@abstractmethod
def cell_ports(self) -> ProtoPorts[TUnit]: ...

def __iter__(self) -> Iterator[ProtoPort[TUnit]]:
def each_port(self) -> Iterator[ProtoPort[TUnit]]:
"""Create a copy of the ports to iterate through."""
if not self.instance.is_regular_array():
if not self.instance.is_complex():
Expand Down Expand Up @@ -220,6 +220,9 @@ def __iter__(self) -> Iterator[ProtoPort[TUnit]]:
for p in self.cell_ports
)

@abstractmethod
def __iter__(self) -> Iterator[ProtoPort[TUnit]]: ...

def each_by_array_coord(self) -> Iterator[tuple[int, int, ProtoPort[TUnit]]]:
if not self.instance.is_regular_array():
if not self.instance.is_complex():
Expand Down Expand Up @@ -356,6 +359,9 @@ def __getitem__(
) -> Port:
return Port(base=super().__getitem__(key).base)

def __iter__(self) -> Iterator[Port]:
yield from map(lambda p: p.to_itype(), self.each_port())


class DInstancePorts(ProtoTInstancePorts[float]):
def __init__(self, instance: DInstance) -> None:
Expand Down Expand Up @@ -388,6 +394,9 @@ def __getitem__(
) -> DPort:
return DPort(base=super().__getitem__(key).base)

def __iter__(self) -> Iterator[DPort]:
yield from map(lambda p: p.to_dtype(), self.each_port())


class VInstancePorts(ProtoInstancePorts[float, VInstance]):
"""Ports of an instance.
Expand Down
24 changes: 24 additions & 0 deletions tests/test_instance_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,30 @@ def test_vinstance_ports_contains(kcl: kf.KCLayout, layers: Layers) -> None:
trans=kf.kdb.DCplxTrans(mag=2),
)
assert ref.ports[0] in ref.ports
assert ref.ports[0].name is not None
assert ref.ports[0].name in ref.ports


def test_iter(kcl: kf.KCLayout, layers: Layers) -> None:
kcell = kcl.kcell()
iref = kcell.create_inst(
kf.factories.straight.straight_dbu_factory(kcl)(
width=5000, length=10000, layer=layers.WG
),
trans=kf.kdb.ICplxTrans(mag=2),
)
assert len(list(iref.ports)) == 2
assert all(isinstance(p, kf.Port) for p in iref.ports)

dkcell = kcl.dkcell()
dref = dkcell.create_inst(
kf.factories.straight.straight_dbu_factory(kcl)(
width=5000, length=10000, layer=layers.WG
),
trans=kf.kdb.DCplxTrans(mag=2),
)
assert len(list(dref.ports)) == 2
assert all(isinstance(p, kf.DPort) for p in dref.ports)


if __name__ == "__main__":
Expand Down
Loading