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-hypernode.com/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 details)

Uploaded Source

File details

Details for the file hookery-5.0.1.tar.gz.

File metadata

  • Download URL: hookery-5.0.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.3

File hashes

Hashes for hookery-5.0.1.tar.gz
Algorithm Hash digest
SHA256 1662f054e8939019f72de297e06168b901914e99469f768c6e04a582e07e9ad4
MD5 d2e27a135f92687bf046f79e84cff529
BLAKE2b-256 29562bfeb60342c9ea4bf0e6ca498b927b873091f395f4dbe96371b3a4837c3e

See more details on using hashes here.

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