Dictionary#

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

Descriptor allowing setting attributes with a dictionary, which has keys defined by key_descriptor and values defined by value_descriptor.

Parameters:
  • key_descriptor (ubermagutil.typesystem.Descriptor or its derived class) – Accepted dictionary key type.

  • value_descriptor (ubermagutil.typesystem.Descriptor or its derived class) – Accepted dictionary value type.

  • allow_empty (bool, optional) – If allow_empty=True, the value can be an empty dictionary.

  • otherwise (type) – This type would also be accepted if specified. It has priority over other descriptor specification.

Raises:
  • TypeError – If value passed is not a dictionary.

  • ValueError – If an empty dictionary is passed or a dictionary with invalid keys or values.

Example

1. The usage of Dictionary descriptor allowing keys defined by ubermagutil.typesystem.Name and values by ubermagutil.typesystem.Scalar.

>>> import ubermagutil.typesystem as ts
...
>>> @ts.typesystem(myattribute=ts.Dictionary(key_descriptor=ts.Name(),
...                                          value_descriptor=ts.Scalar()))
... class DecoratedClass:
...     def __init__(self, myattribute):
...         self.myattribute = myattribute
...
>>> dc = DecoratedClass(myattribute={'a': 1, 'b': -1.1})
>>> dc.myattribute
{'a': 1, 'b': -1.1}
>>> dc.myattribute = {'a': 1, 'b': -3}  # valid set
>>> dc.myattribute
{'a': 1, 'b': -3}
>>> dc.myattribute = {1: 1, 'b': 3}  # invalid set
Traceback (most recent call last):
    ...
TypeError: ...
>>> dc.myattribute = {'a': 1, 'c': 'd'}  # invalid set
Traceback (most recent call last):
    ...
TypeError: ...
>>> 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

Descriptor


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.


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