Skip to content

@hook decorator

hook(inp=None, *, enabled=True, priority=HookPriority.NORMAL)

hook(inp: Callable[P, T], *, enabled: bool = True, priority: HookPriority = HookPriority.NORMAL) -> Callable[P, T]
hook(inp: None, *, enabled: bool = True, priority: HookPriority = HookPriority.NORMAL) -> Callable[[Callable[P, T]], Callable[P, T]]

Initialize hook from callable/context manager.

Added in 0.7.0

Examples:

from onetl.hooks import hook, HookPriority


@hook
def some_func(*args, **kwargs):
    ...


@hook(enabled=True, priority=HookPriority.FIRST)
def another_func(*args, **kwargs):
    ...
from onetl.hooks import hook, HookPriority


@hook
class SimpleContextManager:
    def __init__(self, *args, **kwargs):
        ...

    def __enter__(self):
        ...
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        ...
        return False


@hook(enabled=True, priority=HookPriority.FIRST)
class ContextManagerWithProcessResult:
    def __init__(self, *args, **kwargs):
        ...

    def __enter__(self):
        ...
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        ...
        return False

    def process_result(self, result):
        # special method to handle method result call
        return modify(result)

    ...

HookPriority

Hook priority enum.

All hooks within the same priority are executed in the same order they were registered.

Added in 0.7.0

FIRST = -1

Hooks with this priority will run first.

NORMAL = 0

Hooks with this priority will run after onetl.hooks.hook.HookPriority.FIRST but before onetl.hooks.hook.HookPriority.LAST.

LAST = 1

Hooks with this priority will run last.

Hook

Hook representation.

Added in 0.7.0

Parameters:

  • callback

    (Callable[ParamSpec, T]) –

    Some callable object which will be wrapped into a Hook, like function or ContextManager class.

  • enabled

    (bool, default: True ) –

    Will hook be executed or not. Useful for debugging.

  • priority

    (HookPriority, default: <HookPriority.NORMAL: 0> ) –

    Changes hooks priority, see HookPriority documentation.

Examples:

from onetl.hooks.hook import Hook, HookPriority


def some_func(*args, **kwargs): ...


hook = Hook(callback=some_func, enabled=True, priority=HookPriority.FIRST)

enable()

Enable the hook.

Added in 0.7.0

Examples:

>>> def func1(): ...
>>> hook = Hook(callback=func1, enabled=False)
>>> hook.enabled
False
>>> hook.enable()
>>> hook.enabled
True

disable()

Disable the hook.

Added in 0.7.0

Examples:

>>> def func1(): ...
>>> hook = Hook(callback=func1, enabled=True)
>>> hook.enabled
True
>>> hook.disable()
>>> hook.enabled
False

skip()

Temporary disable the hook.

Note

If hook was created with enabled=False, or was disabled by disable, its state will left intact after exiting the context.

You should call enable explicitly to change its state.

Added in 0.7.0

Examples:

>>> def func1(): ...
>>> hook = Hook(callback=func1, enabled=True)
>>> hook.enabled
True
>>> with hook.skip():
...     print(hook.enabled)
False
>>> # hook state is restored as it was before entering the context manager
>>> hook.enabled
True
>>> def func1(): ...
>>> hook = Hook(callback=func1, enabled=True)
>>> hook.enabled
True
>>> @hook.skip()
... def hook_disabled():
...     print(hook.enabled)
>>> hook_disabled()
False
>>> # hook state is restored as it was before entering the context manager
>>> hook.enabled
True