1

给定一个 eval 方程:

    eval_str = 'VAR1 > 0 and VAR1 < 10 and (VAR2 == VAR1::VALUE_X or VAR2 == VAR2::VALUE_X)'

任务:我需要用它们的实际值替换变量(本例中为 VAR1、VAR2),并用引号将给定的“常量”(VAR1::VALUE_X)括起来。

问题:由于变量名存在于常量和 eval 字符串中,并且由于变量可以替换为包含变量名本身的字符串 - 我遇到了常量值中的变量名将被另一个常量替换的问题或变量值。更好地展示...

     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)

与其通过每个变量/值递增,不如为每个变量/值使用正则表达式替换它们会更好?或者,如果有办法在每次替换后记住我在字符串中的位置,我可以修复我现有的解决方案吗?

特维姆!

4

2 回答 2

1

看起来(对我来说)这样可以通过字符串格式化更容易完成——假设你对输入字符串有一点控制:

>>> d={'action':'bar','verb':'foo'}
>>> print ("don't %(action)s a %(verb)s"%d)

或者这个怎​​么样:

import re
class DictFormatter(dict):
    def __missing__(self,k):
        return k

eval_str = '(VAR2 == VAR2::VALUE_X or VAR2 != VAR2::VALUE_Z) and (VAR1 > 0 and VAR1 < 10)'
var_dict = DictFormatter()
var_dict.update({'VAR1':'5','VAR2':'VAR1::VALUE_Y'})

extra_space = re.compile(r'[\w:]+')  #words are alphanumeric + ':' + '_'.
eval_str = extra_space.sub(lambda m: ' %s '%(m.group()),eval_str) #make sure there is space between "words"
eval_list = [var_dict[item] for item in eval_str.split()]

print " ".join(eval_list)
于 2012-07-10T13:26:08.687 回答
0

要替换VAR1离开,VAR1::VALUE_X您可以使用负前瞻:

string = re.sub(r'VAR\d(?!\:\:)', lambda m: var_dict[m.group(0)], string)

更强大的解决方案会将字符串解析为 AST 并对其进行评估。

于 2012-07-10T13:19:51.750 回答