Vector#
- class ubermagutil.typesystem.Vector(name=None, **kwargs)#
Descriptor allowing setting attributes only with vectors (
list
,tuple
, ornumpy.ndarray
), whose elements are ofnumbers.Real
type.- Parameters:
component_type (int or float type, optional) – Type of the vector components. It should be a subset of
numbers.Real
(int
,float
).size (int, optional) – Size (length, number of elements) of the vector.
positive (bool, optional) – If
positive=True
, values of all vector elements must be positive (>0).unsigned (bool, optional) – If
unsigned=True
, values of all vector components must be unsigned (>=0).otherwise (type) – This type would also be accepted if specified. It has priority over other descriptor specification.
- Raises:
TypeError – If the
type(value)
is notlist
,tuple
, ornumpy.ndarray
or if the type of vector components is neithernumbers.Real
norexpected_type
(if passed).ValueError – If vector component value is
value < 0
andunsigned=True
orvalue <= 0
andpositive=True
.
Example
1. Usage of
Vector
descriptor for defining a three-dimensional vector, whose components myattribute positive integer components.>>> import ubermagutil.typesystem as ts ... >>> @ts.typesystem(myattribute=ts.Vector(size=3, component_type=int, ... positive=True)) ... class DecoratedClass: ... def __init__(self, myattribute): ... self.myattribute = myattribute ... >>> dc = DecoratedClass(myattribute=(1, 2, 12)) >>> dc.myattribute (1, 2, 12) >>> dc.myattribute = (10, 11, 12) # valid set >>> dc.myattribute (10, 11, 12) >>> dc.myattribute = (11, 12) # invalid set Traceback (most recent call last): ... ValueError: ... >>> dc.myattribute = (0, 1, 2) # invalid set Traceback (most recent call last): ... ValueError: ... >>> dc.myattribute = (1, 3.14, 2) # invalid set Traceback (most recent call last): ... TypeError: ... >>> dc.myattribute # the value was not affected by invalid sets (10, 11, 12)
Note
This class was derived from
ubermagutil.typesystem.Descriptor
and inherits its functionality.See also
Methods
__delete__
Deleting the decorated class attribute is never allowed and
AttributeError
is raised.__dir__
Default dir() implementation.
__eq__
Return self==value.
__repr__
Return repr(self).
If
self.const=True
, changing the value of a decorated class attribute after the initial set is not allowed.- __set__(instance, value)#
If
self.const=True
, changing the value of a decorated class attribute after the initial set is not allowed.- Raises:
AttributeError – If changing the value of a decorated class attribute is attempted.
Example
Changing the value of a constant decorated class attribute.
>>> import ubermagutil.typesystem as ts ... >>> @ts.typesystem(myattribute=ts.Descriptor(const=True)) ... class DecoratedClass: ... def __init__(self, myattribute): ... self.myattribute = myattribute ... >>> dc = DecoratedClass(myattribute="John Doe") >>> dc.myattribute 'John Doe' >>> dc.myattribute = 'Jane Doe' Traceback (most recent call last): ... AttributeError: ...