From 4308bdeb5615ad14f5474b7f58bfb1fe62c42b52 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 9 Nov 2020 21:59:28 -0800 Subject: [PATCH] Add parent class MasqueError and use it for traits and all other exceptions --- masque/error.py | 16 +++++++++------- masque/traits/annotatable.py | 4 ++-- masque/traits/doseable.py | 4 ++-- masque/traits/mirrorable.py | 2 +- masque/traits/positionable.py | 4 ++-- masque/traits/repeatable.py | 4 ++-- masque/traits/rotatable.py | 4 ++-- masque/traits/scalable.py | 6 +++--- 8 files changed, 23 insertions(+), 21 deletions(-) diff --git a/masque/error.py b/masque/error.py index e109c20..690550a 100644 --- a/masque/error.py +++ b/masque/error.py @@ -1,13 +1,15 @@ -class PatternError(Exception): +class MasqueError(Exception): """ - Simple Exception for Pattern objects and their contents + Parent exception for all Masque-related Exceptions """ - def __init__(self, value): - self.value = value + pass - def __str__(self): - return repr(self.value) +class PatternError(MasqueError): + """ + Exception for Pattern objects and their contents + """ + pass class PatternLockedError(PatternError): """ @@ -17,7 +19,7 @@ class PatternLockedError(PatternError): PatternError.__init__(self, 'Tried to modify a locked Pattern, subpattern, or shape') -class LibraryError(Exception): +class LibraryError(MasqueError): """ Exception raised by Library classes """ diff --git a/masque/traits/annotatable.py b/masque/traits/annotatable.py index 9d49018..4c0fdaa 100644 --- a/masque/traits/annotatable.py +++ b/masque/traits/annotatable.py @@ -3,7 +3,7 @@ from typing import TypeVar from abc import ABCMeta, abstractmethod from ..utils import annotations_t -from ..error import PatternError +from ..error import MasqueError T = TypeVar('T', bound='Annotatable') @@ -51,5 +51,5 @@ class AnnotatableImpl(Annotatable, metaclass=ABCMeta): @annotations.setter def annotations(self, annotations: annotations_t): if not isinstance(annotations, dict): - raise PatternError(f'annotations expected dict, got {type(annotations)}') + raise MasqueError(f'annotations expected dict, got {type(annotations)}') self._annotations = annotations diff --git a/masque/traits/doseable.py b/masque/traits/doseable.py index 217872c..8fd770a 100644 --- a/masque/traits/doseable.py +++ b/masque/traits/doseable.py @@ -1,7 +1,7 @@ from typing import TypeVar from abc import ABCMeta, abstractmethod -from ..error import PatternError +from ..error import MasqueError T = TypeVar('T', bound='Doseable') @@ -65,7 +65,7 @@ class DoseableImpl(Doseable, metaclass=ABCMeta): @dose.setter def dose(self, val: float): if not val >= 0: - raise PatternError('Dose must be non-negative') + raise MasqueError('Dose must be non-negative') self._dose = val ''' diff --git a/masque/traits/mirrorable.py b/masque/traits/mirrorable.py index 1ec54f6..8fb5d4b 100644 --- a/masque/traits/mirrorable.py +++ b/masque/traits/mirrorable.py @@ -50,7 +50,7 @@ class Mirrorable(metaclass=ABCMeta): # @mirrored.setter # def mirrored(self, val: Sequence[bool]): # if is_scalar(val): -# raise PatternError('Mirrored must be a 2-element list of booleans') +# raise MasqueError('Mirrored must be a 2-element list of booleans') # self._mirrored = numpy.array(val, dtype=bool, copy=True) # # ''' diff --git a/masque/traits/positionable.py b/masque/traits/positionable.py index 71f90ec..4e59e32 100644 --- a/masque/traits/positionable.py +++ b/masque/traits/positionable.py @@ -4,7 +4,7 @@ from typing import TypeVar from abc import ABCMeta, abstractmethod import numpy # type: ignore -from ..error import PatternError +from ..error import MasqueError from ..utils import vector2 @@ -97,7 +97,7 @@ class PositionableImpl(Positionable, metaclass=ABCMeta): val = numpy.array(val, dtype=float) if val.size != 2: - raise PatternError('Offset must be convertible to size-2 ndarray') + raise MasqueError('Offset must be convertible to size-2 ndarray') self._offset = val.flatten() ''' diff --git a/masque/traits/repeatable.py b/masque/traits/repeatable.py index 4a7f391..9f032b7 100644 --- a/masque/traits/repeatable.py +++ b/masque/traits/repeatable.py @@ -1,7 +1,7 @@ from typing import TypeVar, Optional, TYPE_CHECKING from abc import ABCMeta, abstractmethod -from ..error import PatternError +from ..error import MasqueError if TYPE_CHECKING: @@ -71,7 +71,7 @@ class RepeatableImpl(Repeatable, metaclass=ABCMeta): def repetition(self, repetition: Optional['Repetition']): from ..repetition import Repetition if repetition is not None and not isinstance(repetition, Repetition): - raise PatternError(f'{repetition} is not a valid Repetition object!') + raise MasqueError(f'{repetition} is not a valid Repetition object!') self._repetition = repetition ''' diff --git a/masque/traits/rotatable.py b/masque/traits/rotatable.py index c0641f0..67a9371 100644 --- a/masque/traits/rotatable.py +++ b/masque/traits/rotatable.py @@ -5,7 +5,7 @@ import numpy # type: ignore from numpy import pi #from .positionable import Positionable -from ..error import PatternError +from ..error import MasqueError from ..utils import is_scalar, rotation_matrix_2d, vector2 T = TypeVar('T', bound='Rotatable') @@ -57,7 +57,7 @@ class RotatableImpl(Rotatable, metaclass=ABCMeta): @rotation.setter def rotation(self, val: float): if not is_scalar(val): - raise PatternError('Rotation must be a scalar') + raise MasqueError('Rotation must be a scalar') self._rotation = val % (2 * pi) ''' diff --git a/masque/traits/scalable.py b/masque/traits/scalable.py index b31c2f9..ec87c69 100644 --- a/masque/traits/scalable.py +++ b/masque/traits/scalable.py @@ -1,7 +1,7 @@ from typing import TypeVar from abc import ABCMeta, abstractmethod -from ..error import PatternError +from ..error import MasqueError from ..utils import is_scalar @@ -51,9 +51,9 @@ class ScalableImpl(Scalable, metaclass=ABCMeta): @scale.setter def scale(self, val: float): if not is_scalar(val): - raise PatternError('Scale must be a scalar') + raise MasqueError('Scale must be a scalar') if not val > 0: - raise PatternError('Scale must be positive') + raise MasqueError('Scale must be positive') self._scale = val '''