Package: callpyback
callpybackDescription"callpyback" is a comprehensive Python library designed to help developers add callbacks to their functions with ease. It comes with a range of powerful features that make it easy to customize the behavior of your functions in different stages of their execution.You can specify callbacks for on_call, on_success, on_failure, and on_end, and customize the default return value from the decorated function. Additionally, you can pass local scope variables of the decorated function to the on_end callback and define expected exceptions to trigger the on_failure callback. If desired, you can also omit callbacks, falling back to default behavior, and choose which parameters of the callback function to use. Furthermore, with the @background_callback decorator, you can execute callbacks on the background, making it easier to manage concurrency in your code.FeaturesSupporton_call,on_success,on_failureandon_endcallbacksPass decorated function **kwargs and function itself to callbacksOption to specify default return from the decorated functionOption to pass local scope variables of the decorated function to theon_endcallbackOption to specify exception classes to be expected and invokeon_failurecallbackOption to omit callbacks - default callbackOption to omit callback's function parameters (specify only those which you need)Option to execute callbacks on the background (new thread) via@background_callpybackdecoratorInstalationPackage is currently available under the same name at.pip install callpyback(back to top)Usage! important note !In latest version ofcallpyback, when declaring callback functions, following rules must be obeyed:a)on_call()callback MUST eitheraccept no parameters or combination of the following:func- will receive reference to decorated functionfunc_kwargs- will receive parameters passed to the function decorated withCallPyBackb)on_success()callback MUST either accept no parameters or combination of the following:func- will receive reference to decorated functionfunc_result- will receive return value of the function decorated withCallPyBackfunc_kwargs- will receive parameters passed to the function decorated withCallPyBackc)on_failure()callback MUST either accept no parameters or combination of the following:func- will receive reference to decorated functionfunc_exception- will receive exception raised by the function decorated withCallPyBackfunc_kwargs- will receive parameters passed to the function decorated withCallPyBackd)on_end()callback MUST either accept no parameters or combination of the following:func- will receive reference to decorated functionfunc_result- will receive return value of the function decorated withCallPyBackfunc_exception- will receive exception raised by the function decorated withCallPyBackfunc_kwargs- will receive parameters passed to the function decorated withCallPyBackfunc_scope_vars- will receive local variables of the function decorated withCallPyBack, whose names were specified in thepass_varsdecorator parameter.These rules are enforced to allow omitting parameters in the callback function. This is useful when some of these parameters are not needed for the callback function. If those rules are not obeyed, error will be raised during the initialization of theCallPyBackdecorator class.(back to top)PrerequisitesConsider following callbacks:defon_call(func,func_kwargs):print('-----ON CALL CALLBACK-----')func_kwargs_repr=', '.join(f'{key}={val}'forkey,valinfunc_kwargs.items())print(f'Function `{func.__name__}` called with parameters:{func_kwargs_repr}.\n')@background_callpybackdefon_success(func,func_result,func_kwargs):print('-----ON SUCCESS CALLBACK-----')func_kwargs_repr=', '.join(f'{key}={val}'forkey,valinfunc_kwargs.items())print(f'Function `{func.__name__}` successfully done with a result:{func_result}.')print(f'Was called with parameters:{func_kwargs_repr}\n')@background_callpybackdefon_failure(func,func_exception,func_kwargs):print('-----ON FAILURE CALLBACK-----')func_kwargs_repr=', '.join(f'{key}={val}'forkey,valinfunc_kwargs.items())print(f'Function `{func.__name__}failed with an error:{func_exception}!')print(f'Was called with parameters:{func_kwargs_repr}\n')@background_callpybackdefon_end(func,func_result,func_exception,func_kwargs,func_scope_vars):print('-----ON END CALLBACK-----')func_kwargs_repr=', '.join(f'{key}={val}'forkey,valinfunc_kwargs.items())func_scope_vars_repr=', '.join(f'{key}={val}'forkey,valinfunc_scope_vars.items())iffunc_exception:print(f'Function `{func.__name__}failed with an error:{func_exception}!')else:print('No exception was raised')print(f'Function `{func.__name__}` done with a result:{func_result}.')print(f'Was called with parameters:{func_kwargs_repr}')print(f'Local variables of the function:{func_scope_vars_repr}')and initialization of a decorator:custom_callpyback=CallPyBack(on_call=on_call,on_success=on_success,on_failure=on_failure,on_end=on_end,default_return='default',exception_classes=(RuntimeError,),pass_vars=('a',))These will be used in following examples:1. Decorated function executes without error@custom_callpybackdefmethod(x,y,z=None):a=42returnx+yresult=method(1,2)print(f'Result:{result}')will result in-----ONCALLCALLBACK-----
Function`method`calledwithparameters:x=1,y=2,z=None.

Result:3-----ONSUCCESSCALLBACK-----
Function`method`successfullydonewitharesult:3.
Wascalledwithparameters:x=1,y=2,z=None

-----ONENDCALLBACK-----
Noexceptionwasraised
Function`method`donewitharesult:3.
Wascalledwithparameters:x=1,y=2,z=None
Localvariablesofthefunction:a=42on_successandon_endwill be executed on the background thread, whileon_callwill be executed in a blocking way andon_failurewill not be called.2. Decorated function raises an error@custom_callpybackdefmethod(x,y,z=None):a=42raiseRuntimeError("some error")result=method(1,2)print(f'Result:{result}')will result in-----ONCALLCALLBACK-----
Function`method`calledwithparameters:x=1,y=2,z=None.

-----ONFAILURECALLBACK-----
Function`method`failedwithanerror:someerror!
Wascalledwithparameters:x=1,y=2,z=None

-----ONENDCALLBACK-----
Function`method`failedwithanerror:someerror!
Function`method`donewitharesult:default.
Wascalledwithparameters:x=1,y=2,z=None
Localvariablesofthefunction:a=42on_failureandon_endwill be executed on the background thread, whileon_callwill be executed in a blocking way andon_successwill not be called.(back to top)RoadmapAdd ChangelogSupporton_call,on_success,on_failureandon_endcallbacksOption to specify default return from the decorated functionOption to pass local scope variables of the decorated function to theon_endcallbackOption to specify exception classes to be expected and invokeon_failurecallbackOption to omit callbacks - default callbackOption to omit callback's function parameters (specify only those which you need)Option to execute callbacks on the background (new thread) via@background_callpybackdecoratorOption to pass decorated function reference to all callbacksTo be determined...See theopen issuesfor a full list of proposed features (and known issues).(back to top)ContributingContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make aregreatly appreciated.If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!Fork the ProjectCreate your Feature Branch (git checkout -b feature/feature-name)Commit your Changes (git commit -m 'Add some FeatureName')Push to the Branch (git push origin feature/feature-name)Open a Pull RequestLet us review your magical feature(back to top)LicenseDistributed under the MIT License. SeeLICENSE.txtfor more information.(back to top)ContactSamuel Gregorovič -samuel-gregorovic-samuelgregorovic@gmail.comProject Link:https://github.com/samuelgregorovic/callpyback(back to top)