Reputation: 164
Is this possible? What I want to do is something like this: sympify('2>1 | 2<1')
I pretty much only need 'or' and 'and'. I also tried doing sympify('2>1') | sympify('2<1')
. Ideally I would want to just be able to send in a string with relations and logical operators to a function and have it return true or false. (i.e. '(2>1 | 2<1) & 3==3'
)
Upvotes: 2
Views: 243
Reputation: 176750
The problem is operator precedence in Python/SymPy: you need to surround the inequalities with parentheses otherwise 1 | 2
is evaluated first and an error is raised by SymPy. You can write:
>>> sympify('(2>1) | (2<1)')
True # SymPy bool
Of course, you don't really need SymPy's power for logical expressions just involving &
and |
. Python's bool
type supports the operators although you still need parentheses to ensure the correct result:
>>> (2>1) | (2<1)
True # Python bool
Upvotes: 2