A way to hijack python methods and functions and attach callbacks or alter functionality.
Project description
The pyjack contains two basic functions – pyjack.connect and pyjack.disconnect – which allow you to connect to other functions and methods. This can be used to:
attach observers or callbacks to functions/methods: Connect a callback ‘observer’ functions to be called whenever the pyjacked function is called. The callback is called with both the original arguments and the pyjacked function.
filter function/method arguments: A filter function can be provided which is used to alter the arguments passed to the function.
entirely replace existing function/method with your own function/method: pyjack allows you to ‘block’ the original function being called. In essence, this allows you to use pyjack to replace a function with your own callback (or filter) function.
Simple Example: Adding Callbacks To Functions
pyjack can be used to connect callbacks to functions:
import pyjack def max_callback(input, pyjack_org_fn): print 'Someone is trying to max ' + str(input) print 'Also got some pyjack_org_fn ' + str(pyjack_org_fn) pyjack.connect(max, callback = max_callback, filter = None, block = False) print max([10, 20]) # When you are done, disconnect and everything is back to normal ... pyjack.disconnect(max, callback = max_callback, filter = None, block = False)
More Uses
For more information …
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.