0

我需要以如下所示的前缀表示法评估条件语句:

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”。请建议。

4

0 回答 0