I have a series of conditional expressions as strings like: "('a' AND ('b' OR 'c')) OR ('d' AND ('e' OR ('g' AND 'h')))"
which are nested to a non-predictable depth. I have no control over this data; I need to be able to work with it.
I'd like python to be able to understand these structures so that I can manipulate them .
I've tried creating nested python lists by breaking at the parentheses (using pyparsing or unutbu's/falsetru's parser):
>>> str1="('a' AND ('b' OR 'c')) OR ('d' AND ('e' OR ('g' AND 'h')))"
>>> var1=parse_nested(text, r'\s*\(', r'\)\s*')
>>> var1
[["'a' AND", ["'b' OR 'c'"]], 'OR', ["'d' AND", ["'e' OR", ["'g' AND 'h'"]]]]
...But then to be honest I don't know how to get python to interpret the AND/OR relationships between the objects. I feel like i'm going completely the wrong direction with this.
My ideal output would be to be a data structure that maintains the relationship types between the entities in a nested way, so that (for example) it would be easy to create a json or YAML:
OR:
- AND:
- a
- OR:
- b
- c
- AND:
- d
- OR:
- e
- AND:
- g
- h