Subset#
- class ubermagutil.typesystem.Subset(name=None, **kwargs)#
Descriptor allowing setting attributes only with a subset of a predefined set.
- Parameters:
sample_set (any type) – Defines the set of allowed values.
unpack (bool) – If
True
,value
is unpacked asset(value)
.
- Raises:
ValueError – If value is not a subset
sample_set
.
Example
Usage of
Subset
descriptor.
>>> import ubermagutil.typesystem as ts ... >>> @ts.typesystem(myattribute=ts.Subset(sample_set='xyz', unpack=True)) ... class DecoratedClass: ... def __init__(self, myattribute): ... self.myattribute = myattribute ... >>> dc = DecoratedClass(myattribute='yx') >>> dc.myattribute = 'zyyyyx' # valid set >>> dc.myattribute = 'a' # invalid set Traceback (most recent call last): ... ValueError: ...
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: ...