_mixin.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Mixin classes for sharing functionality between unrelated classes.
  2. This module is named with a leading underscore to signify to users that it's
  3. "private" and only intended for internal use by the biomechanics module.
  4. """
  5. __all__ = ['_NamedMixin']
  6. class _NamedMixin:
  7. """Mixin class for adding `name` properties.
  8. Valid names, as will typically be used by subclasses as a suffix when
  9. naming automatically-instantiated symbol attributes, must be nonzero length
  10. strings.
  11. Attributes
  12. ==========
  13. name : str
  14. The name identifier associated with the instance. Must be a string of
  15. length at least 1.
  16. """
  17. @property
  18. def name(self) -> str:
  19. """The name associated with the class instance."""
  20. return self._name
  21. @name.setter
  22. def name(self, name: str) -> None:
  23. if hasattr(self, '_name'):
  24. msg = (
  25. f'Can\'t set attribute `name` to {repr(name)} as it is '
  26. f'immutable.'
  27. )
  28. raise AttributeError(msg)
  29. if not isinstance(name, str):
  30. msg = (
  31. f'Name {repr(name)} passed to `name` was of type '
  32. f'{type(name)}, must be {str}.'
  33. )
  34. raise TypeError(msg)
  35. if name in {''}:
  36. msg = (
  37. f'Name {repr(name)} is invalid, must be a nonzero length '
  38. f'{type(str)}.'
  39. )
  40. raise ValueError(msg)
  41. self._name = name