我正在尝试返回列表中的所有项目,以便可以使用其他函数将其显示在表中,并且还可以重复使用提取的相同数据以在另一个函数上对其进行更改。
此处的目标是从API中提取数据,该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 again\n")
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
希望在没有 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
]
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
]
答案 0 :(得分:0)
将所有提取的结果放入字典中,字典的键为仿真名称。然后在循环末尾返回该字典。
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
答案 1 :(得分:0)
使其成为发电机。代替收益,使用yield:
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