A hopefully simple-to-use system for enabling hooks for before and after function/method calls
Project description
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 static 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 static'))
Thing.do_something.add_after_hook(make_log('after static'))
t = Thing()
t.do_something('test')
# before static: args=(Thing instance, 'test') kwargs={}
# test
# after static: 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 static: args=(Thing instance, 'test') kwargs={}
# before instance: args=(Thing instance, 'test') kwargs={}
# test
# after static: args=(Thing instance, 'test') kwargs={}
# after instance: args=(Thing instance, 'test') kwargs={}
The enable_hooks
decorator can also be used with a class itself. However, note
that the hooks for the methods will not be passed the self reference for hooks
added to instances.
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 static'))
Thing.do_something.add_after_hook(make_log('after static'))
t = Thing()
t.do_something('test')
# before static: args=(Thing instance, 'test') kwargs={}
# test
# after static: 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=('test') kwargs={}
# before static: args=(Thing instance, 'test') kwargs={}
# test
# after static: args=(Thing instance, 'test') kwargs={}
# after instance: args=('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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file simple_hooks-0.0.1.tar.gz
.
File metadata
- Download URL: simple_hooks-0.0.1.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 043f5022142da4f7b9f38b3e46996ab0cbc050a41807d39d79c528045d01e23a |
|
MD5 | ddf8923ad444cac66ac51ea3fa678845 |
|
BLAKE2b-256 | ae5e605d03fac0f587c9db7a6a9c026d76e3a07d13b2310b9c32c7328a1c4dac |
File details
Details for the file simple_hooks-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: simple_hooks-0.0.1-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfe93bce30d9a41bfc81257b787662f9360d55b3a4552221c66cb4db0abac14f |
|
MD5 | fc30fef94df66c14c6c95622d224ba50 |
|
BLAKE2b-256 | c40bda1386573ffc4bcb3d51434330eddeb5f47a3e4bfa5a33d72d46f36262ad |