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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for hookery-1.3.0.tar.gz
Algorithm Hash digest
SHA256 a0d9ffa1194a2b0a24ca47600cdae32a8538bfc97f2006cfd48c2bfa8ec2f19a
MD5 4284ac61850d8a91c6d546cf4a3345c2
BLAKE2b-256 1cf9500716335a6d1b079cf206bf0512f2ca6eb446783d8c6638027fa2583113

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