python - Sympify returning different expression for same input -


i have list of strings need parse sympy expressions. problem is, if 1 of them raises sympifyerror exception, following string not return same sympy expression identical string returned before exception.

for example, following code:

import traceback  sympy.core import sympify sympy.core.evaluate import evaluate sympy.core.sympify import sympifyerror  if __name__ == '__main__':      equations = ['eq(sin(pi/6), x/10)', 'eq(x, 3))', 'eq(sin(pi/6), x/10)']      equation in equations:          try:             evaluate(false):                 expr = sympify(equation)              print(expr)          except sympifyerror:             traceback.print_exc() 

outputs:

traceback (most recent call last): eq(sin(pi/6), x/10)   file "c:\users\vini_\anaconda3\lib\site-packages\sympy\core\sympify.py", line 354, in sympify eq(sin(pi/6**1), x/10**1)     expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)   file "c:\users\vini_\anaconda3\lib\site-packages\sympy\parsing\sympy_parser.py", line 889, in parse_expr     code = stringify_expr(s, local_dict, global_dict, transformations)   file "c:\users\vini_\anaconda3\lib\site-packages\sympy\parsing\sympy_parser.py", line 791, in stringify_expr     toknum, tokval, _, _, _ in generate_tokens(input_code.readline):   file "c:\users\vini_\anaconda3\lib\site-packages\sympy\parsing\sympy_tokenize.py", line 384, in generate_tokens     raise tokenerror("eof in multi-line statement", (lnum, 0)) sympy.parsing.sympy_tokenize.tokenerror: ('eof in multi-line statement', (2, 0))  during handling of above exception, exception occurred:  traceback (most recent call last):   file "c:/users/vini_/pycharmprojects/bugfix_simpify/main.py", line 22, in <module>     expr = sympify(equation)   file "c:\users\vini_\anaconda3\lib\site-packages\sympy\core\sympify.py", line 356, in sympify     raise sympifyerror('could not parse %r' % a, exc) sympy.core.sympify.sympifyerror: sympify of expression 'could not parse 'eq(x, 3))'' failed, because of exception being raised: tokenerror: ('eof in multi-line statement', (2, 0)) 

note first , third strings identical, output different (there's additional **1. second 1 intentionally incorrect (unbalanced parenthesis). i'm using evaluate(false) line because need expression close possible original string.

what doing wrong? there way fix output same identical input strings?

environment: python 3.6.1; sympy 1.1.1

thanks in advance.

your problem vanishes when move context manager evaluate outside tryexcept expression:

import traceback  sympy.core import sympify sympy.core.evaluate import evaluate sympy.core.sympify import sympifyerror  equations = ['eq(sin(pi/6), x/10)', 'eq(x, 3))', 'eq(sin(pi/6), x/10)']  equation in equations:     evaluate(false):         try:             expr = sympify(equation)         except sympifyerror:             traceback.print_exc()         else:             print(expr) 

while should have done anyway (always put little possible in try clause), should not have encountered problem, seems bug – probably related context manager evaluate not being closed. here simpler script (obtained reducing yours) producing similar problem:

from sympy import sympify, evaluate sympy.abc import x,y  try:     evaluate(false):         sympify(")") except:     pass  (x+y).simplify() 

i reported bug.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -