gpt4 book ai didi

python - 将自定义公式转换为 python 函数

转载 作者:太空狗 更新时间:2023-10-30 00:13:31 27 4
gpt4 key购买 nike

<分区>

考虑我们有以下输入

formula = "(([foo] + [bar]) - ([baz]/2) )"

function_mapping = {
"foo" : FooFunction,
"bar" : BarFunction,
"baz" : BazFunction,
}

是否有任何 python 库可以让我解析公式并将其转换为python 函数表示。

例如。

converted_formula = ((FooFunction() + BarFunction() - (BazFunction()/2))

我目前正在研究类似的东西

In [11]: ast = compiler.parse(formula)

In [12]: ast
Out[12]: Module(None, Stmt([Discard(Sub((Add((List([Name('foo')]), List([Name('bar')]))), Div((List([Name('baz')]), Const(2))))))]))

然后进一步处理这棵ast树。

您知道任何更清洁的替代解决方案吗?非常感谢任何帮助或见解!

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com