Add setter/getter for .pattern to catch wrong types

This commit is contained in:
Jan Petykiewicz 2020-05-11 18:52:17 -07:00
parent fa8fc52dd6
commit f8c49cdb5e
2 changed files with 38 additions and 11 deletions

View File

@ -3,7 +3,7 @@
instances of a Pattern in the same parent Pattern. instances of a Pattern in the same parent Pattern.
""" """
from typing import Union, List, Dict, Tuple from typing import Union, List, Dict, Tuple, Optional, Sequence, TYPE_CHECKING
import copy import copy
import numpy import numpy
@ -21,7 +21,7 @@ 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', __slots__ = ('_pattern',
'_offset', '_offset',
'_rotation', '_rotation',
'_dose', '_dose',
@ -34,7 +34,7 @@ class GridRepetition:
'identifier', 'identifier',
'locked') 'locked')
pattern: 'Pattern' _pattern: Optional['Pattern']
""" The `Pattern` being instanced """ """ The `Pattern` being instanced """
_offset: numpy.ndarray _offset: numpy.ndarray
@ -162,6 +162,18 @@ class GridRepetition:
new.locked = self.locked new.locked = self.locked
return new return new
# pattern property
@property
def pattern(self) -> Optional['Pattern']:
return self._pattern
@pattern.setter
def pattern(self, val: Optional['Pattern']):
from .pattern import Pattern
if val is not None and not isinstance(val, Pattern):
raise PatternError('Provided pattern {} is not a Pattern object or None!'.format(val))
self._pattern = val
# offset property # offset property
@property @property
def offset(self) -> numpy.ndarray: def offset(self) -> numpy.ndarray:

View File

@ -3,7 +3,7 @@
offset, rotation, scaling, and other such properties to the reference. offset, rotation, scaling, and other such properties to the reference.
""" """
from typing import Union, List, Dict, Tuple from typing import Union, List, Dict, Tuple, Optional, Sequence, TYPE_CHECKING
import copy import copy
import numpy import numpy
@ -19,9 +19,16 @@ 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', __slots__ = ('_pattern',
'identifier', 'locked') '_offset',
pattern: 'Pattern' or None '_rotation',
'_dose',
'_scale',
'_mirrored',
'identifier',
'locked')
_pattern: Optional['Pattern']
""" The `Pattern` being instanced """ """ The `Pattern` being instanced """
_offset: numpy.ndarray _offset: numpy.ndarray
@ -55,10 +62,6 @@ class SubPattern:
dose: float = 1.0, dose: float = 1.0,
scale: float = 1.0, scale: float = 1.0,
locked: bool = False): locked: bool = False):
if pattern is not None and not hasattr(pattern, 'lock'):
raise PatternError('Provided pattern has no "lock()" method.\n'
'Maybe it''s not a Pattern instance?')
self.unlock() self.unlock()
self.identifier = () self.identifier = ()
self.pattern = pattern self.pattern = pattern
@ -93,6 +96,18 @@ class SubPattern:
new.locked = self.locked new.locked = self.locked
return new return new
# pattern property
@property
def pattern(self) -> Optional['Pattern']:
return self._pattern
@pattern.setter
def pattern(self, val: Optional['Pattern']):
from .pattern import Pattern
if val is not None and not isinstance(val, Pattern):
raise PatternError('Provided pattern {} is not a Pattern object or None!'.format(val))
self._pattern = val
# offset property # offset property
@property @property
def offset(self) -> numpy.ndarray: def offset(self) -> numpy.ndarray: