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



我正在尝试返回列表中的所有项目,以便我可以使用不同的函数将其显示在表中,并且还可以重用提取的相同数据以在另一个函数上对其进行更改。

此处的目标是从包含有关仿真/Pod 信息的 API 中提取数据。有了这些数据,我正在生成漂亮的表格供用户查看,我也使用它来对其进行更改。一些更改是启用/禁用仿真以及启用/禁用每个仿真中存在的设备。

当前功能:

这个当前的函数工作得很好,但它需要重构,因为我需要能够在emulation_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 againn")
view_emulation_info()
user_input = input(input_message())

当我尝试这样做时,它只返回列表中的一个元素,而不是像我在函数内操作数据时那样返回我的所有元素get_emulation_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
]

我实际看到的返回语句:

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
]

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

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

使其成为生成器。使用收益率,而不是回报:

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

最新更新