Descriptor#

class ubermagutil.typesystem.Descriptor(name=None, **kwargs)#

Descriptor base class from which all descriptors in ubermagutil.typesystem are derived.

Before setting the attribute value of a decorated class is allowed, certain type and value checks are performed. If they are not according to the specifications in the __set__ method (defined in the derived class), TypeError or ValueError is raised. If const=True is passed when the class is instantiated, no value changes are allowed after the initial assignment. Deleting attributes of a decorated class is never allowed.

Parameters:
  • name (str) – Attribute name. It must be a valid Python variable name. Defaults to None.

  • const (bool, optional) – If const=True, the attribute of the decorated class is constant and its value cannot be changed after the first set.

Example

1. Deriving a descriptor class from the base class Descriptor, which only allows positive integer values.

>>> import ubermagutil.typesystem as ts
...
>>> class PositiveInt(ts.Descriptor):
...     def __set__(self, instance, value):
...        if not isinstance(value, int):
...            raise TypeError('Allowed only type(value) == int.')
...        if value < 0:
...            raise ValueError('Allowed only value >= 0.')
...        super().__set__(instance, value)
...
>>> @ts.typesystem(myattribute=PositiveInt())
... class DecoratedClass:
...     def __init__(self, myattribute):
...         self.myattribute = myattribute
...
>>> dc = DecoratedClass(myattribute=5)
>>> dc.myattribute
5
>>> dc.myattribute = 101  # valid set
>>> dc.myattribute
101
>>> dc.myattribute = -1  # invalid set - negative value
Traceback (most recent call last):
   ...
ValueError: ...
>>> dc.myattribute = 3.14  # invalid set - float value
Traceback (most recent call last):
   ...
TypeError: ...
>>> dc.myattribute  # value has not beed affected by invalid sets
101

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).

__set__

If self.const=True, changing the value of a decorated class attribute after the initial set is not allowed.


__delete__(instance)#

Deleting the decorated class attribute is never allowed and AttributeError is raised.

Raises:

AttributeError – If deleting decorated class attribute is attempted.

Example

  1. Deleting an attribute of a decorated class.

>>> import ubermagutil.typesystem as ts
...
>>> @ts.typesystem(myattribute=ts.Descriptor())
... class DecoratedClass:
...     def __init__(self, myattribute):
...         self.myattribute = myattribute
...
>>> dc = DecoratedClass(myattribute="Nikola Tesla")
>>> dc.myattribute
'Nikola Tesla'
>>> del dc.myattribute
Traceback (most recent call last):
   ...
AttributeError: ...
__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

  1. 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: ...