python 级别联动实现

本文详细介绍了使用Python从数据库中查询系统、节点、任务及API信息的方法,并构建了一个树状结构的数据接口。通过多级联查,实现了数据的高效获取与组织。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from utils.httputil import Http
from utils.dbQry import MyDbUtil

http = Http()
mydb = MyDbUtil()


def query_interface():
    list = mydb.select_some("systems", "switch", 1)
    nodes = mydb.select_some("node", "switch", 1)
    tasks = mydb.select_some("task", "switch", 1)
    apis = mydb.select_some("apis", "switch", 1)

    if list:
        treelist = []
        for node in list:
            system = {"sys_id": node.get("id"), "sys_name": node.get("name"), "children": []}
            treelist.append(system)

        for tl in treelist:
            if nodes:
                for node in nodes:
                    if tl.get('sys_id') == node.get('systems_id'):
                        node2 = {"node_id": node.get("id"), "node_name": node.get("name"), "children": []}
                        if tasks:
                            for task in tasks:
                                if node.get("id") == task.get('node_id'):
                                    node3 = {"task_id": task.get("id"), "task_name": task.get("name"),
                                             "task_result": task.get("expected"),
                                             "expression": eval(task.get("expression")),
                                             "frequency": task.get("frequency"), "children": []}
                                    node2['children'].append(node3)
                                    if apis:
                                        for api in apis:
                                            if task.get("id") == api.get("task_id"):
                                                node4 = {"api_id": api.get("id"), "api_name": api.get("name"),
                                                         "method": api.get("method"), "url": api.get("url"),
                                                         "params": api.get("params")}
                                                node3['children'].append(node4)
                        tl['children'].append(node2)

        return treelist
    else:
        return None

    # rs = http.doHttp(method=interface.get("method"), url=interface.get("url"), data=json.dumps(interface.get("params")))
    # print("CCC",rs)


if __name__:
    rs = query_interface()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值