I generate a conditional statement using python's (2.7) eval()
function like so:
my_list = ['2 > 1','3 > 2','4 > 3']
if eval('(' + ') or ('.join(my_list) + ')'):
print 'yes'
else:
print 'no'
In my case, the list is generated by code, my_list comes from a parameter file, and the list is joined with 'or' statements in the conditional expression. The code above prints 'yes'.
It works fine for small lists, but at a certain number of characters in the eval()
statement and I get a string error.
Some searching finds these threads that point to a bug:
But their max eval() size is much larger than what I found. In my case, I find between 1744 and 1803 characters does the issue begin. I tried this code and it does crash between the two statements
>>> eval("1.0*"*10000+"1.0")
1.0
>>> eval("1.0*"*100000+"1.0")
# segfault here
So, that brings me back to think that it is not eval(), but actually some max on the if
statement.
What's another way to conditionally apply the rules in the list that doesn't involve long strings and the eval() function?
Interestingly, I made my_list much bigger:
my_list = ['2 > 1']*1000000
and the code works fine...