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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for hookery-1.4.0.tar.gz
Algorithm Hash digest
SHA256 4128ce74198b091282280e0623e6533d31a76ca38f5ac10644d6667fb3de4e56
MD5 4e2a4a99dac226bd2d1cff9538afcbd2
BLAKE2b-256 5cb038da777ae7b95e7cfe158c4de6741be28dd264c1a12d0b0f7607bad36d91

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