Skip to main content

Simple Hooks

The purpose of this package is to provide a simple way to add hooks to functions and methods that trigger before or after the function/method call.

Only downside is that it breaks original type analysis and related tools.

Installation

pip install simple-hooks

Usage

There are several ways to use this library. There are four functions available:

  • enable_hooks
  • enable_hooks_on_callable
  • enable_hooks_on_method
  • enable_hooks_on_class

The simplest way to use this is to use enable_hooks as a decorator as it will automatically call either enable_hooks_on_class or enable_hooks_on_callable; enable_hooks_on_class will call enable_hooks_on_method instead of enable_hooks_on_callable for class methods.

from simple_hooks import enable_hooks
make_log = lambda label: lambda *args, **kwargs: print(f"{label}: {args=} {kwargs=}")


@enable_hooks
def does_something(arg: str):
    print(arg)

does_something.add_before_hook(make_log('before'))
does_something.add_after_hook(make_log('after'))

does_something('hello')
# before: args=('hello',) kwargs={}
# hello
# after: args=('hello',) kwargs={}

The enable_hooks decorator can also be used with class methods:

from simple_hooks import enable_hooks
make_log = lambda label: lambda *args, **kwargs: print(f"{label}: {args=} {kwargs=}")

class Thing:
    @enable_hooks
    def do_something(self, arg: str):
        print(arg)
    def __repr__(self) -> str:
        return "Thing instance"

Thing.do_something.add_before_hook(make_log('before original'))
Thing.do_something.add_after_hook(make_log('after original'))
t = Thing()

t.do_something('test')
# before original: args=(Thing instance, 'test') kwargs={}
# test
# after original: args=(Thing instance, 'test') kwargs={}

t.do_something.add_before_hook(make_log('before instance'))
t.do_something.add_after_hook(make_log('after instance'))

t.do_something('test')
# prints the following:
# before original: args=(Thing instance, 'test') kwargs={}
# before instance: args=(Thing instance, 'test') kwargs={}
# test
# after original: args=(Thing instance, 'test') kwargs={}
# after instance: args=(Thing instance, 'test') kwargs={}

The enable_hooks decorator can also be used with a class itself.

from simple_hooks import enable_hooks
make_log = lambda label: lambda *args, **kwargs: print(f"{label}: {args=} {kwargs=}")


@enable_hooks
class Thing:
    def do_something(self, arg: str):
        print(arg)
    def __repr__(self) -> str:
        return "Thing instance"

Thing.do_something.add_before_hook(make_log('before original'))
Thing.do_something.add_after_hook(make_log('after original'))
t = Thing()

t.do_something('test')
# before original: args=(Thing instance, 'test') kwargs={}
# test
# after original: args=(Thing instance, 'test') kwargs={}

t.do_something.add_before_hook(make_log('before instance'))
t.do_something.add_after_hook(make_log('after instance'))

t.do_something('test')
# prints the following:
# before instance: args=(Thing instance, 'test') kwargs={}
# before original: args=(Thing instance, 'test') kwargs={}
# test
# after original: args=(Thing instance, 'test') kwargs={}
# after instance: args=(Thing instance, 'test') kwargs={}

Testing

To test, clone the repo and run the following:

python tests/test_functions.py

There are currently only 5 tests as this is a simple library.

License

ISC License

Copyleft (c) 2023 k98kurz

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyleft notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Metadata

Release files for simple-hooks 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-hooks 0.1.0
File Size Uploaded Provenance
simple_hooks-0.1.0.tar.gz 3.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-hooks 0.1.0
File Interpreter ABI Platform
simple_hooks-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.3 kB

Release files / simple_hooks-0.1.0.tar.gz

Download URL simple_hooks-0.1.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
61753b13041a4b001186c5ba06dba94483916177136080091f8f83f29a12776e
BLAKE2b-256 checksum
How to use checksums
740c101620dade8d1a38eb0e61fe263c28ffad0ff54353d2392849756dc1d889
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / simple_hooks-0.1.0-py3-none-any.whl

Download URL simple_hooks-0.1.0-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b33f27a9870b33c65a4da40fb463e5319c0eed243a682d04c560b83d357c02a
BLAKE2b-256 checksum
How to use checksums
cbbbcddf7478f8f6e81bf078ab382c96a39a6d9eb903700c3195733c5d69a866
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Security for simple-hooks 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page