Name#

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

Python identifier descriptor.

It allows setting attributes only with strings representing a valid Python identifier which is not also a keyword. In other words, it allows valid Python variable names. If allowed_char is passed, value is first split at that character and then individual parts of the string checked.

Parameters:

allowed_char ((1,) str) – Character allowed in value.

Raises:
  • TypeError – If the type(value) is not str.

  • ValueError – If the string is not a valid identifier or it is a Python keyword.

Example

  1. Usage of Name descriptor.

>>> import ubermagutil.typesystem as ts
...
>>> @ts.typesystem(myattribute=ts.Name())
... class DecoratedClass:
...     def __init__(self, myattribute):
...         self.myattribute = myattribute
...
>>> dc = DecoratedClass(myattribute='object_name')
>>> dc.myattribute
'object_name'
>>> dc.myattribute = 'newname'  # valid set
>>> dc.myattribute
'newname'
>>> dc.myattribute = '123newname'  # invalid set
Traceback (most recent call last):
    ...
ValueError: ...
>>> dc.myattribute = 'Nikola Tesla'  # invalid set
Traceback (most recent call last):
    ...
ValueError: ...
>>> dc.myattribute  # the value was not affected by invalid sets
'newname'

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