创建pythonic变量if-else-structure

问题描述

我正在寻找一种通过用户输入在python中创建变量if-elif-else-structures的方法我有以下示例:

measVals = {'time': [400,500,...],'volt': [12.5,12.0,'current': [500,350,...]}

静态if-elif-else结构:

if measVals['time'][0] <= 400 and measVals['volt'][0] > 8:
    if measVals['volt'][1] < 20:
        minMaxValues = {'current': {'min':0,'max': 50}}
    elif measVals['volt'][1] < 12:
        minMaxValues = {'current': {'min':0,'max': 100}}
elif measVals['time'][0] >= 500
    minMaxValues = {'current': {'min':0,'max': 500}}
    

现在,另一个用户想要根据自己的测量值定义自己的标准和minMaxValues, 因此if-elif-else的结构可能与我的完全不同。

我的目标是读取该数据作为代码的参数,因此我不必更改 在定义新条件等任何时候编写代码

                                |   
                                | measVals
User defined criteria        ___V___        
------------------------->  |       |   
                            |       |
User defined minMaxValues   |  Code |
------------------------->  |       |   
                            |_______|
                                |
                                | correct minMaxValue dict
                                V
                            

用户测量值:

measVals = {'time': [0,10,'temp': [23,23,'pressure1': [500,'pressure2': [0,20,...]}
                            

用户定义标准和相应的最小/最大值(伪代码):

    crit_1 = 'measVals['time'][0] <= 50' 
         .
         .
         .
    crit_1x = 'any condition' -> {'pressure1': {'max': 500,'min': 0},'pressure2': {'max': 50,'min': 0}}
    crit_2 = 'any condition' 
    crit = 'measVals['temp'][2] < 30'-> {'pressure1': {'max': 300,'pressure2': {'max': 0,'min': 0}}
    crit_x = 'any condition'
         .
         .
         .
    crit_xx = 'any condition' -> {'pressure1': {'max': w,'min': x},'pressure2': {'max': y,'min': z}}

所以我的想法是将用户定义的数据读入(例如,使用gui)作为嵌套字典,其最低级别的条件为键和minMaxValues,如下所示:

criteriaMinMaxDict = 
{'measVals['time'][0] <= 50': {crit_1x: {'pressure1': {'max': 500,'min': 0}}},crit_2: {'measVals['temp'][2] < 30': {'pressure1': {'max': 300,crit_x: {crit_xx: {'pressure1': {'max': w,'min': z}}}}
 

使用此字典,我必须将eval(key)与递归结合使用以获得正确的minMaxValues。 可以,但是看起来很错误,尤其是条件字符串作为键。

有人有更好的(更Python化的)方式来解决此类问题吗?

预先感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)