adorn.unit.anum module
Representation of enum.Enum
- class adorn.unit.anum.Anum
Bases:
adorn.unit.unit.UnitA wrapper around
EnumThe members of the enumeration are added to
_registryand are accessible by specifying the enumeration they belong to and thestrrepresentation of their namefrom 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
Anumorchestrator (Orchestrator) – container of all types, not used
- Returns
- if
Truethenobjis represented byAnum, otherwise
False
- if
- 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
objwill be converted intoorchestrator (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
Anumfor the key- Parameters
key (Any) – the Type that may be a subclass of the
Unitorchestrator (Orchestrator) – container of all types, not used
- Returns
- if
keyis anAnumthen return it, otherwise
None
- if
- Return type
Optional[type]
- classmethod register()
Add
Enumand it’s members to the_registry- Returns
- funcion that adds the decorated
Enumand 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:
UnRepresentedTypeError:objdid not map to any of the types contained in the_registry
AnumWrongTypeError:objwas not of typestr
AnumMemberError:objspecified a value that did not map to one of the members oftarget_cls
- Parameters
target_cls (Type) – the target Type, the given
objwill be checked againstorchestrator (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
TypeCheckErrorthen there was an issue which would prevent converting
objto an instance oftarget_cls, otherwiseNone
- if
- Return type
Optional[TypeCheckError]