发布于2024-11-30 14:05 阅读(1009) 评论(0) 点赞(13) 收藏(3)
我正在尝试使用PyHamcrest
python 库来验证嵌套字典(子图)是否是另一个更大的嵌套字典(完整图)的子集。
在下面的例子中,我尝试验证子图 json的节点子集(及其属性子列表)是否存在于完整图json的所有节点列表(及其所有属性列表)中。
以下示例数据供参考:
子图json 是
{
"nodes": [{
"properties": [{
"key": "key11",
"value": ["val11", "val12"]
}, {
"key": "key12",
"value": "val21"
}],
"types": ["t11", "t12"]
}]
}
完整的图表json是(简化的)
{
"graph": {
"id": "graph_id",
"timestamp": "str_of_time",
"nodes": [{
"id": "node_id1",
"properties": [{
"key": "key11",
"value": ["val11", "val12"]
}, {
"key": "key12",
"value": "val21"
}, {
"key": "key13",
"value": "val31"
} {
"key": "key14",
"value": ["val41", "val42"]
}],
"types": ["t11", "t12", "t13", "t14"]
},
{
"id": "node_id2",
"properties": [{
"key": "key21",
"value": ["val21", "val22"]
}, {
"key": "key22",
"value": "val23"
}],
"types": ["t21", "t22"]
},
...and a lot of other nodes....
],
"edges": [{
"id": "edge_id1",
"properties": [],
"type": "type1",
"source": {
"id": "source_id1",
"resolutionType": "source_res1"
},
"target": {
"id": "target_id1",
"resolutionType": "target_res1"
}
}, {
"id": "edge_id2",
"properties": [{
"key": "key1",
"value": "val1"
}, {
"key": "key2",
"value": "val2"
}],
"type": "type2",
"source": {
"id": "source_id2",
"resolutionType": "source_res2"
},
"target": {
"id": "target_id2",
"resolutionType": "target_res2"
}
}
...and a lot of other edges....
]
}
}
这是我尝试过的但是它失败了因为它需要一个完整的字典列表而不仅仅是子字典。
assert_that(
full_graph["graph"]["nodes"],
has_items(sub_graph["nodes"])
)
我收到错误
AssertionError:
Expected: (a sequence containing <[list of ALL nodes in sub graph]>)
but: a sequence containing <[list of ALL nodes of full graph]>. # how do I set this to only list of expected nodes and NOT ALL nodes in full graph?
有关结构的附加信息(如果它不是来自上面的示例 json)
graph
- 字典
id
- 字符串timestamp
- 字符串nodes
- 列表dicts
edges
- 列表dicts
node
id
-> 字符串types
-> str 列表properties
-> 字典列表edge
id
-> 字符串type
-> 字符串properties
-> 字典列表source
-> 字典target
-> 字典所以,我可能以错误的方式使用了该库,并且无法弄清楚如何检查子图 json 是否是完整图 json 的子集。
问候,这很复杂
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/2046139/daa839f939a6d7285e19/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!