elasticai.creator.function_utils#

Module Contents#

Classes#

FunctionDecorator

Apply a callback to functions and either their own or custom names.

RegisterDescriptor

Automatically connect the FunctionDecorator to a callback and make it look like a method.

KeyedFunctionDispatcher

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 the dispatch_key_fn and the argument passed to call to process the argument and return the result.

FunctionRegistry

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 as callback(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

__call__(arg: elasticai.creator.function_utils.FN | str, arg2: elasticai.creator.function_utils.FN | None = None, /) elasticai.creator.function_utils.Tout | Callable[[elasticai.creator.function_utils.FN], elasticai.creator.function_utils.Tout][source]#
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", where name is the name of the field assigned to this descriptor.

For an example see the KeyedFunctionDispatcher below.

__set_name__(instance, name)[source]#
__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 the dispatch_key_fn and the argument passed to call 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(…)’

__contains__(item: str | Callable[[elasticai.creator.function_utils.Tin], elasticai.creator.function_utils.Tout]) bool[source]#
can_dispatch(item: elasticai.creator.function_utils.Tin) bool[source]#
call(arg: elasticai.creator.function_utils.Tin) elasticai.creator.function_utils.Tout[source]#
__call__(arg: elasticai.creator.function_utils.Tin) elasticai.creator.function_utils.Tout[source]#
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