leaspy.utils.functional

Attributes

Classes

NamedInputFunction

Bridge from a function with positional parameters to a function with keyword-only parameters.

Functions

get_named_parameters(f)

Get the names of parameters of the input function f, which should be

Package Contents

Exp
Identity
MatMul
Mean
OrthoBasis
Prod
Sqr
Std
Sum
SumDim
class NamedInputFunction[source]

Bases: Generic[RT]

Bridge from a function with positional parameters to a function with keyword-only parameters.

Attributes:
fCallable

The original function. The named parameters to be sent in f should be: positional, positional-or-keyword, or variadic arguments. It can also have some keyword-only arguments, but they should be fixed once for all with attribute kws.

parameterstuple [str, …]

Assigned names, in order, for positional parameters of f.

kwsNone (default) or KwargsType

Some optional fixed keyword parameters to pass upon function calls.

Notes

We do not implement the mapping of keyword-only to renamed keyword-only parameters for now since it is not needed and would make the logic more complex. Particularly due to the existence of positional-only parameters.

f: Callable[Ellipsis, RT]
parameters: Tuple[str, Ellipsis]
kws: KwargsType | None = None
call(named_params)[source]

Call the underlying function with the correct positional arguments, retrieved by parameter names in input variables.

Parameters:
named_paramsTMapping [str, Any]

A mapping of parameter names to their values.

Returns:
RT

The result of calling the function f with the provided named parameters.

Parameters:

named_params (Mapping[str, Any])

Return type:

RT

then(g, **g_kws)[source]

Return a new NamedInputFunction applying (g o f) function.

Parameters:
gCallable [RT, S]

A function to apply after the original function f.

Parameters:

g (Callable[[RT], S])

static bound_to(f, check_arguments=None)[source]

Return a new factory to create new NamedInputFunction instances that are bound to the provided function.

Parameters:
fCallable [RT]

The function to which the new NamedInputFunction will be bound.

check_argumentsCallable [tuple [str, …], KwargsType], optional

An optional function to check the provided parameters and keyword arguments before creating the NamedInputFunction.

Parameters:
  • f (Callable[Ellipsis, RT])

  • check_arguments (Optional[Callable[[Tuple[str, Ellipsis], KwargsType], None]])

get_named_parameters(f)[source]

Get the names of parameters of the input function f, which should be a NamedInputFunction or a function with keyword-only parameters.

Parameters:
fCallable

The function from which to extract parameter names.

Returns:
tuple [str, …]

A tuple containing the names of the parameters of the function f.

Raises:
ValueError

If the function f has positional parameters or if it has keyword-only parameters that are not allowed by the NamedInputFunction interface.

Parameters:

f (Callable)

Return type:

Tuple[str, Ellipsis]