Skip to content

Commit

Permalink
add valiator and eq to BasePort
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewMckee4 committed Jan 31, 2025
1 parent 31b01fa commit 4760478
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/kfactory/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pydantic import (
BaseModel,
model_serializer,
model_validator,
)
from ruamel.yaml.constructor import BaseConstructor
from typing_extensions import TypedDict
Expand Down Expand Up @@ -116,6 +117,14 @@ class BasePort(BaseModel, arbitrary_types_allowed=True):
info: Info = Info()
port_type: str

@model_validator(mode="after")
def check_exclusivity(self) -> Self:
if self.trans is None and self.dcplx_trans is None:
raise ValueError("Both trans and dcplx_trans cannot be None.")

Check warning on line 123 in src/kfactory/port.py

View check run for this annotation

Codecov / codecov/patch

src/kfactory/port.py#L123

Added line #L123 was not covered by tests
if self.trans is not None and self.dcplx_trans is not None:
raise ValueError("Only one of trans or dcplx_trans can be set.")

Check warning on line 125 in src/kfactory/port.py

View check run for this annotation

Codecov / codecov/patch

src/kfactory/port.py#L125

Added line #L125 was not covered by tests
return self

def __copy__(self) -> BasePort:
"""Copy the BasePort."""
return BasePort(
Expand Down Expand Up @@ -192,6 +201,33 @@ def get_dcplx_trans(self) -> kdb.DCplxTrans:
assert self.trans is not None, "Both trans and dcplx_trans are None"
return kdb.DCplxTrans(self.trans.to_dtype(self.kcl.dbu))

def __eq__(self, other: object) -> bool:
if not isinstance(other, BasePort):
return False

Check warning on line 206 in src/kfactory/port.py

View check run for this annotation

Codecov / codecov/patch

src/kfactory/port.py#L206

Added line #L206 was not covered by tests
return (
(self.trans is None and other.trans is None)
or (
(
self.trans is not None
and other.trans is not None
and self.trans == other.trans
)
and (self.dcplx_trans is None and other.dcplx_trans is None)
)
or (
(
self.dcplx_trans is not None
and other.dcplx_trans is not None
and self.dcplx_trans == other.dcplx_trans
)
and self.name == other.name
and self.kcl == other.kcl
and self.cross_section == other.cross_section
and self.port_type == other.port_type
and self.info == other.info
)
)


class ProtoPort(Generic[TUnit], ABC):
"""Base class for kf.Port, kf.DPort."""
Expand Down
33 changes: 33 additions & 0 deletions tests/test_packing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

import kfactory as kf


def test_pack_kcells(kcl: kf.KCLayout) -> None:
c = kcl.kcell()
straight = kf.factories.straight.straight_dbu_factory(kcl)(
width=1000, length=1000, layer=kf.kdb.LayerInfo(1, 0)
)
instance_group = kf.packing.pack_kcells(
c, [straight] * 4, max_height=2000, max_width=2000
)
assert instance_group.bbox() == kf.kdb.DBox(0, 0, 2000, 2000)


def test_pack_instances(kcl: kf.KCLayout) -> None:
c = kcl.kcell()
straight = kf.factories.straight.straight_dbu_factory(kcl)(
width=1000, length=1000, layer=kf.kdb.LayerInfo(1, 0)
)
ref = c << straight
ref2 = c << straight
ref3 = c << straight
ref4 = c << straight
instance_group = kf.packing.pack_instances(
c, [ref, ref2, ref3, ref4], max_height=2000, max_width=2000
)
assert instance_group.bbox() == kf.kdb.DBox(0, 0, 2000, 2000)


if __name__ == "__main__":
pytest.main(["-s", __file__])
4 changes: 2 additions & 2 deletions tests/test_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def test_dports_add_port(kcl: kf.KCLayout, layers: Layers) -> None:
port4.base.trans = kf.kdb.Trans.R90
port4.base.dcplx_trans = None
out_port = ports.add_port(port=port4, name="o6")
assert "o6" in ports.get_all_named().keys()
assert out_port in ports


def test_dports_create_port(kcl: kf.KCLayout, layers: Layers) -> None:
Expand All @@ -474,7 +474,7 @@ def test_dports_create_port(kcl: kf.KCLayout, layers: Layers) -> None:
ports.create_port(name="o1", width=0.001, layer=1, center=(1000, 1000), angle=1)

port = ports.create_port(name="o1", width=10, layer=1, trans=kf.kdb.Trans.R90)
assert port in ports.get_all_named().values()
assert port in ports


def test_dports_get_all_named(kcl: kf.KCLayout, layers: Layers) -> None:
Expand Down

0 comments on commit 4760478

Please sign in to comment.