Parameter#
- class ubermagutil.typesystem.Parameter(name=None, **kwargs)#
Descriptor allowing setting attributes with a value described as
descriptor
or a dictionary. If a dictionary is passed, dictionary keys are strings defined byubermagutil.typesystem.Name
descriptor, and the values are defined bydescriptor
.- Parameters:
descriptor (ubermagutil.typesystem.Descriptor or its derived class) – Accepted value, or if a dictionary is passed, allowed value type.
otherwise (type) – This type would also be accepted if specified. It has priority over other descriptor specification.
Example
The usage of
Property
descriptor allowing scalars.
>>> import ubermagutil.typesystem as ts ... >>> @ts.typesystem(myattribute=ts.Parameter(descriptor=ts.Scalar())) ... class DecoratedClass: ... def __init__(self, myattribute): ... self.myattribute = myattribute ... >>> dc = DecoratedClass(myattribute=-2) >>> dc.myattribute -2 >>> dc.myattribute = {'a': 1, 'b': -3} # valid set >>> dc.myattribute {'a': 1, 'b': -3} >>> dc.myattribute = {'a': 1, 'b': 'abc'} # invalid set Traceback (most recent call last): ... TypeError: ... >>> dc.myattribute = {'a b': 1, 'c': -3} # invalid set Traceback (most recent call last): ... ValueError: ... >>> dc.myattribute = {} # invalid set Traceback (most recent call last): ... ValueError: ... >>> dc.myattribute # the value was not affected by invalid sets {'a': 1, 'b': -3}
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: ...