Package: simple-hooks
Simple HooksThe 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.Installationpip install simple-hooksUsageThere are several ways to use this library. There are four functions available:enable_hooksenable_hooks_on_callableenable_hooks_on_methodenable_hooks_on_classThe simplest way to use this is to useenable_hooksas a decorator as it will
automatically call eitherenable_hooks_on_classorenable_hooks_on_callable;enable_hooks_on_classwill callenable_hooks_on_methodinstead ofenable_hooks_on_callablefor class methods.fromsimple_hooksimportenable_hooksmake_log=lambdalabel:lambda*args,**kwargs:print(f"{label}:{args=}{kwargs=}")@enable_hooksdefdoes_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={}Theenable_hooksdecorator can also be used with class methods:fromsimple_hooksimportenable_hooksmake_log=lambdalabel:lambda*args,**kwargs:print(f"{label}:{args=}{kwargs=}")classThing:@enable_hooksdefdo_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={}Theenable_hooksdecorator 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.fromsimple_hooksimportenable_hooksmake_log=lambdalabel:lambda*args,**kwargs:print(f"{label}:{args=}{kwargs=}")@enable_hooksclassThing:defdo_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={}TestingTo test, clone the repo and run the following:python tests/test_functions.pyThere are currently only 5 tests as this is a simple library.LicenseISC LicenseCopyleft (c) 2023 k98kurzPermission 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.