elasticai.creator.function_utils
#
Module Contents#
Classes#
Apply a callback to functions and either their own or custom names. |
|
Automatically connect the |
|
Functions can be registered with custom key or by
using the function’s name as key.
Upon |
|
DEPRECATED: use the MultiArgDispatcher instead! |
Data#
API#
- elasticai.creator.function_utils.Tin#
‘TypeVar(…)’
- elasticai.creator.function_utils.Tout#
‘TypeVar(…)’
- elasticai.creator.function_utils.FN#
‘TypeVar(…)’
- class elasticai.creator.function_utils.FunctionDecorator(callback: Callable[[str, elasticai.creator.function_utils.FN], elasticai.creator.function_utils.Tout])[source]#
Bases:
typing.Generic
[elasticai.creator.function_utils.FN
,elasticai.creator.function_utils.Tout
]Apply a callback to functions and either their own or custom names.
- Param:
callback
: will be called ascallback(name, fn)
Important
if you want to use this as a decorator, do not forget to return the wrapped function from your callback.
Examples:
>>> registry = dict() >>> def register_fn(name, fn): ... registry[name] = fn ... return fn ... >>> register = FunctionDecorator(register_fn) >>> def my_fn(x): ... print(x) ... >>> register(my_fn) >>> registry["my_fn"]("hello, world!") "hello, world!"
another example could look like this:
registry = dict() def register_fn(name, fn): registry[name] = fn return fn register = FunctionDecorator(register_fn) @register("other_name") @register def my_fn(x): print(x)
This will add
my_fn
to the registry using the name"my_fn"
and the name"other_name"
.Initialization
- class elasticai.creator.function_utils.RegisterDescriptor[source]#
Bases:
typing.Generic
[elasticai.creator.function_utils.Tin
,elasticai.creator.function_utils.Tout
]Automatically connect the
FunctionDecorator
to a callback and make it look like a method.The owning instance needs to define a callback that has the name
f"_{name}_callback"
, wherename
is the name of the field assigned to this descriptor.For an example see the
KeyedFunctionDispatcher
below.- __get__(instance, owner=None) elasticai.creator.function_utils.FunctionDecorator[Callable[[elasticai.creator.function_utils.Tin], elasticai.creator.function_utils.Tout], Callable[[elasticai.creator.function_utils.Tin], elasticai.creator.function_utils.Tout]] [source]#
- class elasticai.creator.function_utils.KeyedFunctionDispatcher(dispatch_key_fn: Callable[[elasticai.creator.function_utils.Tin], str])[source]#
Bases:
typing.Generic
[elasticai.creator.function_utils.Tin
,elasticai.creator.function_utils.Tout
]Functions can be registered with custom key or by using the function’s name as key. Upon
call
the registry will use the function specified by thedispatch_key_fn
and the argument passed tocall
to process the argument and return the result.Initialization
- register: elasticai.creator.function_utils.RegisterDescriptor[elasticai.creator.function_utils.Tin, elasticai.creator.function_utils.Tout]#
‘RegisterDescriptor(…)’
- class elasticai.creator.function_utils.FunctionRegistry(dispatch_key_fn: Callable[[elasticai.creator.function_utils.Tin], str])[source]#
Bases:
elasticai.creator.function_utils.KeyedFunctionDispatcher
DEPRECATED: use the MultiArgDispatcher instead!
Initialization