如何将python dict父级标记为失败或基于子级传递

时间:2018-07-25 02:02:05

标签: python python-3.x

我有一个测试json报告,如果有任何失败的子节点,如何标记父节点失败,如果所有子节点都通过,如何标记父节点通过?

原始

{
    "scenario":
        {
            "server": {
                "testsuit_1": {
                    "test_get": {"outcome": "passed"},
                    "test_set": {"outcome": "failed"}
                },
                "testsui_2": {
                    "test_get": {"outcome": "passed"},
                    "test_set": {"outcome": "passed"}
                }
            },
            "client": {
                'test_receive': {"outcome": 'pass'},
                'test_send': {"outcome": 'fail'},
            }
        }
}

我必须将原始字典的字典转换为字典列表,因为没有空间可以添加新的outcome

期望

[
    {
        'node_name': 'scenario',
        'outcome': 'failed',
        'children': [
            {
                'node_name': 'server',
                'outcome': 'failed',
                'children': [
                    {
                        'node_name': 'testsuit_1',
                        'outcome': 'failed',
                        'children': [...]
                    },
                    {
                        'node_name': 'testsuit_2',
                        'outcome': 'pass',
                        'children': [...]
                    }
                ]
            },
            {
                'node_name': 'client',
                'outcome': 'pass',
                'children': [
                    {
                        'node_name': 'test_receive',
                        'outcome': 'pass',
                        'children': []
                    },
                    {
                        'node_name': 'test_send',
                        'outcome': 'pass',
                        'children': []
                    }
                ]
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:2)

您可以创建两个功能:一个用于向前看并确定用户是否失败,另一个主要功能是遍历整个结构:

def lookahead(d):
  if isinstance(d, str):
    return d == 'passed'
  if all('outcome' in b for b in d.values()):
     return all(b["outcome"] == "passed" for b in d.values())
  return all(lookahead(b) for b in d.values())

def new_struct(d):
  return [{'node_name':a, 'outcome':['failed', 'pass'][lookahead(b)] if isinstance(b, dict) and 'outcome' not in b else b['outcome'], 'children':[] if not isinstance(b, dict) or 'outcome' in b else new_struct(b)} for a, b in d.items()]

import json
d = {'scenario': {'server': {'testsuit_1': {'test_get': {'outcome': 'passed'}, 'test_set': {'outcome': 'failed'}}, 'testsui_2': {'test_get': {'outcome': 'passed'}, 'test_set': {'outcome': 'passed'}}}, 'client': {'test_receive': {'outcome': 'pass'}, 'test_send': {'outcome': 'fail'}}}}
print(json.dumps(new_struct(d), indent=4))

输出:

 [
    {
    "node_name": "scenario",
    "outcome": "failed",
    "children": [
        {
            "node_name": "server",
            "outcome": "failed",
            "children": [
                {
                    "node_name": "test_1",
                    "outcome": "failed",
                    "children": [
                        {
                            "node_name": "get",
                            "outcome": "passed",
                            "children": []
                        },
                        {
                            "node_name": "set",
                            "outcome": "failed",
                            "children": []
                        }
                    ]
                },
                {
                    "node_name": "test_2",
                    "outcome": "pass",
                    "children": [
                        {
                            "node_name": "get",
                            "outcome": "passed",
                            "children": []
                        },
                        {
                            "node_name": "set",
                            "outcome": "passed",
                            "children": []
                        }
                    ]
                }
            ]
        },
        {
            "node_name": "client",
            "outcome": "failed",
            "children": [
                {
                    "node_name": "receive",
                    "outcome": "pass",
                    "children": []
                },
                {
                    "node_name": "send",
                    "outcome": "fail",
                    "children": []
                }
            ]
        }
    ]
  }
]