0

I have a dictionary with some function expressions as values. Each of the values are very similar, except the part in the middle. In the following example, only earn_yld, free_cash_flow_yield and eps_growth are different in the long formula.

factor_bql = {
    "ltm_earnings_yield": bq.func.dropna(bq.data.earn_yld(as_of_date=bq.func.RANGE(params['start'],params['end']))),
    "ltm_fcf_yield": bq.func.dropna(bq.data.free_cash_flow_yield(as_of_date=bq.func.RANGE(params['start'],params['end']))),
    'ltm_eps_growth': bq.func.dropna(bq.data.eps_growth(as_of_date=bq.func.RANGE(params['start'],params['end'])))
}

Is there any way to write a function or variable to simplify the values of the dictionary to something like

def simple_formula(xyz):
    ... ...

factor_bql = {
    "ltm_earnings_yield": simple_formula('earn_yld'),
    "ltm_fcf_yield": simple_formula('free_cash_flow_yield'),
    'ltm_eps_growth': simple_formula('eps_growth')
}

3 Answers 3

1

I'd do this in following way:

def simple_formula(fn):
    return bq.func.dropna(fn(as_of_date=bq.func.RANGE(params['start'],params['end'])))

factor_bql = {
    "ltm_earnings_yield": simple_formula(bq.data.earn_yld),
    "ltm_fcf_yield": simple_formula(bq.data.free_cash_flow_yield),
    'ltm_eps_growth': simple_formula(bq.data.eps_growth)
}

So, functions themselves (not their names) are parameters of simple_formula.

Sign up to request clarification or add additional context in comments.

2 Comments

@Tupteq--I suppose argument xyz is superfluous in simpe_formula?
@DarrylG I assumed xyz to represent an example parameter (as in question). But I think you're right, I'll remove it.
0

You can use the globals function to call a function in the current module by the string representation of its name.

def func1(bar):
    return "func1" + str(bar)


def func2(bar):
    return "func2" + str(bar)


def simple_formula(func_name):
    return globals()[func_name](bar="baz")


factor_bql = {
    "key1": simple_formula("func1"),
    "key2": simple_formula("func2"),
}

print(factor_bql["key2"]) # prints "func2baz"

Comments

0

Assuming bq.data is some object:

def simple_formula(xyz):
    method = getattr(bq.data, xyx) # get a method by its name
    return bq.func.dropna(method(as_of_date=bq.func.RANGE(params['start'],params['end'])))

2 Comments

Thanks. I tried that approach and got the following error message: ``` File "<ipython-input-32-40f549057bae>", line 20 return bq.func.dropna(method((as_of_date=bq.func.RANGE(params['start'],params['end']))) ^ SyntaxError: invalid syntax ``` Looks like that the equal sign in the formula is generating syntax error. That is not the issue if I directly use the formula, rather than simple_formula function.
Looks like brackets don't match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.