美文网首页
字典菜单级联结构

字典菜单级联结构

作者: 高峥 | 来源:发表于2022-05-21 18:18 被阅读0次
    #!/usr/bin/env python3.8
    # _*_ coding: utf-8 _*_
    # Description:
    # Author: gaozengzeng <15383479200@168.com>
    # Date: 2022/5/21
    
    res = {
        "json": [
            "rigid",
            "better for data interchange"
        ],
        "yaml": [
            "slim and flexible",
            "better for configuration"
        ],
        "object": {
            "key": "value",
            "shen": {
                "null_value": None,
                "deep_1": [],
                "deep_2": {
                    "boolean": True,
                    "deep_3": {
                        "null_value": None,
                        "null": "123"
                    }
                },
            },
            "array": [
                {
                    "null_value": None
                },
                {
                    "boolean": True
                },
                {
                    "integer": 1
                }
            ]
        },
        "paragraph": "Blank lines denote\nparagraph breaks\n",
        "content": "Or we\ncan auto\nconvert line breaks\nto save space"
    }
    
    
    def func_menu(key="", res=None, ll=None):
        for k, v in res.items():
            k_format = "^".join([key, k]) if key else k
            if not isinstance(v, dict):
                if isinstance(v, list):
                    ll.append({k_format: "list"})
                elif isinstance(v, str):
                    ll.append({k_format: "string"})
                elif isinstance(v, bool):
                    ll.append({k_format: "bool"})
                elif isinstance(v, float):
                    ll.append({k_format: "float"})
                elif isinstance(v, type(None)):
                    ll.append({k_format: "null"})
            else:
                ll.append({k_format: "dict"})
                func_menu(k_format, v, ll)
        return ll
    
    
    def func_children(key="", res=None, ll=None):
        for k, v in res.items():
            k_format = "^".join([key, k]) if key else k
            if not isinstance(v, dict):
                if isinstance(v, list):
                    ll.append({"type": "list", "label": k_format})
                elif isinstance(v, str):
                    ll.append({"type": "string", "label": k_format})
                elif isinstance(v, bool):
                    ll.append({"type": "bool", "label": k_format})
                elif isinstance(v, float):
                    ll.append({"type": "float", "label": k_format})
                elif isinstance(v, type(None)):
                    ll.append({"type": "null", "label": k_format})
            else:
                ll.append({"type": "dict", "label": k_format, "chlidren": func_children(k_format, v, [])})
        return ll
    
    
    if __name__ == '__main__':
    
        print(func_children(res=res, ll=[]))
        print(func_menu(res=res, ll=[]))
    
    

    相关文章

      网友评论

          本文标题:字典菜单级联结构

          本文链接:https://www.haomeiwen.com/subject/wvncprtx.html