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 tag support for cell decorator #496

Merged
merged 1 commit into from
Oct 17, 2024
Merged
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
39 changes: 18 additions & 21 deletions src/kfactory/kcell.py
Original file line number Diff line number Diff line change
Expand Up @@ -4213,17 +4213,6 @@ def to_dbu(
"""Convert Shapes or values in dbu to DShapes or floats in um."""
return kdb.CplxTrans(self.layout.dbu).inverted() * other

# @computed_field # type: ignore[prop-decorator]
# @property
# def name(self) -> str:
# """Name of the KCLayout."""
# return self._name

# @property
# def settings(self) -> KCellSettings:
# """Settings dictionary set by __init__ with metainfo."""
# return self._settings

@overload
def cell(
self,
Expand Down Expand Up @@ -4252,6 +4241,7 @@ def cell(
info: dict[str, MetaData] | None = None,
post_process: Iterable[Callable[[KCell], None]] = tuple(),
debug_names: bool | None = None,
tags: list[str] | None = None,
) -> Callable[[KCellFunc[KCellParams, KCell]], KCellFunc[KCellParams, KCell]]: ...

def cell(
Expand All @@ -4274,6 +4264,7 @@ def cell(
info: dict[str, MetaData] | None = None,
post_process: Iterable[Callable[[KC], None]] = tuple(),
debug_names: bool | None = None,
tags: list[str] | None = None,
) -> (
KCellFunc[KCellParams, KC]
| Callable[[KCellFunc[KCellParams, KCell]], KCellFunc[KCellParams, KCell]]
Expand Down Expand Up @@ -4320,6 +4311,9 @@ def cell(
post_process: List of functions to call after the cell has been created.
debug_names: Check on setting the name whether a cell with this name already
exists.
tags: Tag cell functions with user defined tags. With this, cell functions
can then be retrieved with `kcl.factories.tags[my_tag]` or if filtered
for multiple `kcl.factories.for_tags([my_tag1, my_tag2, ...])`.
"""
if check_instances is None:
check_instances = config.check_instances
Expand Down Expand Up @@ -4580,6 +4574,9 @@ def wrapped_cell(
function_name = f.func.__name__
else:
raise ValueError(f"Function {f} has no name.")
if tags:
for tag in tags:
self.factories.tags[tag].append(wrapper_autocell)
self.factories[basename or function_name] = wrapper_autocell
return wrapper_autocell

Expand Down Expand Up @@ -5286,15 +5283,25 @@ def layerenum_from_dict(


class Factories(UserDict[str, Callable[..., T]]):
tags: dict[str, list[Callable[..., T]]]

def __init__(self, data: dict[str, Callable[..., T]]) -> None:
super().__init__(data)
self.tags = defaultdict(list)

def __getattr__(self, name: str) -> Any:
if name != "data":
return self.data[name]
else:
self.__getattribute__(name)

def for_tags(self, tags: list[str]) -> list[Callable[..., T]]:
if len(tags) > 0:
tag_set = set(self.tags[tags[0]])
for tag in tags[1:]:
tag_set &= set(self.tags[tag])
return list(tag_set)


class VShapes(BaseModel, arbitrary_types_allowed=True):
"""Emulate `[klayout.db.Shapes][klayout.db.Shapes]`."""
Expand All @@ -5304,7 +5311,6 @@ class VShapes(BaseModel, arbitrary_types_allowed=True):
_bbox: kdb.DBox = kdb.DBox()

def __init__(self, cell: VKCell, _shapes: list[ShapeLike] | None = None):
# self._shapes = [_shape for _shape in _shapes] or []
BaseModel.__init__(self, cell=cell)
self._shapes = _shapes or []

Expand Down Expand Up @@ -6899,7 +6905,6 @@ def __init__(
self.cross_section = cross_section
self.info = Info(**info)
if trans is not None:
# self.width = cast(int, width)
if isinstance(trans, str):
self.trans = kdb.Trans.from_s(trans)
else:
Expand Down Expand Up @@ -7309,14 +7314,6 @@ def dwidth(self) -> float:
"""Width of the port in um."""
return self.kcl.to_um(self.width)

# @dwidth.setter
# def dwidth(self, value: float) -> None:
# self.width = self.kcl.to_dbu(value)
# assert self.kcl.to_um(self.width) == float(value), (
# "When converting to dbu the width does not match the desired width"
# f"({self.dwidth} / {value})!"
# )

@property
def dmirror(self) -> bool:
"""Mirror flag of the port."""
Expand Down
Loading