The Twython module uses requests module internally.
I want to wrap/decorate request's requests.post(*k, **kw)
method so everything Twython makes a request.post(...)
call it will be transparently decorated/wrapped without interfering with the Twython module.
If I edited the requests codebase that'd be easy enough but I'm curious how to solve the general problem of adding a decorator to an already defined function/method.
import requests
def magic_wrapper_doodad(...)
...
...
requests.post = magic_wrapper_doodad(my_function, requests.post) # plz?
import Twython
# thanks to the above magic, requests.post is wrapped just as if it was defined like:
@decorator
def trace(f, *args, **kw):
print("calling %s with args %s, %s" % (f.__name__, args, kw))
return f(*args, **kw)
...
... #inside requests.py now:
@trace
def post(self, *args, **kw):
...
how do I write magic_wrapper_doodad()
- or some alternative code - so I can decorate the code like this?