Skip to content
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
139 changes: 45 additions & 94 deletions src/kfactory/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import UserDict, UserList
from collections.abc import Callable, Hashable
from hashlib import sha3_512
from types import FunctionType
from types import FunctionType, UnionType
from typing import TYPE_CHECKING, Any, TypeGuard, overload

import numpy as np
Expand Down Expand Up @@ -311,113 +311,64 @@ def get_cell_name(
return name


_ISHAPES: UnionType = (
kdb.Box
| kdb.Edge
| kdb.Path
| kdb.Polygon
| kdb.Region
| kdb.SimplePolygon
| kdb.Text
)
_DSHAPES: UnionType = (
kdb.DBox | kdb.DEdge | kdb.DPath | kdb.DPolygon | kdb.DSimplePolygon | kdb.DText
)
_SERIALIZABLE_SHAPES: UnionType = (
kdb.CplxTrans
| kdb.DCplxTrans
| kdb.DEdgePair
| kdb.DPoint
| kdb.DTrans
| kdb.DVector
| kdb.EdgePair
| kdb.EdgePairs
| kdb.Edges
| kdb.ICplxTrans
| kdb.LayerInfo
| kdb.Matrix2d
| kdb.Matrix3d
| kdb.Point
| kdb.Texts
| kdb.Trans
| kdb.VCplxTrans
| kdb.Vector
| lay.LayerProperties
| _ISHAPES
| _DSHAPES
)
_SERIALIZABLE_VALUES_OR_SHAPES: UnionType = (
bool | int | float | str | _SERIALIZABLE_SHAPES
)


def serializible_value_or_shape_guard(
value: Any,
) -> TypeGuard[int | float | bool | str | SerializableShape]:
return isinstance(
value,
int
| float
| bool
| str
| kdb.Box
| kdb.DBox
| kdb.Edge
| kdb.DEdge
| kdb.EdgePair
| kdb.DEdgePair
| kdb.EdgePairs
| kdb.Edges
| lay.LayerProperties
| kdb.Matrix2d
| kdb.Matrix3d
| kdb.Path
| kdb.DPath
| kdb.Point
| kdb.DPoint
| kdb.Polygon
| kdb.DPolygon
| kdb.SimplePolygon
| kdb.DSimplePolygon
| kdb.Region
| kdb.Text
| kdb.DText
| kdb.Texts
| kdb.Trans
| kdb.DTrans
| kdb.CplxTrans
| kdb.ICplxTrans
| kdb.DCplxTrans
| kdb.VCplxTrans
| kdb.Vector
| kdb.DVector
| kdb.LayerInfo,
)
return isinstance(value, _SERIALIZABLE_VALUES_OR_SHAPES)


def serializible_shape_guard(
value: Any,
) -> TypeGuard[SerializableShape]:
return isinstance(
value,
kdb.Box
| kdb.DBox
| kdb.Edge
| kdb.DEdge
| kdb.EdgePair
| kdb.DEdgePair
| kdb.EdgePairs
| kdb.Edges
| lay.LayerProperties
| kdb.Matrix2d
| kdb.Matrix3d
| kdb.Path
| kdb.DPath
| kdb.Point
| kdb.DPoint
| kdb.Polygon
| kdb.DPolygon
| kdb.SimplePolygon
| kdb.DSimplePolygon
| kdb.Region
| kdb.Text
| kdb.DText
| kdb.Texts
| kdb.Trans
| kdb.DTrans
| kdb.CplxTrans
| kdb.ICplxTrans
| kdb.DCplxTrans
| kdb.VCplxTrans
| kdb.Vector
| kdb.DVector
| kdb.LayerInfo,
)
return isinstance(value, _SERIALIZABLE_SHAPES)


def ishape_guard(value: Any) -> TypeGuard[IShapeLike]:
return isinstance(
value,
kdb.Polygon
| kdb.Edge
| kdb.Path
| kdb.Box
| kdb.Text
| kdb.SimplePolygon
| kdb.Region,
)
return isinstance(value, _ISHAPES)


def dshape_guard(value: Any) -> TypeGuard[DShapeLike]:
return isinstance(
value,
kdb.DPolygon
| kdb.DEdge
| kdb.DPath
| kdb.DBox
| kdb.DText
| kdb.DSimplePolygon,
)
return isinstance(value, _DSHAPES)


def get_function_name(f: Callable[..., Any]) -> str:
Expand Down
Loading