Use __slots__ for class members

Also use the other sort of type hints for instance variables
This commit is contained in:
Jan Petykiewicz 2019-05-17 00:37:56 -07:00
parent 0b962999b2
commit 38f64f7c62
11 changed files with 86 additions and 69 deletions

View File

@ -14,15 +14,15 @@ class Label:
""" """
A circle, which has a position and radius. A circle, which has a position and radius.
""" """
__slots__ = ('_offset', '_layer', '_string', 'identifier')
# [x_offset, y_offset] # [x_offset, y_offset]
_offset = numpy.array([0.0, 0.0]) # type: numpy.ndarray _offset: numpy.ndarray
# Layer (integer >= 0) # Layer (integer >= 0) or 2-Tuple of integers
_layer = 0 # type: int or Tuple _layer: int or Tuple
# Label string # Label string
_string = None # type: str _string: str
# Arbitrary identifier tuple # Arbitrary identifier tuple
identifier: Tuple identifier: Tuple

View File

@ -33,10 +33,11 @@ class Pattern:
may reference the same Pattern object. may reference the same Pattern object.
:var name: An identifier for this object. Not necessarily unique. :var name: An identifier for this object. Not necessarily unique.
""" """
shapes = None # type: List[Shape] __slots__ = ('shapes', 'labels', 'subpatterns', 'name')
labels = None # type: List[Labels] shapes: List[Shape]
subpatterns = None # type: List[SubPattern or GridRepetition] labels: List[Label]
name = None # type: str subpatterns: List[SubPattern or GridRepetition]
name: str
def __init__(self, def __init__(self,
shapes: List[Shape]=(), shapes: List[Shape]=(),

View File

@ -23,19 +23,30 @@ class GridRepetition:
GridRepetition provides support for efficiently embedding multiple copies of a Pattern GridRepetition provides support for efficiently embedding multiple copies of a Pattern
into another Pattern at regularly-spaced offsets. into another Pattern at regularly-spaced offsets.
""" """
__slots__ = ('pattern',
'_offset',
'_rotation',
'_dose',
'_scale',
'_mirrored',
'_a_vector',
'_b_vector',
'a_count',
'b_count',
'identifier')
pattern = None # type: Pattern pattern: 'Pattern'
_offset = (0.0, 0.0) # type: numpy.ndarray _offset: numpy.ndarray
_rotation = 0.0 # type: float _rotation: float
_dose = 1.0 # type: float _dose: float
_scale = 1.0 # type: float _scale: float
_mirrored = None # type: List[bool] _mirrored: List[bool]
_a_vector = None # type: numpy.ndarray _a_vector: numpy.ndarray
_b_vector = None # type: numpy.ndarray or None _b_vector: numpy.ndarray or None
a_count = None # type: int a_count: int
b_count = 1 # type: int b_count: int
identifier: Tuple identifier: Tuple

View File

@ -20,15 +20,14 @@ class Arc(Shape):
The rotation gives the angle from x-axis, counterclockwise, to the first (x) radius. The rotation gives the angle from x-axis, counterclockwise, to the first (x) radius.
The start and stop angle are measured counterclockwise from the first (x) radius. The start and stop angle are measured counterclockwise from the first (x) radius.
""" """
__slots__ = ('_radii', '_angles', '_width', '_rotation',
_radii = None # type: numpy.ndarray 'poly_num_points', 'poly_max_arclen')
_angles = None # type: numpy.ndarray _radii: numpy.ndarray
_width = 1.0 # type: float _angles: numpy.ndarray
_rotation = 0.0 # type: float _width: float
_rotation: float
# Defaults for to_polygons poly_num_points: int
poly_num_points = DEFAULT_POLY_NUM_POINTS # type: int poly_max_arclen: float
poly_max_arclen = None # type: float
# radius properties # radius properties
@property @property
@ -148,6 +147,7 @@ class Arc(Shape):
mirrored: Tuple[bool] = (False, False), mirrored: Tuple[bool] = (False, False),
layer: int=0, layer: int=0,
dose: float=1.0): dose: float=1.0):
self.identifier = ()
self.radii = radii self.radii = radii
self.angles = angles self.angles = angles
self.width = width self.width = width

View File

@ -14,12 +14,10 @@ class Circle(Shape):
""" """
A circle, which has a position and radius. A circle, which has a position and radius.
""" """
__slots__ = ('_radius', 'poly_num_points', 'poly_max_arclen')
_radius = None # type: float _radius: float
poly_num_points: int
# Defaults for to_polygons poly_max_arclen: float
poly_num_points = DEFAULT_POLY_NUM_POINTS # type: int
poly_max_arclen = None # type: float
# radius property # radius property
@property @property
@ -46,6 +44,7 @@ class Circle(Shape):
offset: vector2=(0.0, 0.0), offset: vector2=(0.0, 0.0),
layer: int=0, layer: int=0,
dose: float=1.0): dose: float=1.0):
self.identifier = ()
self.offset = numpy.array(offset, dtype=float) self.offset = numpy.array(offset, dtype=float)
self.layer = layer self.layer = layer
self.dose = dose self.dose = dose

View File

@ -16,13 +16,12 @@ class Ellipse(Shape):
An ellipse, which has a position, two radii, and a rotation. An ellipse, which has a position, two radii, and a rotation.
The rotation gives the angle from x-axis, counterclockwise, to the first (x) radius. The rotation gives the angle from x-axis, counterclockwise, to the first (x) radius.
""" """
__slots__ = ('_radii', '_rotation',
_radii = None # type: numpy.ndarray 'poly_num_points', 'poly_max_arclen')
_rotation = 0.0 # type: float _radii: numpy.ndarray
_rotation: float
# Defaults for to_polygons poly_num_points: int
poly_num_points = DEFAULT_POLY_NUM_POINTS # type: int poly_max_arclen: float
poly_max_arclen = None # type: float
# radius properties # radius properties
@property @property
@ -89,6 +88,7 @@ class Ellipse(Shape):
mirrored: Tuple[bool] = (False, False), mirrored: Tuple[bool] = (False, False),
layer: int=0, layer: int=0,
dose: float=1.0): dose: float=1.0):
self.identifier = ()
self.radii = radii self.radii = radii
self.offset = offset self.offset = offset
self.rotation = rotation self.rotation = rotation

View File

@ -19,10 +19,11 @@ class Path(Shape):
A normalized_form(...) is available, but can be quite slow with lots of vertices. A normalized_form(...) is available, but can be quite slow with lots of vertices.
""" """
_vertices = None # type: numpy.ndarray __slots__ = ('_vertices', '_width', '_cap', '_cap_extensions')
_width = None # type: float _vertices: numpy.ndarray
_cap = None # type: Path.Cap _width: float
_cap_extensions = None # type: numpy.ndarray or None _cap: 'Path.Cap'
_cap_extensions: numpy.ndarray or None
class Cap(Enum): class Cap(Enum):
Flush = 0 # Path ends at final vertices Flush = 0 # Path ends at final vertices
@ -151,6 +152,9 @@ class Path(Shape):
layer: int=0, layer: int=0,
dose: float=1.0, dose: float=1.0,
) -> 'Path': ) -> 'Path':
self._cap_extensions = None # Since .cap setter might access it
self.identifier = ()
self.offset = offset self.offset = offset
self.layer = layer self.layer = layer
self.dose = dose self.dose = dose

View File

@ -18,7 +18,8 @@ class Polygon(Shape):
A normalized_form(...) is available, but can be quite slow with lots of vertices. A normalized_form(...) is available, but can be quite slow with lots of vertices.
""" """
_vertices = None # type: numpy.ndarray __slots__ = ('_vertices',)
_vertices: numpy.ndarray
# vertices property # vertices property
@property @property
@ -77,6 +78,7 @@ class Polygon(Shape):
layer: int=0, layer: int=0,
dose: float=1.0, dose: float=1.0,
): ):
self.identifier = ()
self.layer = layer self.layer = layer
self.dose = dose self.dose = dose
self.vertices = vertices self.vertices = vertices

View File

@ -24,18 +24,13 @@ class Shape(metaclass=ABCMeta):
""" """
Abstract class specifying functions common to all shapes. Abstract class specifying functions common to all shapes.
""" """
__slots__ = ('_offset', '_layer', '_dose', 'identifier')
# [x_offset, y_offset] _offset: numpy.ndarray # [x_offset, y_offset]
_offset = numpy.array([0.0, 0.0]) # type: numpy.ndarray _layer: int or Tuple # Layer (integer >= 0 or tuple)
_dose: float # Dose
# Layer (integer >= 0 or tuple) identifier: Tuple # An arbitrary identifier for the shape,
_layer = 0 # type: int or Tuple # usually empty but used by Pattern.flatten()
# Dose
_dose = 1.0 # type: float
# An arbitrary identifier for the shape, usually not set but used by Pattern.flatten()
identifier = () # type: Tuple
# --- Abstract methods # --- Abstract methods
@abstractmethod @abstractmethod

View File

@ -16,11 +16,16 @@ __author__ = 'Jan Petykiewicz'
class Text(Shape): class Text(Shape):
_string = '' # type: str """
_height = 1.0 # type: float Text (to be printed e.g. as a set of polygons).
_rotation = 0.0 # type: float This is distinct from non-printed Label objects.
_mirrored = None # type: List[str] """
font_path = '' # type: str __slots__ = ('_string', '_height', '_rotation', '_mirrored', 'font_path')
_string: str
_height: float
_rotation: float
_mirrored: List[str]
font_path: str
# vertices property # vertices property
@property @property

View File

@ -21,13 +21,13 @@ class SubPattern:
SubPattern provides basic support for nesting Pattern objects within each other, by adding SubPattern provides basic support for nesting Pattern objects within each other, by adding
offset, rotation, scaling, and associated methods. offset, rotation, scaling, and associated methods.
""" """
__slots__ = ('pattern', '_offset', '_rotation', '_dose', '_scale', '_mirrored', 'identifier')
pattern = None # type: Pattern pattern: 'Pattern'
_offset = (0.0, 0.0) # type: numpy.ndarray _offset: numpy.ndarray
_rotation = 0.0 # type: float _rotation: float
_dose = 1.0 # type: float _dose: float
_scale = 1.0 # type: float _scale: float
_mirrored = None # type: List[bool] _mirrored: List[bool]
identifier: Tuple identifier: Tuple
def __init__(self, def __init__(self,