简体   繁体   English

在 python 中动态构建表达式

[英]Building an expression in python dynamically

I want to build an expression and evaluate it in python.我想构建一个表达式并在 python 中对其进行评估。 Basically I will have Rule ID for which I need to build a Trigger condition (T), LHS (L), RHS (R) in the form of expression.基本上,我将拥有规则 ID,我需要以表达式的形式为其构建触发条件 (T)、LHS (L)、RHS (R)。

All the data required to build the above 3 expression will be stored in a form of three tables in database.构建上述3个表达式所需的所有数据都将以三张表的形式存储在数据库中。 Let say,让说,

1) Constant table - stores rule Id and constants along with their order 1) 常量表 - 存储规则 ID 和常量及其顺序

2) Operator table - stores rule Id and operators, prior operator along with their order 2) 运算符表 - 存储规则 ID 和运算符、优先运算符及其顺序

3) Variable table - stores rule Id and variables, prior operator along with their order 3) 变量表 - 存储规则 ID 和变量、优先运算符及其顺序

Tables data will be in form of表格数据将采用以下形式

在此处输入图片说明

so based on the equation order id, I need to build T, L, R expression for the given RuleId.所以基于方程顺序 id,我需要为给定的 RuleId 构建 T、L、R 表达式。 For example: expression for the RuleId =3, T,L,R will be as follows例如:RuleId =3, T,L,R 的表达式如下

T : ( ! 2204) and ( ! 2204) T:(!2204)和(!2204)

L : 2204电话:2204

R : 2204 * 2.00000 R : 2204 * 2.00000

Tried by building the expressions using stack, queue in C#, it worked fine.通过在 C# 中使用堆栈和队列构建表达式进行了尝试,效果很好。 Is there any other easy way exists in python to achieve this? python中还有其他简单的方法来实现这一点吗?

Had a very similar issue and cs95's solution on the related post worked for me:有一个非常相似的问题,cs95 在相关帖子上的解决方案对我有用:

Dynamically filtering a pandas dataframe 动态过滤熊猫数据框

Basically, string parsing + eval() looks like a good option, or df.query() if you're using pandas already.基本上,字符串解析 + eval() 看起来是一个不错的选择,或者 df.query() 如果您已经在使用 Pandas。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM