我需要在前缀表示法中评估条件语句,如下所示:
condition = ('AND', 'NE','$type','email','EQ','$link','space')
其中: -
'AND','NE', 'EQ'
等是逻辑运算符 - AND, Not equal to (NE), EQ (equal to
)
为了自动评估,除了'AND'和'OR'之外,我可以从操作员模块中为操作员休息。有人可以建议吗?下面有更多解释(如果可以更好地实施,请告诉我):
在评估条件时,以$开头的那些将被替换为输入字典,其键等于字符串(在剥离$'之后)。
Input = {'type': 'email', 'link' : 'tab' }
运算符模块可用于评估二进制操作,如:
subst_op = []
Inp = {'type': 'email', 'link' : 'tab' }
# substitute input parameters
for cond in condition:
subst_op.append(Inp[cond[1:]] if '$' in cond else cond)
#while input, start from right pushing operands on to stack and if operator, pop two operands, evaluate with the operator and push on to stack (http://en.wikipedia.org/wiki/Polish_notation)
....
# for evaluation, use the operator module
if condition in ('NE','EQ' ..)
getattr(operator, condition)(popped_operand1,popped_operand2)
运营商没有'AND'和'OR'。请建议。