如何返回数据结构中的所有项?

2024-05-29 07:46:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图返回列表中的所有项,以便使用不同的函数将其显示在表中,并重用提取的相同数据在另一个函数中对其进行更改。你知道吗

这里的目标是从包含仿真/pod信息的API中提取数据。有了这些数据,我生成了漂亮的表供用户查看,我还使用它对其进行更改。其中一些更改是启用/禁用仿真以及启用/禁用每个仿真中存在的设备。你知道吗

当前函数:

这个当前的函数工作得很好,但它需要重构,因为我需要能够在仿真\u extraction中重用相同的数据来打印一个表,并对该数据进行更改。你知道吗

    def get_emulation_id():
        """This will get_emulation data and based on the emulation name chosen
        by the user it will extract the data by using emulation id and format a
        new structure so it can build a table.
        """
        emulation_name = input(input_message(msg="Please ENTER Emulation name: "))
        emulation_found = False
        for data in get_emulation():
            if emulation_name in data["name"]:
                emulation_found = True
                emulation = tesuto.apis.Emulation.get(data["id"]).data
                emulation_extraction = []
                for device in emulation.devices:
                    # tesuto.console.output(device)
                    emulation_extraction.append(
                        {
                            "name": device["name"],
                            "model_name": device["model_name"],
                            "version_name": device["version_name"],
                            "enabled/disabled": device["is_enabled"],
                        }
                    )
                headers = ["name", "model_name", "version_name", "enabled/disabled"]
                pretty_table = get_pretty_table(headers, emulation_extraction)

                print("\nEmulation name: ", data["name"], "\n")
                print(pretty_table.get_string())

        if not emulation_found:
            print("\nInvalid emulation name, Try again\n")
            view_emulation_info()

        user_input = input(input_message())

当我尝试执行此操作时,它只返回列表中的一个元素,而不是所有元素,就像我在get\u emulation\u id函数中处理数据时一样:

    def get_emulation_id():
        """This will get_emulation data and based on the emulation name chosen
        by the user it will extract the data by using emulation id and format a
        new structure so it can build a table.
        """
        emulation_name = input(input_message(msg="Please ENTER Emulation name: "))
        emulation_found = False
        for data in get_emulation():
            if emulation_name in data["name"]:
                emulation_found = True
                emulation = tesuto.apis.Emulation.get(data["id"]).data
                emulation_extraction = []
                for device in emulation.devices:
                    # tesuto.console.output(device)
                    emulation_extraction.append(
                        {
                            "name": device["name"],
                            "model_name": device["model_name"],
                            "version_name": device["version_name"],
                            "enabled/disabled": device["is_enabled"],
                        }
                    )

                print("\nEmulation name: ", data["name"], "\n")
               # print(print_device_table().get_string())
                print(json.dumps(emulation_extraction, indent=4))

                return emulation_extraction

期望看到的和我看到的没有返回语句:

Please ENTER Emulation name: pod41

Emulation name:  pod410 

[
    {
        "name": "nxos-spine1",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "nxos-spine2",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "csr1",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr2",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr3",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    ....omitted
]

Emulation name:  pod411 

[
    {
        "name": "nxos-spine1",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "nxos-spine2",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "csr1",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr2",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr3",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
   ....omitted
]

Emulation name:  pod412 

[
    {
        "name": "nxos-spine1",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "nxos-spine2",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "csr1",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr2",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr3",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "vmx1",
        "model_name": "MX",
        "version_name": "18.2R1.9",
        "enabled/disabled": false
   ....omitted
]

Emulation name:  pod413 

[
    {
        "name": "nxos-spine1",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "nxos-spine2",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "csr1",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr2",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr3",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    ....omitted
]

我在return语句中实际看到的内容:

Please ENTER Emulation name: pod41

Emulation name:  pod410 

[
    {
        "name": "nxos-spine1",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "nxos-spine2",
        "model_name": "NX-OSv",
        "version_name": "7.0.3.I7.4",
        "enabled/disabled": false
    },
    {
        "name": "csr1",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr2",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    {
        "name": "csr3",
        "model_name": "CSR1000v",
        "version_name": "16.8.1",
        "enabled/disabled": false
    },
    ....omitted
]

Tags: namefalsedatagetmodelversiondeviceenabled
2条回答

把它做成发电机。使用收益率代替收益率:

def get_emulation_id():
        """This will get_emulation data and based on the emulation name chosen
        by the user it will extract the data by using emulation id and format a
        new structure so it can build a table.
        """
        emulation_name = input(input_message(msg="Please ENTER Emulation name: "))
        emulation_found = False
        for data in get_emulation():
            if emulation_name in data["name"]:
                emulation_found = True
                emulation = tesuto.apis.Emulation.get(data["id"]).data
                emulation_extraction = []
                for device in emulation.devices:
                    # tesuto.console.output(device)
                    yield {
                              "name": device["name"],
                              "model_name": device["model_name"],
                              "version_name": device["version_name"],
                              "enabled/disabled": device["is_enabled"],
                          }

这允许您调用如下函数

result_list = [thing for thing in get_emulation_id()]

或者像这样懒洋洋地重复

for result in get_emulation_id():
    # do something with result

将所有提取的结果放在一个字典中,该字典的键是仿真名称。然后在循环结束时返回字典。你知道吗

def get_emulation_id():
    """This will get_emulation data and based on the emulation name chosen
    by the user it will extract the data by using emulation id and format a
    new structure so it can build a table.
    """
    emulation_name = input(input_message(msg="Please ENTER Emulation name: "))
    extracted_emulations = {}
    for data in get_emulation():
        if emulation_name in data["name"]:
            emulation = tesuto.apis.Emulation.get(data["id"]).data
            emulation_extraction = [{
                    "name": device["name"],
                    "model_name": device["model_name"],
                    "version_name": device["version_name"],
                    "enabled/disabled": device["is_enabled"],
                } for device in emulation.devices]

            extracted_emulations[data["name"]] = emulation_extraction

    return extracted_emulations

相关问题 更多 >

    热门问题