From df179c9233b019039758c62b8d4165f910695868 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 11 May 2020 18:39:02 -0700 Subject: [PATCH] Improve type annotations for layer --- masque/file/gdsii.py | 6 +++--- masque/label.py | 10 +++++----- masque/shapes/arc.py | 4 ++-- masque/shapes/circle.py | 4 ++-- masque/shapes/ellipse.py | 4 ++-- masque/shapes/path.py | 6 +++--- masque/shapes/polygon.py | 10 +++++----- masque/shapes/shape.py | 8 ++++---- masque/shapes/text.py | 4 ++-- masque/utils.py | 1 + 10 files changed, 29 insertions(+), 28 deletions(-) diff --git a/masque/file/gdsii.py b/masque/file/gdsii.py index ca80cc6..7606b1f 100644 --- a/masque/file/gdsii.py +++ b/masque/file/gdsii.py @@ -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 diff --git a/masque/label.py b/masque/label.py index 1e46e6a..a5f8c5e 100644 --- a/masque/label.py +++ b/masque/label.py @@ -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 = () diff --git a/masque/shapes/arc.py b/masque/shapes/arc.py index 25aff16..f8b049c 100644 --- a/masque/shapes/arc.py +++ b/masque/shapes/arc.py @@ -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() diff --git a/masque/shapes/circle.py b/masque/shapes/circle.py index 8e47912..a306796 100644 --- a/masque/shapes/circle.py +++ b/masque/shapes/circle.py @@ -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() diff --git a/masque/shapes/ellipse.py b/masque/shapes/ellipse.py index 931cf95..af936d2 100644 --- a/masque/shapes/ellipse.py +++ b/masque/shapes/ellipse.py @@ -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() diff --git a/masque/shapes/path.py b/masque/shapes/path.py index 7b0ff9c..7d0ed17 100644 --- a/masque/shapes/path.py +++ b/masque/shapes/path.py @@ -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': """ diff --git a/masque/shapes/polygon.py b/masque/shapes/polygon.py index daef7fc..ab588aa 100644 --- a/masque/shapes/polygon.py +++ b/masque/shapes/polygon.py @@ -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': """ diff --git a/masque/shapes/shape.py b/masque/shapes/shape.py index 8c5b2cb..3f40841 100644 --- a/masque/shapes/shape.py +++ b/masque/shapes/shape.py @@ -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 diff --git a/masque/shapes/text.py b/masque/shapes/text.py index 53a9551..8741e3d 100644 --- a/masque/shapes/text.py +++ b/masque/shapes/text.py @@ -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, ): diff --git a/masque/utils.py b/masque/utils.py index 91ba8b2..bdb06a2 100644 --- a/masque/utils.py +++ b/masque/utils.py @@ -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: