I am attempting to write a wrapper for the statsmodels formula API (this is a simplified version, the function does more than this):
import statsmodels.formula.api as smf
def wrapper(formula, data, **kwargs):
return smf.logit(formula, data).fit(**kwargs)
If I give this function to a user, who then attempts to define his/her own function:
def square(x):
return x**2
model = wrapper('y ~ x + square(x)', data=df)
they will receive a NameError
because the patsy
module is looking in the namespace of wrapper
for the function square
. Is there a safe, Pythonic way to handle this situation without knowing a priori what the function names are or how many functions will be needed?
FYI: This is for Python 3.4.3.