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
commit 38f64f7c62
11 changed files with 89 additions and 72 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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