@hook decorator¶
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)
...
Bases: int, Enum
Hook priority enum.
All hooks within the same priority are executed in the same order they were registered.
Added in 0.7.0
Bases: Generic[T]
Hook representation.
Added in 0.7.0
Examples:
from onetl.hooks.hook import Hook, HookPriority
def some_func(*args, **kwargs): ...
hook = Hook(callback=some_func, enabled=True, priority=HookPriority.FIRST)
__init__(callback, enabled=True, priority=HookPriority.NORMAL)
¶
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