Use __slots__ for class members

Also use the other sort of type hints for instance variables
lethe/HEAD
Jan Petykiewicz 5 years ago
parent 0b962999b2
commit 38f64f7c62

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

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

@ -23,19 +23,30 @@ class GridRepetition:
GridRepetition provides support for efficiently embedding multiple copies of a Pattern
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
_rotation = 0.0 # type: float
_dose = 1.0 # type: float
_scale = 1.0 # type: float
_mirrored = None # type: List[bool]
_offset: numpy.ndarray
_rotation: float
_dose: float
_scale: float
_mirrored: List[bool]
_a_vector = None # type: numpy.ndarray
_b_vector = None # type: numpy.ndarray or None
a_count = None # type: int
b_count = 1 # type: int
_a_vector: numpy.ndarray
_b_vector: numpy.ndarray or None
a_count: int
b_count: int
identifier: Tuple

@ -20,15 +20,14 @@ class Arc(Shape):
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.
"""
_radii = None # type: numpy.ndarray
_angles = None # type: numpy.ndarray
_width = 1.0 # type: float
_rotation = 0.0 # type: float
# Defaults for to_polygons
poly_num_points = DEFAULT_POLY_NUM_POINTS # type: int
poly_max_arclen = None # type: float
__slots__ = ('_radii', '_angles', '_width', '_rotation',
'poly_num_points', 'poly_max_arclen')
_radii: numpy.ndarray
_angles: numpy.ndarray
_width: float
_rotation: float
poly_num_points: int
poly_max_arclen: float
# radius properties
@property
@ -148,6 +147,7 @@ class Arc(Shape):
mirrored: Tuple[bool] = (False, False),
layer: int=0,
dose: float=1.0):
self.identifier = ()
self.radii = radii
self.angles = angles
self.width = width

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

@ -16,13 +16,12 @@ class Ellipse(Shape):
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.
"""
_radii = None # type: numpy.ndarray
_rotation = 0.0 # type: float
# Defaults for to_polygons
poly_num_points = DEFAULT_POLY_NUM_POINTS # type: int
poly_max_arclen = None # type: float
__slots__ = ('_radii', '_rotation',
'poly_num_points', 'poly_max_arclen')
_radii: numpy.ndarray
_rotation: float
poly_num_points: int
poly_max_arclen: float
# radius properties
@property
@ -89,6 +88,7 @@ class Ellipse(Shape):
mirrored: Tuple[bool] = (False, False),
layer: int=0,
dose: float=1.0):
self.identifier = ()
self.radii = radii
self.offset = offset
self.rotation = rotation

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

@ -18,7 +18,8 @@ class Polygon(Shape):
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
@property
@ -77,6 +78,7 @@ class Polygon(Shape):
layer: int=0,
dose: float=1.0,
):
self.identifier = ()
self.layer = layer
self.dose = dose
self.vertices = vertices

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

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

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

Loading…
Cancel
Save