Skip to main content

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

Project description

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

pip install hookery==3.10.1

Basic Usage

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.

>>> 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hookery-5.0.1.tar.gz (6.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page