Improve type annotations for layer

This commit is contained in:
Jan Petykiewicz 2020-05-11 18:39:02 -07:00
parent 95ab0934b7
commit df179c9233
10 changed files with 29 additions and 28 deletions

View File

@ -20,7 +20,7 @@ import gzip
from .utils import mangle_name, make_dose_table
from .. import Pattern, SubPattern, GridRepetition, PatternError, Label, Shape
from ..shapes import Polygon, Path
from ..utils import rotation_matrix_2d, get_bit, set_bit, vector2, is_scalar
from ..utils import rotation_matrix_2d, get_bit, set_bit, vector2, is_scalar, layer_t
from ..utils import remove_colinear_vertices, normalize_mirror
#TODO document how GDS rotation / mirror works
@ -210,7 +210,7 @@ def dose2dtype(patterns: List[Pattern],
for shape in pat.shapes:
data_type = dose_vals_list.index(shape.dose * pat_dose)
if is_scalar(shape.layer):
if isinstance(shape.layer, int):
shape.layer = (shape.layer, data_type)
else:
shape.layer = (shape.layer[0], data_type)
@ -371,7 +371,7 @@ def read(stream: io.BufferedIOBase,
return patterns_dict, library_info
def _mlayer2gds(mlayer):
def _mlayer2gds(mlayer: layer_t) -> Tuple[int, int]:
""" Helper to turn a layer tuple-or-int into a layer and datatype"""
if is_scalar(mlayer):
layer = mlayer

View File

@ -4,10 +4,10 @@ import numpy
from numpy import pi
from .error import PatternError, PatternLockedError
from .utils import is_scalar, vector2, rotation_matrix_2d
__author__ = 'Jan Petykiewicz'
from .utils import is_scalar, vector2, rotation_matrix_2d, layer_t
class Label:
@ -19,7 +19,7 @@ class Label:
_offset: numpy.ndarray
""" [x_offset, y_offset] """
_layer: int or Tuple
_layer: layer_t
""" Layer (integer >= 0, or 2-Tuple of integers) """
_string: str
@ -56,14 +56,14 @@ class Label:
# layer property
@property
def layer(self) -> int or Tuple[int]:
def layer(self) -> layer_t:
"""
Layer number (int or tuple of ints)
"""
return self._layer
@layer.setter
def layer(self, val: int or List[int]):
def layer(self, val: layer_t):
self._layer = val
# string property
@ -81,7 +81,7 @@ class Label:
def __init__(self,
string: str,
offset: vector2=(0.0, 0.0),
layer: int=0,
layer: layer_t = 0,
locked: bool = False):
self.unlock()
self.identifier = ()

View File

@ -6,10 +6,10 @@ from numpy import pi
from . import Shape, Polygon, normalized_shape_tuple, DEFAULT_POLY_NUM_POINTS
from .. import PatternError
from ..utils import is_scalar, vector2
__author__ = 'Jan Petykiewicz'
from ..utils import is_scalar, vector2, layer_t
class Arc(Shape):
@ -158,7 +158,7 @@ class Arc(Shape):
offset: vector2 = (0.0, 0.0),
rotation: float = 0,
mirrored: Tuple[bool] = (False, False),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
locked: bool = False):
self.unlock()

View File

@ -5,10 +5,10 @@ from numpy import pi
from . import Shape, Polygon, normalized_shape_tuple, DEFAULT_POLY_NUM_POINTS
from .. import PatternError
from ..utils import is_scalar, vector2
__author__ = 'Jan Petykiewicz'
from ..utils import is_scalar, vector2, layer_t
class Circle(Shape):
@ -46,7 +46,7 @@ class Circle(Shape):
poly_num_points: int = DEFAULT_POLY_NUM_POINTS,
poly_max_arclen: float = None,
offset: vector2 = (0.0, 0.0),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
locked: bool = False):
self.unlock()

View File

@ -6,10 +6,10 @@ from numpy import pi
from . import Shape, Polygon, normalized_shape_tuple, DEFAULT_POLY_NUM_POINTS
from .. import PatternError
from ..utils import is_scalar, rotation_matrix_2d, vector2
__author__ = 'Jan Petykiewicz'
from ..utils import is_scalar, rotation_matrix_2d, vector2, layer_t
class Ellipse(Shape):
@ -93,7 +93,7 @@ class Ellipse(Shape):
offset: vector2 = (0.0, 0.0),
rotation: float = 0,
mirrored: Tuple[bool] = (False, False),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
locked: bool = False):
self.unlock()

View File

@ -6,7 +6,7 @@ from numpy import pi, inf
from . import Shape, normalized_shape_tuple, Polygon, Circle
from .. import PatternError
from ..utils import is_scalar, rotation_matrix_2d, vector2
from ..utils import is_scalar, rotation_matrix_2d, vector2, layer_t
from ..utils import remove_colinear_vertices, remove_duplicate_vertices
__author__ = 'Jan Petykiewicz'
@ -144,7 +144,7 @@ class Path(Shape):
offset: vector2 = (0.0, 0.0),
rotation: float = 0,
mirrored: Tuple[bool] = (False, False),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
locked: bool = False,
) -> 'Path':
@ -182,7 +182,7 @@ class Path(Shape):
offset: vector2 = (0.0, 0.0),
rotation: float = 0,
mirrored: Tuple[bool] = (False, False),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
) -> 'Path':
"""

View File

@ -5,7 +5,7 @@ from numpy import pi
from . import Shape, normalized_shape_tuple
from .. import PatternError
from ..utils import is_scalar, rotation_matrix_2d, vector2
from ..utils import is_scalar, rotation_matrix_2d, vector2, layer_t
from ..utils import remove_colinear_vertices, remove_duplicate_vertices
__author__ = 'Jan Petykiewicz'
@ -74,7 +74,7 @@ class Polygon(Shape):
offset: vector2 = (0.0, 0.0),
rotation: float = 0.0,
mirrored: Tuple[bool] = (False, False),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
locked: bool = False,
):
@ -100,7 +100,7 @@ class Polygon(Shape):
def square(side_length: float,
rotation: float = 0.0,
offset: vector2 = (0.0, 0.0),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
) -> 'Polygon':
"""
@ -130,7 +130,7 @@ class Polygon(Shape):
ly: float,
rotation: float = 0,
offset: vector2 = (0.0, 0.0),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
) -> 'Polygon':
"""
@ -164,7 +164,7 @@ class Polygon(Shape):
yctr: float = None,
ymax: float = None,
ly: float = None,
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
) -> 'Polygon':
"""

View File

@ -4,7 +4,7 @@ import copy
import numpy
from ..error import PatternError, PatternLockedError
from ..utils import is_scalar, rotation_matrix_2d, vector2
from ..utils import is_scalar, rotation_matrix_2d, vector2, layer_t
__author__ = 'Jan Petykiewicz'
@ -29,7 +29,7 @@ class Shape(metaclass=ABCMeta):
_offset: numpy.ndarray
""" `[x_offset, y_offset]` """
_layer: int or Tuple
_layer: layer_t
""" Layer (integer >= 0 or tuple) """
_dose: float
@ -162,14 +162,14 @@ class Shape(metaclass=ABCMeta):
# layer property
@property
def layer(self) -> int or Tuple[int]:
def layer(self) -> layer_t:
"""
Layer number (int or tuple of ints)
"""
return self._layer
@layer.setter
def layer(self, val: int or List[int]):
def layer(self, val: layer_t):
self._layer = val
# dose property

View File

@ -5,7 +5,7 @@ from numpy import pi, inf
from . import Shape, Polygon, normalized_shape_tuple
from .. import PatternError
from ..utils import is_scalar, vector2, get_bit, normalize_mirror
from ..utils import is_scalar, vector2, get_bit, normalize_mirror, layer_t
# Loaded on use:
# from freetype import Face
@ -76,7 +76,7 @@ class Text(Shape):
offset: vector2 = (0.0, 0.0),
rotation: float = 0.0,
mirrored: Tuple[bool] = (False, False),
layer: int = 0,
layer: layer_t = 0,
dose: float = 1.0,
locked: bool = False,
):

View File

@ -8,6 +8,7 @@ import numpy
# Type definitions
vector2 = Union[numpy.ndarray, Tuple[float, float]]
layer_t = Union[int, Tuple[int, int]]
def is_scalar(var: Any) -> bool: