I am writing a webapi using flask and flask-restplus. I have created a decorator to calculate the time to execute api call.
import time
class TimeCalculateDecorator:
def __init__(self, func):
self.function = func
def __call__(self,*args, **kws):
ts = time.time()
result = self.function(*args, **kws)
te = time.time()
print('func:%r args:[%r, %r] took: %2.4f sec' % \
(self.function.__name__, args, kw, te-ts))
return result
this is how I am using it...
class MultipleEmp_controller(Resource):
@inject
def __init__(self,api, dataservice:DataServiceBase):
self.service = dataservice
@TimeCalculateDecorator
def get(self):
"""Get Request for version 1"""
emp_schema = EmployeeSchema(many=True)
emps = emp_schema.dump(self.service.getemployees())
return emps
The problem comes when decorator calls above controller method and found self argument is missing from there. Here is the exact error message...
"get() missing 1 required positional argument: 'self'",
I tried google but no luck.
I tried with simple method decorator like below...
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print('func:%r args:[%r, %r] took: %2.4f sec' % \
(method.__name__, args, kw, te-ts))
return result
return timed
and this is working perfectly fine. The issue comes when I make it as class, not as method.