exatomic.exa.typed module

Strongly Typed Class Attributes

This module provides a typed attribute class and class decorator/base class for creating classes with type enforced attributes. Enforcing an attribute’s type is handled by Python’s property mechanism (the property’s set function checks the value’s type). A simple usage example follows.

@typed
class Foo(object):
    bar = Typed(int, doc="Always an integer type")

The simple code example generates code similar to the following when the module is executed (i.e. imported).

class Foo(object):
    @property
    def bar(self):
        return self._bar

    @bar.setter
    def bar(self, value):
        if not isinstance(value, int):
            try:
                value = int(value)
            except Exception as e:
                raise TypeError("Cannot convert value") from e
        self._bar = value

The Typed object additionally provides mechanisms for triggering function calls before and after get, set, and delete, and attempts automatic conversion (as shown above) for all types supported for a given attribute.

exatomic.exa.typed.typed(cls)[source]

Class decorator that updates a class definition with strongly typed property attributes.

See also

If the class will be inherited, use TypedClass.

exatomic.exa.typed.yield_typed(obj_or_cls)[source]

Generator that yields typed object names of the class (or object’s class).

Parameters:

obj_or_cls (object) – Class object or instance of class

Returns:

name (array) – Names of class attributes that are strongly typed

class exatomic.exa.typed.Typed(types, doc=None, autoconv=True, pre_set=None, allow_none=True, post_set=None, pre_get=None, pre_del=None, post_del=None, verbose=False)[source]

Bases: object

A representation of a strongly typed class attribute.

@typed
class Strong(object):
    foo = Typed(int, doc="my int")

The above example creates a class object that has a property-like attribute which requires its value to be of type int. Additional arguments provide the ability to have the property’s getter, setter, and deleter functions call other functions or methods of the class. If provided by the class, strongly typed attributes created by here automatically attempt to to set themselves (see below).

@typed
class Strong(object):
    _setters = ("_set", )
    foo = Typed(int, doc="my int")

    def _set_foo(self):
        self.foo = 42

By defining a _getters class attribute the strongly typed property knows that, if the foo attribute’s value (i.e. _foo) is not defined (or is defined as None), that the property getter should first call the _set_foo class method, and after it should proceed with getting the property value. Note that _set_foo cannot accept arguments (it must be ‘automatic’).

Parameters:
  • types (iterable, type) – Iterable of types or type

  • doc (str) – Documentation

  • autoconv (bool) – Attempt automatic type conversion when setting (default true)

  • allow_none (bool) – As an additional type, allow None (default true)

  • pre_set (callable, str) – Callable or class method name called before setter

  • post_set (callable, str) – Callabel or class method name called after setter

  • pre_get (callable, str) – Callable or class method name called before getter

  • pre_del (callable, str) – Callable or class method name called before setter

  • post_del (callable, str) – Callabel or class method name called after setter

Warning

Automatic type conversion (autoconv = true) is not guaranteed to work in all cases and is known to fail for non-Python objects such as numpy ndarray types: Setting autoconv to false is recommened for these cases.

Typed(np.ndarray, autoconv=False)    # Do not attempt auto conversion
class exatomic.exa.typed.TypedMeta(name, bases, namespace)[source]

Bases: type

A metaclass for creating typed attributes which can be used instead of the class decorator.

class Foo(metaclass=TypedMeta):
    bar = Typed(int, doc="Always an int")

See also

typed() and data

class exatomic.exa.typed.TypedClass[source]

Bases: object

A mixin class which can be used to create a class with strongly typed attributes.

class Foo(TypedClass):
    bar = Typed(int, doc="Still an int")

See also

typed()