Skip to main content

Python hooks for methods

Project description

https://img.shields.io/travis/Shir0kamii/pyhooks/master.svg https://img.shields.io/coveralls/Shir0kamii/pyhooks/master.svg

PyHooks is meant to expose method hook for classes

Status

This project is maintained, feel free to open issues or pull requests.

Purpose

Have you ever wanted to execute code before or after a method ? PyHooks solve this exact problem taking inspiration from marshmallow’s hook system.

Installation

Like any other published python package, you can install it via pip :

pip install pyhooks

How to use ?

To use it, you first need to implement a hooked method. You do this by decorating the method with @Hook.

For example, suppose you have a class that at some moment save your data (such as a database). If you want to be able to plug new behavior, your code will look like this:

from pyhooks import Hook


class DatabaseEntry(object):
    @Hook
    def save(self):
        pass # save implementation here

Thanks to the @Hook line, you will now be able to add functions that execute before and after the save method using the decorators @precall_register and @postcall_register.

For instance, if you want to increment a version variable before saving, you could do:

from pyhooks import precall_register


class VersionnedEntry(DatabaseEntry):
    @precall_register("save")
    def increment_version(self):
        self.version += 1

The decorator directive indicates to your class that increment_version should be run before the save method that is passed to the decorator as argument.

Examples

You can find some more examples in the examples/ directory of this repository.

Advanced usage

You can specialize a register decorator by calling it outside of a decorator context. The last example would yield:

from pyhooks import precall_register

pre_save = precall_register("save")


class VersionnedEntry(DatabaseEntry):
    @pre_save
    def increment_version(self):
        self.version += 1

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

pyhooks-1.0.3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

pyhooks-1.0.3-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file pyhooks-1.0.3.tar.gz.

File metadata

  • Download URL: pyhooks-1.0.3.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for pyhooks-1.0.3.tar.gz
Algorithm Hash digest
SHA256 7c3e62349488b53ffcf40241ac061d89a269bc4e5d59c3cd448a5b8fe49ce7e0
MD5 97d80d8e30e13e283301f624a614cd2a
BLAKE2b-256 bba10ae6105cbcd0ca5c3b7435e2bb55dbbe9f14530cae84a582066d5f541910

See more details on using hashes here.

File details

Details for the file pyhooks-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyhooks-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for pyhooks-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6c9bdc14fbc87bc97f935aadef56bbca42697205ca0b5d1291259868296144a7
MD5 eb1655605eb0aecf2e88b7b1cf74a825
BLAKE2b-256 dbee9b01ffe55d589fcc380e5c396dd16eefd106ef5a315095e14c203610f469

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