adorn.unit.complex module

Container of types with a class hierarchy.

class adorn.unit.complex.Complex

Bases: adorn.unit.unit.Unit

Unit that preserves class hierarchy

It is recommended you use this construct for your custom package.

classmethod by_name(name=None)

Returns a callable function that constructs an argument of the class.

Parameters

name (Optional[str]) – name of the class you want the constructor for, if None, name() will be used

Returns

constructor for the requested class

Return type

Callable[…, _ComplexT]

constructor_name: Optional[str] = None

Name of the constructor to use to instantiate the class

constructor_type: Optional[str] = None

The name of the ConstructorValue you want to instantiate the arguments for your class

contains(obj, orchestrator)

Check if the obj is registered

Parameters
  • obj (Any) – potentially a type that may be registered

  • orchestrator (Orchestrator) – container of all types, which is used for nested types

Returns

if True, the type is registered

Return type

bool

from_obj(target_cls, orchestrator, obj)

Convert obj to an instance of target_cls

Parameters
  • target_cls (Type) – the target Type, the given obj will be converted into

  • orchestrator (Orchestrator) – container of all types, typically used to recurse down nested types

  • obj (Any) – an instance, usually of type Params, that will be converted into an instance of target_cls

Returns

an instance of target_cls derived from obj

Return type

Any

Raises
general_check(cls, orchestrator, obj, is_from_obj=False)

Checks that need to be performed for both type_check and from_obj

Exceptions:
Parameters
  • cls (Type) – the target class that obj is trying to be converted into

  • orchestrator (Orchestrator) – container of all types, typically used to recurse down nested types

  • obj (Any) – the instance that will have the sanity checks applied to it

  • is_from_obj (bool) – if True this check is being done for from_obj(), which allows instances of cls to be considered valid

Returns

if TypeCheckError then there was

an issue which would prevent converting obj to an instance of target_cls, otherwise None

Return type

Optional[TypeCheckError]

get(key, orchestrator)

Return the relevant registered type

Parameters
  • key (Any) – the type that may be a registered value

  • orchestrator (Orchestrator) – container of all types, typically used to recurse nested types

Returns

if key is a registered value,

then the registered value is returned, otherwise None

Return type

Optional[type]

classmethod get_direct_parent()

Get the parent(s) of a class

Returns

if None, then the class

has no parents, if _ComplexT then the class has a parent, if List[_ComplexT] then the class has multiple parents

Return type

Optional[Union[_ComplexT, List[_ComplexT]]]

classmethod get_instantiate_children()

Get the subclasses for a class that can be instantiated

Returns

All the subclasses of a class that can be instantiated

Return type

Dict[str, _ComplexT]

classmethod get_intermediate_children()

Get the children of a class, that are also parents

Note

Some of the classes in the list may not be able to be instantiated

Returns

list of subclasses of that are also parents

Return type

List[_ComplexT]

classmethod list_available()

List all the names of subclasses that can be instantiated

Returns

names of subclasses that can be instantiated

Return type

List[str]

match(key)

Check if the key is a subclass of Complex

Parameters

key (Any) – potential type to be checked

Returns

if True then key is a Type of the appropriate type

Return type

bool

classmethod name()

Get the string associated with a given class

Returns

the string associated with a class

Return type

str

Raises

Exception – the given class, cannot be instantiated and therefore does not have a name

parameter_order: Optional[List[str]] = None

The order in which the parameters for a class should be instantiated

classmethod register(name)

Decorator that adds a class to the class hierarchy

Parameters

name (Optional[str]) – The str that will map to the given type, if None, then the class is both a child of another Complex type and a parent of other Complex type(s), but cannot be instantiated

Returns

funcion that adds the decorated type

to the _registry with the name being the key and the decorated class being the value

Return type

Callable[[Type[_T]], Type[_T]]

classmethod resolve_class_name(name=None)

Returns the class that corresponds to the given name

Parameters

name (str) – name of the class the caller wants

Returns

the type associated with name specified

Return type

Type[_ComplexT]

classmethod root()

Initialize all the class attributes needed to track the class hieracrchy

This decorator is typically called on a direct descendant of Complex enabling all of the direct descendant’s children to be tracked.

Returns

decorator that initializes the

class attributes for tracking

Return type

Callable[[Type[_T]], Type[_T]]

type_check(target_cls, orchestrator, obj)

Ensure obj can be converted to the type target_cls

Exceptions:
Parameters
  • target_cls (Type) – the target Type, the given obj will be checked against

  • orchestrator (Orchestrator) – container of all types, typically used to recurse down nested types

  • obj (Any) – an instance, usually of type Params, that will be checked to see if it can be converted to an instance of target_cls

Returns

if TypeCheckError then there was

an issue which would prevent converting obj to an instance of target_cls, otherwise None

Return type

Optional[TypeCheckError]