From 22ff2dcf4095a6d17c40897a4722cc344d433517 Mon Sep 17 00:00:00 2001 From: amazingTest <523314409@qq.com> Date: Thu, 26 Sep 2019 09:58:12 +0800 Subject: [PATCH] =?UTF-8?q?[feat](common)=E6=96=B0=E5=A2=9E=E3=80=8C?= =?UTF-8?q?=E6=9F=A5=E6=89=BE=E5=88=97=E8=A1=A8=E4=B8=AD=E6=BB=A1=E8=B6=B3?= =?UTF-8?q?=E7=89=B9=E5=AE=9A=E6=9D=A1=E4=BB=B6=E7=9A=84=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E7=9A=84=E5=80=BC=E3=80=8D=E7=9A=84=E5=8F=98=E9=87=8F=E5=AE=9A?= =?UTF-8?q?=E4=BD=8D=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/common.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/backend/utils/common.py b/backend/utils/common.py index fe52a07..2aa4f5d 100644 --- a/backend/utils/common.py +++ b/backend/utils/common.py @@ -234,13 +234,33 @@ def dict_get(dic, locators, default=None): except KeyError: return default continue - if isinstance(value, list): + if isinstance(value, list) and len(value) > 0: if can_convert_to_int(locator): try: value = value[int(locator)] except IndexError: return default continue + elif is_specific_search_by_dict_value(locator) and all([isinstance(v, dict) for v in value]): + first_equal_index = locator.index('=') + last_dot_index = locator.rindex('.') + matched_key_re = locator[:first_equal_index] # 字典中存在满足的正则条件的键 + matched_value_re = locator[first_equal_index + 1:last_dot_index] # matched_key对应的值需要满足的正则条件 + needed_value_key = locator[last_dot_index + 1:] # 满足正则条件的字典中待取的值的键 + + for dic in value: + for k, v in dic.items(): + if re.match(matched_key_re, str(k)) and re.match(matched_value_re, str(v)): + needed_value = dic.get(needed_value_key) + value = needed_value + break + else: + continue + break + else: + return default + + continue elif locator == 'random': try: value = value[random.randint(0, len(value) - 1)] @@ -251,6 +271,13 @@ def dict_get(dic, locators, default=None): return value +def is_specific_search_by_dict_value(expression): + if re.match(r'(.)+=(.)+\.(.)+', expression): + return True + else: + return False + + def is_slice_expression(expression): if re.match('(-?\d+)?:(-?\d+)?', expression): return True @@ -541,4 +568,3 @@ def get_random_key(digit_num=16): if __name__ == '__main__': pass - -- GitLab