From f8c49cdb5e4bc4472cfa78d26ef1f77172ae0376 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 11 May 2020 18:52:17 -0700 Subject: [PATCH] Add setter/getter for .pattern to catch wrong types --- masque/repetition.py | 18 +++++++++++++++--- masque/subpattern.py | 31 +++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/masque/repetition.py b/masque/repetition.py index 47c4370..5b0c23c 100644 --- a/masque/repetition.py +++ b/masque/repetition.py @@ -3,7 +3,7 @@ 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 numpy @@ -21,7 +21,7 @@ class GridRepetition: GridRepetition provides support for efficiently embedding multiple copies of a `Pattern` into another `Pattern` at regularly-spaced offsets. """ - __slots__ = ('pattern', + __slots__ = ('_pattern', '_offset', '_rotation', '_dose', @@ -34,7 +34,7 @@ class GridRepetition: 'identifier', 'locked') - pattern: 'Pattern' + _pattern: Optional['Pattern'] """ The `Pattern` being instanced """ _offset: numpy.ndarray @@ -162,6 +162,18 @@ class GridRepetition: new.locked = self.locked 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 @property def offset(self) -> numpy.ndarray: diff --git a/masque/subpattern.py b/masque/subpattern.py index 1bb926c..2577ce9 100644 --- a/masque/subpattern.py +++ b/masque/subpattern.py @@ -3,7 +3,7 @@ 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 numpy @@ -19,9 +19,16 @@ class SubPattern: SubPattern provides basic support for nesting Pattern objects within each other, by adding offset, rotation, scaling, and associated methods. """ - __slots__ = ('pattern', '_offset', '_rotation', '_dose', '_scale', '_mirrored', - 'identifier', 'locked') - pattern: 'Pattern' or None + __slots__ = ('_pattern', + '_offset', + '_rotation', + '_dose', + '_scale', + '_mirrored', + 'identifier', + 'locked') + + _pattern: Optional['Pattern'] """ The `Pattern` being instanced """ _offset: numpy.ndarray @@ -55,10 +62,6 @@ class SubPattern: dose: float = 1.0, scale: float = 1.0, 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.identifier = () self.pattern = pattern @@ -93,6 +96,18 @@ class SubPattern: new.locked = self.locked 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 @property def offset(self) -> numpy.ndarray: