adorn.unit.anum module

Representation of enum.Enum

class adorn.unit.anum.Anum

Bases: adorn.unit.unit.Unit

A wrapper around Enum

The members of the enumeration are added to _registry and are accessible by specifying the enumeration they belong to and the str representation of their name

from collections import defaultdict
from enum import auto, Enum

from adorn.orchestrator.base import Base
from adorn.unit.anum import Anum

class Example(Anum):
    _registry = defaultdict(dict)

@Example.register()
class AB(Enum):
    a = auto
    b = auto

base = Base([Example()])
assert base.from_obj(AB, "a") == AB.a
contains(obj, orchestrator)

Check if obj is registered to Anum

Parameters
  • obj (Any) – the Type that may be a subclass of Anum

  • orchestrator (Orchestrator) – container of all types, not used

Returns

if True then obj is represented by Anum,

otherwise False

Return type

bool

from_obj(target_cls, orchestrator, obj)

Convert obj to an instance of the type target_cls

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

  • orchestrator (Orchestrator) – container of all types, not used

  • obj (Any) – an instance, that will be converted to an instance of target_cls

Returns

a member of target_cls

Return type

_E

get(key, orchestrator)

Return the relevant Anum for the key

Parameters
  • key (Any) – the Type that may be a subclass of the Unit

  • orchestrator (Orchestrator) – container of all types, not used

Returns

if key is an Anum then return it,

otherwise None

Return type

Optional[type]

classmethod register()

Add Enum and it’s members to the _registry

Returns

funcion that adds the decorated

Enum and its members to the _registry

Return type

Callable[[Type[_E]], Type[_E]]

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, not used

  • obj (Any) – an instance, 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]