Given an equation for eval:
eval_str = 'VAR1 > 0 and VAR1 < 10 and (VAR2 == VAR1::VALUE_X or VAR2 == VAR2::VALUE_X)'
Task: I need to replace variables (VAR1,VAR2 in this example), with their actual values, and also surround the given ‘constant’ (VAR1::VALUE_X) with quotes.
Problem: Since the variable name exists in the constant and in the eval string, and since the variable could replaced with a string that contains the variable name itself – I run into issues where the variable name in the constant value will be replaced by another constant or variable value. Better shown…
eval_str = '(VAR2 == VAR2::VALUE_X or VAR2 != VAR2::VALUE_Z) and (VAR1 > 0 and VAR1 < 10)'
var_dict = {'VAR1':'5','VAR2':'VAR1::VALUE_Y'}
# what I do is strip out most of the special characters
# the following findall = ['VAR1', '0', 'and', 'VAR1', '10', 'and', 'VAR2', 'VAR1::VALUE_X', 'or', 'VAR2', 'VAR2::VALUE_X']
for x in re.findall('[^\s()<>=!]+',eval_str):
# skip digits, and, or, None *need to improve regex
if x.replace('.','').isdigit() or x == 'and' or x == 'or' or x == 'None':
continue
# if variable is in dict
if x in var_dict.keys():
# only replace first
eval_str = eval_str.replace(x,var_dict[x],1)
# if is constant
elif x.__contains__('::'):
eval_str = eval_str.replace(x,'\'%s\''%x,1)
print eval_str
# (5::VALUE_Y == '5::VALUE_Y::VALUE_X' or VAR2 != 'VAR2::VALUE_Z') and (VAR1 > 0 and VAR1 < 10)
Instead of incrementing through each variable/value, perhaps it would be better to replace them all using a regex for each one? Or could I fix my existing solution if there was a way to remember my position in the string after each replace?
TYVM!
To replace
VAR1leaving alongVAR1::VALUE_Xyou could use negative lookahead:A more robust solution would parse the string into an AST and eval it.