Skip to content

qref.functools

Tools for constructing functions operating on Qref objects.

ensure_routine

ensure_routine(data: AnyQrefType) -> RoutineV1

Ensure that given objects is of RoutineV1 type.

This functions may serve for constructing functions accepting either RoutineV1 oor SchemaV1 objects, as well as dictionaries that represent them.

Parameters:

Name Type Description Default
data AnyQrefType

the objects that has to be converted (if neccessary) to RoutineV1. Can either be

required

Returns:

Type Description
RoutineV1

An object of type RoutineV1 corresponding to the provided data.

Source code in src/qref/functools.py
@singledispatch
def ensure_routine(data: AnyQrefType) -> RoutineV1:
    """Ensure that given objects is of RoutineV1 type.

    This functions may serve for constructing functions accepting either RoutineV1 oor SchemaV1
    objects, as well as dictionaries that represent them.

    Args:
        data: the objects that has to be converted (if neccessary) to RoutineV1. Can either be
        an instance of SchemaV1, in which case its `program` attribute will be returned,
        an instance of RoutineV1, in which case the object will be returned without changes,
        or a dictionary, in which case it will serve to constructe RoutineV1, or SchemaV1.

    Returns:
        An object of type RoutineV1 corresponding to the provided data.
    """
    raise NotImplementedError()

_ensure_routine_from_dict

_ensure_routine_from_dict(
    data: dict[str, Any]
) -> RoutineV1
Source code in src/qref/functools.py
@ensure_routine.register(dict)
def _ensure_routine_from_dict(data: dict[str, Any]) -> RoutineV1:
    return SchemaV1(**data).program if "version" in data else RoutineV1(**data)

_ensure_routine_from_schema_v1

_ensure_routine_from_schema_v1(data: SchemaV1) -> RoutineV1
Source code in src/qref/functools.py
@ensure_routine.register
def _ensure_routine_from_schema_v1(data: SchemaV1) -> RoutineV1:
    return data.program

_ensure_routine_from_routine_v1

_ensure_routine_from_routine_v1(
    data: RoutineV1,
) -> RoutineV1
Source code in src/qref/functools.py
@ensure_routine.register
def _ensure_routine_from_routine_v1(data: RoutineV1) -> RoutineV1:
    return data

accepts_all_qref_types

accepts_all_qref_types(
    f: Callable[Concatenate[RoutineV1, P], T]
) -> Callable[Concatenate[AnyQrefType, P], T]

Make a callable accepting RoutineV1 as first arg capable of accepting arbitrary QREF object.

Here, by arbitrary QREF object we mean either an instance of SchemaV1, an instance of RoutineV1, or any dictionary that can be converted to an instance of SchemaV1 or RoutineV1.

Parameters:

Name Type Description Default
f Callable[Concatenate[RoutineV1, P], T]

Callable to be augmented.

required

Returns:

Type Description
Callable[Concatenate[AnyQrefType, P], T]

A new callable preserving behavoiur of f, but also capable of accepting SchemaV1 instance or dicts

Callable[Concatenate[AnyQrefType, P], T]

as first arguments.

Source code in src/qref/functools.py
def accepts_all_qref_types(f: Callable[Concatenate[RoutineV1, P], T]) -> Callable[Concatenate[AnyQrefType, P], T]:
    """Make a callable accepting RoutineV1 as first arg capable of accepting arbitrary QREF object.

    Here, by arbitrary QREF object we mean either an instance of SchemaV1, an instance of RoutineV1,
    or any dictionary that can be converted to an instance of SchemaV1 or RoutineV1.

    Args:
        f: Callable to be augmented.

    Returns:
        A new callable preserving behavoiur of f, but also capable of accepting SchemaV1 instance or dicts
        as first arguments.
    """

    @wraps(f)
    def _inner(routine: SchemaV1 | RoutineV1 | dict[str, Any], *args: P.args, **kwargs: P.kwargs) -> T:
        return f(ensure_routine(routine), *args, **kwargs)

    return _inner