From d9ddefe5dbbd48eb6162039dce9fa84aa4168013 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Thu, 7 Dec 2023 09:37:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20base=20=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E5=AF=B9=E6=AF=94=E5=AD=97=E5=85=B8=E5=8F=8A=E5=85=B6=E5=B5=8C?= =?UTF-8?q?=E5=A5=97=E5=86=85=E5=AE=B9=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/utils/tools.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/apps/utils/tools.py b/apps/utils/tools.py index 18dc6661..7033a83d 100755 --- a/apps/utils/tools.py +++ b/apps/utils/tools.py @@ -9,10 +9,11 @@ import requests from io import BytesIO from rest_framework.serializers import ValidationError + def tran64(s): missing_padding = len(s) % 4 if missing_padding != 0: - s = s+'='* (4 - missing_padding) + s = s+'=' * (4 - missing_padding) return s @@ -202,4 +203,37 @@ def check_phone_e(phone): re_phone = r'^1\d{10}$' if not re.match(re_phone, phone): raise ValidationError('手机号格式错误') - return phone \ No newline at end of file + return phone + + +def compare_dicts(dict1, dict2, ignore_order=False): + if ignore_order: + for key in sorted(dict1.keys()): + if key not in dict2 or not compare_values(dict1[key], dict2[key], ignore_order): + return False + return True + else: + return dict1 == dict2 + + +def compare_lists_of_dicts(list1, list2, ignore_order=False): + """比较两个列表,这里的列表包含字典(对象)""" + if ignore_order: + # 转换列表中的字典为元组列表,然后排序进行比较 + sorted_list1 = sorted((tuple(sorted(d.items())) for d in list1)) + sorted_list2 = sorted((tuple(sorted(d.items())) for d in list2)) + return sorted_list1 == sorted_list2 + else: + # 按顺序比较列表中的字典 + return all(compare_dicts(obj1, obj2) for obj1, obj2 in zip(list1, list2)) + + +def compare_values(val1, val2, ignore_order=False): + """通用比较函数,也可以处理字典和列表。""" + if isinstance(val1, list) and isinstance(val2, list): + # 假设这里我们关心列表中对象的顺序 + return compare_lists_of_dicts(val1, val2, ignore_order) + elif isinstance(val1, dict) and isinstance(val2, dict): + return compare_dicts(val1, val2, ignore_order) + else: + return val1 == val2