How do I decorate a class method with arguments? Current code is
def establish_con(func):
con, meta = db.connect(config.user, config.password, config.db)
meta.reflect(bind=con)
def inner(self, query, con, *args, **kwargs):
return func(self, query, con, *args, **kwargs)
con.close()
return inner
class DataReader:
def __init__(self):
self.data = {}
@establish_con
def execQuery(self, query, con):
# con, meta = db.connect(config.user, config.password, config.db)
# meta.reflect(bind=con)
result = pd.read_sql(query, con)
# con.close()
return result
test = DataReader()
df = test.execQuery("Select * from backtest limit 10")
print(df)
Currently, the first argument seems to be the class instance. I tried different variations of the code, but always ran into too many/not enough/undefined arguments problems.
I've read other posts like
How do I pass extra arguments to a Python decorator?
and others, but still can't figure it out.
Edit: Not a duplicate of Python decorators in classes as in this answer no arguments need to be passed to the function.