它可能与正则表达式断言?
示例:dict
{
"mimetype": "application/json",
"status_code": 200,
"data": {
"id": 1,
"username": "foo",
"access_token": "5151818748748"
}
}
使用:正则表达式 access_token
{
"mimetype": "application/json",
"status_code": 200,
"data": {
"id": 1,
"username": "foo",
"access_token": "(.+)"
}
}
假设我正确理解了您:
import re
def assert_dict(template, thing):
if len(template) != len(thing):
raise AssertionError("Assertion failed")
for key in template:
if isinstance(template[key], dict):
assert_dict(template[key], thing[key])
else:
if template[key] == thing[key]:
continue
elif re.fullmatch(template[key], thing[key]):
continue
else:
raise AssertionError("Assertion failed")
这将检查它们是否具有相同的键值对,如果是,则首先检查它们是否相同,如果不相同,则检查第二个值是否与第一个匹配。
只要字典中没有任何花哨的内容,这将起作用。列表将起作用,但列表中的命令不起作用,尽管实现它也是相当琐碎的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句