Skip to main content

Trivial, primitive, naive, and optimistic hook registry in Python

Project description

pip install hookery

It’s really simple. There are some events in the lifetime of your simple Python application that you want to hook into, and you don’t want to be overriding methods or writing conditional code to do that. What you do is you introduce events and register hooks.

Quick Start

Let’s assume that ThingsService class is the corner stone of your API. This service allows adding things and removing them:

class ThingsService(object):
    def __init__(self):
        self._things = set()

    def add(thing):
        self._things.add(thing)

    def remove(thing)
        self._things.remove(thing)

You wish users of your API to be able to execute their code when a thing is added or removed, but you don’t want them to be extending your service class to do that.

What you do is you create an internal hook registry inside ThingsService and register two events that you wish your users to hook into, and expose them on your service class so that users can use them as decorators to register their hooks:

from hookery import HookRegistry

class ThingsService(object):
    def __init__(self):
        self._things = set()

        self._hooks = HookRegistry(self)
        self.thing_added = self._hooks.register_event('thing_added')
        self.thing_removed = self._hooks.register_event('thing_removed')

    def add(self, thing):
        self._things.add(thing)
        self.thing_added.trigger(things_service=self, thing=thing)

    def remove(self, thing):
        self._things.remove(thing)
        self.thing_removed.trigger(things_service=self, thing=thing)

Your API users can now register their hooks and be notified of all the latest updates from the things service:

service = ThingsService()

@service.thing_added
def added(thing):
    print('{} was added'.format(thing))

@service.thing_removed
def removed(thing, things_service):
    print('{} was removed, {} things remain'.format(thing, len(things_service._things)))


service.add(1)
service.add(2)
service.add(3)

service.remove(2)

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-1.1.2.tar.gz (5.5 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: hookery-1.1.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hookery-1.1.2.tar.gz
Algorithm Hash digest
SHA256 93384e4b45c919bd8d8901a7cdd79903a53692bbe3a5a402feb2410bd4cc39c6
MD5 590879ca1aa4293c7241622b8aab9d4c
BLAKE2b-256 d10d729b63e7169c7d9f34506f9b46b0784cf9173d1e92acb39e3020795fd3ae

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