python:如何验证用户定义的条件?

python:如何验证用户定义的条件?,python,validation,conditional-statements,Python,Validation,Conditional Statements,我正在努力测试预定义的条件,这些条件采用用户提供的参数,如下例所示: cond = "if ( 1 is yes and 2 is no ) or ( 1 is yes and 2 is no )" cond2 = "if (3 is no or 1 is no )" vars = [] lst = cond.split() lst += cond2.split() for l in lst: if l.isdigit(): if l not i

我正在努力测试预定义的条件,这些条件采用用户提供的参数,如下例所示:

cond = "if ( 1 is yes and 2 is no ) or ( 1 is yes and 2 is no )" cond2 = "if (3 is no or 1 is no )" vars = [] lst = cond.split() lst += cond2.split() for l in lst: if l.isdigit(): if l not in vars: vars.append(l) # ... sort # ... read user answers => x = no, y = no, y = yes # ... replace numbers with input (yes or no) # ... finally I have cond = "if ( no is yes and no is no ) or ( no is yes and no is no )" cond2 = "if (yes is no or no is no )" cond=“如果(1为是,2为否)或(1为是,2为否)” cond2=“如果(3为否或1为否)” 变量=[] lst=条件拆分() lst+=cond2.split() 对于lst中的l: 如果l.isdigit(): 如果我不在VAR中: 附加变量(l) # ... 分类 # ... 阅读用户答案=>x=否,y=否,y=是 # ... 用输入替换数字(是或否) # ... 终于有了 cond=“如果(否为是,否为否)或(否为是,否为否)” cond2=“如果(是表示否或否表示否)” 首先,这是正确的方法吗?
第二,如果上述条件为真或假,如何验证


先谢谢你

用于解析和编译字符串,然后执行生成的AST。

因此,在根据Ignacio的提示进行了一些阅读之后,我认为在对字符串进行了一些修改之后,我就拥有了它。但是,仍然不能确定这是否是正确的方法。
因此,我的条件变量定义如下

cond = """if ( 'no' is 'yes' and 'no' is 'no' ): result.append[1] else: result.append[0] """ cond=“”如果('no'是'yes','no'是'no'): 结果。追加[1] 其他: 结果。追加[0] """ 此外,我还创建变量来存储条件结果,以便以后对其进行评估

result = [] 结果=[] 我在我的字符串上运行exec

exec(cond) 执行官(第二) 最后,我可以评估结果

if result[0] == 1 print "Condition was met" else: print "Condition wasn't met" 如果结果[0]==1 打印“满足条件” 其他: 打印“不符合条件”
非常感谢您的任何想法或评论。

用户的所有答案都可以用来形成一个唯一的二进制数(一个仅由0和1组成)。您可以为每个条件创建一个表(列表),该表由每个位置的答案的每个可能组合索引,并存储给定条件(表示为lambda函数)对该集合的值

设置这些表后,您可以通过查找由给定答案组合索引的对应表中的值来确定任何条件是否为真。下面是如何设置的示例

NUM_ANSWERS = 4
NUM_COMBOS = 2**NUM_ANSWERS
NO,YES = 'no','yes'

def index(answers):
    """ Convert a list of yes/no answers into binary number. """
    binstr = ''.join([('1' if a is 'yes' else '0') for a in answers])
    return int(binstr, 2)

def answers(index):
    """ Convert binary value of number into list of yes/no answers. """
    masks = [2**p for p in range(NUM_ANSWERS-1, -1, -1)]
    bits = [((index & m) / m) for m in masks]
    return [[NO,YES][b] for b in bits]

# condition expressions
cond_expr1 = lambda a1,a2,a3,a4: a1 is YES and a2 is NO  # a3,a4 ignored
cond_expr2 = lambda a1,a2,a3,a4: (
                ( a1 is YES and a2 is NO ) or ( a3 is YES and a4 is NO )
             )
# build tables for each condition
cond1 = []
cond2 = []
for i in range(NUM_COMBOS):
    ans_combo = answers(i)
    cond1.append( cond_expr1(*ans_combo) )
    cond2.append( cond_expr2(*ans_combo) )

# once tables are built, you can lookup the corresponding conditional
print cond1[ index(['yes', 'no', 'no', 'yes']) ]  # True
print cond2[ index(['yes', 'no', 'yes', 'no']) ]  # True

您可能想要解析布尔表达式?考虑建立一个解析器-一个现有的库(我爱LPL)。我正在看它,但是仍然无法通过。请给出一些代码示例好吗?如果您只需要计算一个表达式,不管是布尔表达式还是其他表达式,您可以使用
eval()
——比如说
result=eval(cond)
,其中
cond
只包含要计算的表达式,例如
cond=“'no'是'yes','no'是'no'
。您还可以使用
result.append[eval(cond)]
一次存储多个。