动态评估在Python简单布尔逻辑 [英] Dynamically evaluating simple boolean logic in Python

查看:139
本文介绍了动态评估在Python简单布尔逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些动态生成的布尔逻辑EX pressions,如:

I've got some dynamically-generated boolean logic expressions, like:


  • (A或B)和(C或D)

  • A或(A和B)

  • A

  • 空 - 值为True

  • (A or B) and (C or D)
  • A or (A and B)
  • A
  • empty - evaluates to True

占位符被替换为布尔值。如若我,

The placeholders get replaced with booleans. Should I,


  1. 转换此信息到Python前pression像 True或(TRUE或FALSE)评估呢?

  2. 创建一个二叉树,其中一个节点是一个布尔连词 / 析取对象,并递归评价它?

  3. 转换入套S-EX pressions并使用Lisp语言解析器?

  4. 别的东西吗?

  1. Convert this information to a Python expression like True or (True or False) and eval it?
  2. Create a binary tree where a node is either a bool or Conjunction/Disjunction object and recursively evaluate it?
  3. Convert it into nested S-expressions and use a Lisp parser?
  4. Something else?

建议表示欢迎。

推荐答案

这应该不难在所有写一个计算器,可以解决这个问题,例如使用的 pyparsing 。你只需要一些操作来处理(与,或,和分组?),所以你应该能够自己分析和评估。

It shouldn't be difficult at all to write a evaluator that can handle this, for example using pyparsing. You only have a few operations to handle (and, or, and grouping?), so you should be able to parse and evaluate it yourself.

您不应该需要明确形成二叉树评估前pression。

You shouldn't need to explicitly form the binary tree to evaluate the expression.

这篇关于动态评估在Python简单布尔逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆