Декоратор @slot
Decorator which enables hooks functionality on a specific class method.
Decorated methods get additional nested methods:
* :obj:`onetl.hooks.slot.Slot.bind`
* :obj:`onetl.hooks.slot.Slot.suspend_hooks`
* :obj:`onetl.hooks.slot.Slot.resume_hooks`
* :obj:`onetl.hooks.slot.Slot.skip_hooks`
.. note::
Supported method types are:
* Regular methods
* ``@classmethod``
* ``@staticmethod``
It is not allowed to use this decorator over ``_private`` and ``__protected`` methods and ``@property``.
But is allowed to use on ``__dunder__`` methods, like ``__init__``.
.. versionadded:: 0.7.0
Examples
.. code:: python
from onetl.hooks import support_hooks, slot, hook
@support_hooks
class MyClass:
@slot
def my_method(self, arg): ...
@slot # decorator should be on top of all other decorators
@classmethod
def class_method(cls): ...
@slot # decorator should be on top of all other decorators
@staticmethod
def static_method(arg): ...
@MyClass.my_method.bind
@hook
def callback1(self, arg): ...
@MyClass.class_method.bind
@hook
def callback2(cls): ...
@MyClass.static_method.bind
@hook
def callback3(arg): ...
obj = MyClass()
obj.my_method(1) # will execute callback1(obj, 1)
MyClass.class_method(2) # will execute callback2(MyClass, 2)
MyClass.static_method(3) # will execute callback3(3)
Source code in onetl/hooks/slot.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | |
Bases: Protocol
Protocol which is implemented by a method after applying :obj:~slot decorator.
.. versionadded:: 0.7.0
Source code in onetl/hooks/slot.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 | |
__hooks__
property
Collection of hooks bound to the slot
resume_hooks()
Resume all hooks bound to the slot.
.. note::
If hook is disabled by :obj:`onetl.hooks.hook.Hook.disable`, it will stay disabled.
You should call :obj:`onetl.hooks.hook.Hook.enable` explicitly.
Examples
.. code:: python
from onetl.hooks.hook import hook, support_hooks, slot
@support_hooks
class MyClass:
@slot
def my_method(self, arg): ...
@MyClass.my_method.bind
@hook
def callback1(self, arg): ...
obj = MyClass()
obj.my_method(1) # will call callback1(obj, 1)
MyClass.my_method.suspend_hooks()
obj.my_method(1) # will NOT call callback1
MyClass.my_method.resume_hooks()
obj.my_method(2) # will call callback1(obj, 2)
Source code in onetl/hooks/slot.py
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
skip_hooks()
Context manager which temporary stops all the hooks bound to the slot.
.. note::
If hooks were stopped by :obj:`~suspend_hooks`, they will not be resumed
after exiting the context/decorated function.
You should call :obj:`~resume_hooks` explicitly.
Examples
.. tabs::
.. code-tab:: py Context manager syntax
from onetl.hooks.hook import hook, support_hooks, slot
@support_hooks
class MyClass:
@slot
def my_method(self, arg):
...
@MyClass.my_method.bind
@hook
def callback1(self, arg):
...
obj = MyClass()
obj.my_method(1) # will call callback1(obj, 1)
with MyClass.my_method.skip_hooks():
obj.my_method(1) # will NOT call callback1
obj.my_method(2) # will call callback1(obj, 2)
.. code-tab:: py Decorator syntax
from onetl.hooks.hook import hook, support_hooks, slot
@support_hooks
class MyClass:
@slot
def my_method(self, arg):
...
@MyClass.my_method.bind
@hook
def callback1(self, arg):
...
@MyClass.my_method.skip_hooks()
def method_without_hooks(obj, arg):
obj.my_method(arg)
obj = MyClass()
obj.my_method(1) # will call callback1(obj, 1)
method_without_hooks(obj, 1) # will NOT call callback1
obj.my_method(2) # will call callback1(obj, 2)
Source code in onetl/hooks/slot.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
suspend_hooks()
Stop all the hooks bound to the slot.
Examples
.. code:: python
from onetl.hooks.hook import hook, support_hooks, slot
@support_hooks
class MyClass:
@slot
def my_method(self, arg): ...
@MyClass.my_method.bind
@hook
def callback1(self, arg): ...
obj = MyClass()
obj.my_method(1) # will call callback1(obj, 1)
MyClass.my_method.suspend_hooks()
obj.my_method(1) # will NOT call callback1
Source code in onetl/hooks/slot.py
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | |