I am working with Grammatical Evolution (GE) on Python 3.7. My grammar generates executable strings in the format:
np.where(<variable> <comparison_sign> <constant>, (<probability1>), (<probability2>))
Yet, the string can get quite complex, with several chained np.where
.
<constant>
in some cases contains leading zeros, which makes the executable string to generate errors. GE is supposed to generate expressions containing leading zeros, however, I have to detect and remove them.
An example of a possible solution containing leading zeros:
"np.where(x < 02, np.where(x > 01.5025, (0.9), (0.5)), (1))"
Problem:
- There are two types of numbers containing leading zeros: int and float.
- Supposing that I detect "02" in the string. If I replace all occurrences in the string from "02" to "2", the float "01.5025" will also be changed to "01.525", which cannot happen.
I've made several attempts with different re
patterns, but couldn't solve it.
To detect that an executable string contains leading zeros, I use:
try:
_ = eval(expression)
except SyntaxError:
new_expression = fix_expressions(expression)
I need help building the fix_expressions
Python function.