I’m using the requests module, but the question is much more generic.
Is there a way to automatically execute code before calling methods from imported modules?
This would make writing code easier for me. At the moment I worry about hammering friendly webservices, so unless I can find an answer, I’ll have to implement my own controls.
I thought by doing this:
import requests as requests2
…then at some point further on in the code, define a requests() function, with special arguments or some magical undiscovered syntax. After running its own code, the special sauce would forward method calls to the real module aliased to requests2.
Can this be done?
You mean something like adding decorators to every function in another module? You could emulate that with a class:
Example usage: