Dynamically evaluating simple boolean logic in Python

Posted by a paid nerd on Stack Overflow See other posts from Stack Overflow or by a paid nerd
Published on 2010-03-18T04:49:55Z Indexed on 2010/03/18 4:51 UTC
Read the original article Hit count: 483

Filed under:
|
|
|
|

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

  • (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. 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?

Suggestions welcome.

© Stack Overflow or respective owner

Related posts about python

Related posts about boolean