Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hookery

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hookery

Trivial, primitive, naive, and optimistic hooks in Python 3.6+

  • 5.0.1
  • PyPI
  • Socket score

Maintainers
1

hookery


Minimally invasive hooks with no metaclasses and no class decorators since v5.0.0.

Status

  • v3.10.1 status is Beta.
  • v5.x status is Pre-Alpha.

I use v3.10.1 (https://pypi.org/project/hookery/3.10.1/) in production for work.

This document is for v5.x which is NOT production-ready yet.

Installation

.. code-block:: shell

pip install hookery==3.10.1

Basic Usage

.. code-block:: python

from hookery import Hook, hooks

class Profile:
    on_activated = Hook()

    @on_activated
    def log_activation(self):
        print(f"Activating {self}")

    def activate(self):
        hooks.trigger(self.on_activated)


class WarehouseProfile(Profile):

    @Profile.on_activated
    def log_activation(self):
        print(f"Warehouse profile {self} is being activated")

    @Profile.on_activated
    def another_handler(self):
        print(f"This will also be printed")

The dummy example above demonstrates that:

  • a hook registration requires no metaclasses, no base classes, no class decorators.
  • you can register any number of handlers per hook inside the same class
  • a handler called log_activation in child class does not override a handler with the same name in parent class.

.. code-block:: python

>>> profile = WarehouseProfile()
>>> profile.activate()
Activating <__main__.WarehouseProfile object at 0x103ee66d8>
Warehouse profile <__main__.WarehouseProfile object at 0x103ee66d8> is being activated
This will also be printed

Limitations

  • For us, hooks and hook handlers are part of the class specification. This means that a class cannot have new hooks added or new hook handlers registered after the class has been created. If you wish to add functionality you either modify the class or extend it.
  • @classmethod's and @staticmethod's cannot be registered as handlers because hooks apply to instances of a class, not the class itself.
  • It is best to not decorate handlers with anything else.

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc