变量的值支持是列表,并且可以获取其中值,如${var[0][0]}

This commit is contained in:
Leozhanggg 2021-10-18 18:37:03 +08:00
parent e385aa81b4
commit 29cbc3406b
2 changed files with 33 additions and 9 deletions

View File

@ -25,8 +25,18 @@ def get_value(data, value):
# for key in data: # for key in data:
# __relevance = get_value(data[key], value) # __relevance = get_value(data[key], value)
for key in data: for key in data:
if isinstance(data[key], dict) or isinstance(data[key], list): if isinstance(data[key], dict):
get_value(data[key], value) get_value(data[key], value)
elif isinstance(data[key], list):
for each in data[key]:
if isinstance(each, dict):
get_value(each, value)
# for each in data[key]:
# if isinstance(each, dict):
# break
else:
if key == value:
__relevance.append(data[key])
else: else:
if key == value: if key == value:
__relevance.append(data[key]) __relevance.append(data[key])
@ -54,8 +64,11 @@ def get_relevance(data, relevance_list, relevance=None):
# 去除参数[n]标识 # 去除参数[n]标识
for index, value in enumerate(relevance_list): for index, value in enumerate(relevance_list):
mark = re.findall(r"\[\-?[0-9]*\]", value) mark = re.findall(r"\[\-?[0-9]*\]", value)
if mark: # if mark:
relevance_list[index] = value.strip(mark[0]) # relevance_list[index] = value.strip(mark[0])
for m in mark:
value = value.replace(m, '')
relevance_list[index] = value
# 去除重复参数 # 去除重复参数
relevance_list = list(set(relevance_list)) relevance_list = list(set(relevance_list))

View File

@ -66,21 +66,32 @@ def replace_relevance(param, relevance=None):
# 关联参数多值时指定索引值替换 # 关联参数多值时指定索引值替换
mark = re.findall(r"\[\-?[0-9]*\]", each) mark = re.findall(r"\[\-?[0-9]*\]", each)
# 判断关联参数是否指定索引值var[n] # 判断关联参数是否指定索引值var[n]
if mark: if len(mark)==0:
if isinstance(relevance[each], list):
value = relevance[each][0]
else:
value = relevance[each]
elif len(mark)==1:
var = each.strip(mark[0]) var = each.strip(mark[0])
n = int(mark[0].strip('[').strip(']')) n = int(mark[0].strip('[').strip(']'))
value = relevance[var][n] value = relevance[var][n]
each = each.replace('[', '\[').replace(']', '\]') each = each.replace('[', '\[').replace(']', '\]')
else: else:
if isinstance(relevance[each], list): var = each
value = relevance[each][0] for m in mark:
else: var = var.replace(m, '')
value = relevance[each] n1 = int(mark[0].strip('[').strip(']'))
n2 = int(mark[1].strip('[').strip(']'))
value = relevance[var][n1][n2]
each = each.replace('[', '\[').replace(']', '\]')
# 生成正在表达式并替换关联参数 # 生成正在表达式并替换关联参数
pattern = re.compile('\${' + each + '}') pattern = re.compile('\${' + each + '}')
try: try:
param = re.sub(pattern, value, param) if param.strip('${' + each + '}'):
param = re.sub(pattern, str(value), param)
else:
param = re.sub(pattern, value, param)
except TypeError: except TypeError:
param = value param = value
except KeyError: except KeyError: