Let's look at a simple method:
def test_method(): a = 1 b = 10000 c = 20000 sum1 = sum(range(a,b)) sum2 = sum(range(b,c)) return (sum1,sum2)
To use this method with a decorator, a simple decorator would be:
from functools import wraps def timed_decorator(f): @wraps(f) def wrapper(*args, **kwds): start = time.time() result = f(*args, **kwds) elapsed = (time.time() - start)*1000 logger.debug("f::{0} t::{1:0.2f} ms".format(f.__name__, elapsed)) return result return wrapper
Now, if I want certain test_method lines test_method indicate line 4 sum1 = sum(range(a,b)) , the current implementation includes inline coding, for example:
def test_method(): a = 1 b = 10000 c = 20000 start = time.time() sum1 = sum(range(a,b))
The goal is to use a decorator for timelines from M to N of a particular method without changing the code in the method. Is it possible to introduce such logic using a decorator?
python
Dhruvpathak
source share