[英]Operations on nested dicts and lists
I have file containing test results in the following format: 我的文件包含以下格式的测试结果:
TEST1=passed
TEST2=failed
TEST3=passed
I want to compare the tests with it's scope: 我想将测试与它的范围进行比较:
test_scope = {
'SCOPE1': [
'TEST1',
'TEST2',
],
'SCOPE2': [
'TEST3',
],
'SCOPE3': [
'TEST4',
'TEST5',
],
'SCOPE4': [
'TEST6',
'TEST7',
],
}
The ideal output would be: 理想的输出为:
test_results = {
"SCOPE1": [
{
"name": "TEST1",
"result": "passed"
"id": 0
},
{
"name": "TEST2",
"result": "failed"
"id": 1
},
],
"SCOPE2": [
{
"name": "TEST3",
"result": "passed"
"id": 1
},
],
"SCOPE3": [
{
"name": "TEST4",
"result": "not run"
"id": 1
},
{
"name": "TEST5",
"result": "not run"
"id": 1
},
]
"SCOPE4": [
{
"name": "TEST6",
"result": "not run"
"id": 0
},
{
"name": "TEST7",
"result": "not run"
"id": 1
},
],
}
where id
is the testname index in the list in test_scope
. 其中
id
是test_scope
中列表中的testname索引。
What I have achieved: 我所取得的成就:
class TBD(object):
def get_test_results(self, path, results_file):
test_results = []
with open('path', 'r') as results_file:
lines = results_file.readlines()
for line in lines:
test_case = line.rstrip().split('=')[0]
test_result = line.rstrip().split('=')[1]
for section, tests in test_scope.iteritems():
if test_case in tests:
section = section
id = tests.index(test_case)
test_results.append({'name': test_case,
'result': test_result,
'section': section,
'id': id)
return test_results
With output: 输出:
[{'id': 0,
'section': 'SCOPE1',
'name': 'TEST1',
'result': 'passed'},
...]
However I am stuck and don't know how to get the test cases with not run results (those present in test_scope
but not in test_results
). 但是我被困住了,不知道如何获得没有运行结果的测试用例(那些出现在
test_scope
而不是test_results
)。
Any advice on how to progress from here? 关于如何从这里前进的任何建议? Of course the data structure can be freely changed if there is easier way to store it, as I am quite inexperienced yet :)
当然,如果有更简单的存储方法,则可以自由更改数据结构,因为我还没有经验:)
Rather than drive your output by the test results file, split reading the test results and outputting your test_results
data structure into two separate steps. 而不是通过测试结果文件来驱动输出,而是将读取测试结果和输出
test_results
数据结构分为两个单独的步骤。
In step one, read the file, and store the information into a dictionary mapping test name -> result. 在第一步中,读取文件,并将信息存储到字典映射测试名称->结果中。 In step two, produce the
test_results
output by walking the test_scope
data structure, using the dictionary produced by step 1 to determine test outcomes. 在第二步中,使用步骤1生成的字典来确定测试结果,通过遍历
test_scope
数据结构来生成test_results
输出。 This makes it trivial to include all entries from test_scope
, even if the test was not part of the test run. 这使得包括
test_scope
所有条目变得微不足道,即使该测试不是测试运行的一部分。
# step 1, read the results file
results = {}
with open('path', 'r') as results_file:
results = dict(line.strip().split('=') for line in results_file if line.strip())
# step 2, build the output
test_results = {
scope: [
{
"id": i,
"name": test_name,
"result": results.get(test_name, "not run"),
} for i, test_name in enumerate(tests)
] for scope, tests in test_scope.iteritems()
}
Demo: 演示:
>>> from io import BytesIO
>>> results_file_data = '''\
... TEST1=passed
... TEST2=failed
... TEST3=passed
... '''
>>> with BytesIO(results_file_data) as results_file:
... results = dict(line.strip().split('=') for line in results_file if line.strip())
...
>>> test_results = {
... scope: [
... {
... "id": i,
... "name": test_name,
... "result": results.get(test_name, "not run"),
... } for i, test_name in enumerate(tests)
... ] for scope, tests in test_scope.iteritems()
... }
>>> from pprint import pprint
>>> pprint(test_results)
{'SCOPE1': [{'id': 0, 'name': 'TEST1', 'result': 'passed'},
{'id': 1, 'name': 'TEST2', 'result': 'failed'}],
'SCOPE2': [{'id': 0, 'name': 'TEST3', 'result': 'passed'}],
'SCOPE3': [{'id': 0, 'name': 'TEST4', 'result': 'not run'},
{'id': 1, 'name': 'TEST5', 'result': 'not run'}],
'SCOPE4': [{'id': 0, 'name': 'TEST6', 'result': 'not run'},
{'id': 1, 'name': 'TEST7', 'result': 'not run'}]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.