I don't know exactly how to search for this question or find the answer. Or if it's possible the way I'm describing.
FWI, it's not c/profile
I'm looking for. I know based on what I say that it may seem that's what I want, but as far as I can tell, it's not. I know I could probably inherit from them, but that's just essentially making something new anyway.
If I have a class the has some methods defined, and I wish to do testing. Is there a way I can create a wrapper/decorator or some type of logger, as to when all (or possible selected) methods run within a class, I run code to print what method is running and how long it takes (or whatever other code I want to run).
For Example
@some_decorator (what Im trying to figure out)
class Adder:
def add(self, a, b):
return a + b
def more_add(self, c, d):
return self.add(self.add(c, d), self.add(c, d))
# return (c + d) + (c + d)
Adder().add(1, 2)
Adder().more_add(4, 5)
"""
output >>
Method "add" called
took 1 second
Method "more_add" called
Method "add" called
Method "add" called
took 1 second
Method "add" called
took 1 second
took 3 seconds
took 3 seconds
"""
I would like to do this without individually going in and overloading/overriding/decorating each method. I hope this makes sense.
P.S. I'm not currently worried too much about the syntax or whether exactly how I typed it, it would work. Thanks!