Skip to content

@support_hooks decorator

skip_hooks(cls)

Context manager (and decorator) which temporary disables hooks for all the methods of a specific class.

Examples:

.. tabs::

.. code-tab:: py Context manager syntax

    @support_hooks
    class MyClass:
        @slot
        def my_method(self, arg):
            ...


    @MyClass.my_method.hook
    def callback(self, arg):
        ...


    obj = MyClass()
    obj.my_method(1)  # will execute callback(obj, 1)

    with MyClass.skip_hooks():
        obj.my_method()  # will NOT execute callback

    # running outside the context restores previous behavior
    obj.my_method(2)  # will execute callback(obj, 2)

.. code-tab:: py Decorator syntax

    @support_hooks
    class MyClass:
        @slot
        def my_method(self, arg):
            ...


    @MyClass.my_method.hook
    def callback(self, arg):
        ...


    def with_hook_enabled():
        obj = MyClass()
        obj.my_method(1)


    with_hook_enabled()  # will execute callback(obj, 1)


    @MyClass.skip_hooks()
    def with_all_hooks_disabled():
        obj = MyClass()
        obj.my_method(1)


    with_all_hooks_disabled()  # will NOT execute callback function

    # running outside a decorated function restores previous behavior
    obj = MyClass()
    obj.my_method(2)  # will execute callback(obj, 2)

.. versionadded:: 0.7.0

suspend_hooks(cls)

Disables all hooks for all the methods of a specific class.

Examples:

.. code:: python

@support_hooks
class MyClass:
    @slot
    def my_method(self, arg): ...


@MyClass.my_method.hook
def callback(self, arg): ...


obj = MyClass()
obj.my_method(1)  # will execute callback(obj, 1)

MyClass.suspend_hooks()

obj.my_method(2)  # will NOT execute callback

.. versionadded:: 0.7.0

resume_hooks(cls)

Enables all hooks for all the methods of a specific class.

Examples:

.. code:: python

@support_hooks
class MyClass:
    @slot
    def my_method(self, arg): ...


@MyClass.my_method.hook
def callback(self, arg): ...


obj = MyClass()

MyClass.suspend_hooks()
obj.my_method(1)  # will NOT execute callback

MyClass.resume_hooks()

obj.my_method(2)  # will execute callback(obj, 2)

.. versionadded:: 0.7.0

support_hooks(cls)

Decorator which adds hooks functionality to a specific class.

Only methods decorated with :obj:~slot can be used for connecting hooks.

Adds :obj:~skip_hooks, :obj:~suspend_hooks and :obj:~resume_hooks to the class.

.. versionadded:: 0.7.0

Examples:

.. code:: python

from onetl.hooks.hook import support_hooks, slot


@support_hooks
class MyClass:
    @slot
    def my_method(self, arg): ...


@MyClass.my_method.hook
def callback(self, arg): ...


MyClass().my_method()  # will execute callback function