gpt4 book ai didi

python - 有没有一种不同的方式可以根据使用 argparse 模块输入的参数显示我的表?

转载 作者:太空宇宙 更新时间:2023-11-03 21:20:54 24 4
gpt4 key购买 nike

目标是根据终端中输入的参数显示表格。

我尝试创建一个函数,使用 if、elif 和 else 语句输出每个单独的表,但只会单独显示这些表。

我还尝试了一种首选的不同方法,它会根据我的参数向我的表添加一列。


def generate_table(inventory):
args = arguments()
data = generate_data(inventory)

main_headers = ['os_version', 'serial_number']
lldp_headers = ['lldp']
out_file = ['outfile']
main_header = []
lldp_header = []
main_table_header = PrettyTable()
lldp_table_header = PrettyTable()

for arg in vars(args):
if arg in main_headers and getattr(args, arg):
main_header.append(arg)
elif arg in lldp_headers and getattr(args, arg):
lldp_header.append(arg)
elif arg in out_file and getattr(args, arg):
out_file.append(arg)
output_file(inventory)

main_header.insert(0, 'Hostname')
main_table_header.field_names = main_header
lldp_table_header.field_names = ['Hostname', 'Neighbor', 'Local Interface', 'Neighbor Interface']

for hostname, details in data.items():
row = [hostname]
for column in main_table_header.field_names[1:]:
row.append(details[column])
main_table_header.add_row(row)
for lldp_data in details['lldp']:
neighbor = lldp_data['device-id']
local_int = lldp_data['local-interface']
neigh_int = lldp_data['connecting-interface']
lldp_table_header.add_row([hostname, neighbor, local_int, neigh_int])


print(main_table_header)
print(lldp_table_header)




def arguments():
parser = argparse.ArgumentParser(description='Argparse for Training Course.')
parser.add_argument('-s', '--serial_number', action='store_true', help='Device Serial Numbers')
parser.add_argument('-v', '--os_version', action='store_true', help='Output Devices OS')
parser.add_argument('--lldp', action='store_true', help='Output LLDP Data')
parser.add_argument('--outfile', action='store_true', help='Output to file')
parser.add_argument('--inventory', help='Inventory File', default=["inventory.yml"], required=True)
args = parser.parse_args()

return args


def get_inventory(inventory):
with open(inventory) as fh:
yml_file = yaml.load(fh)

return yml_file


def main():
args = arguments()
if not os.path.isfile(args.inventory):
sys.exit('Please specify valid, readable YAML file with data')

inventory = get_inventory(args.inventory)
generate_table(inventory)



if __name__ == '__main__':
main()
YAML FILE:

csr1:
username: admin
password: pass
transport: restconf
csr2:
username: admin
password: pass
transport: restconf

这就是我所期望的:

python3 rest5.py --inventory inventory.yml -v
+----------+------------+
| Hostname | os_version |
+----------+------------+
| csr1 | 16.6 |
| csr2 | 16.6 |
+----------+------------+

python3 rest5.py --inventory inventory.yml -s
+----------+---------------+
| Hostname | serial_number |
+----------+---------------+
| csr1 | 9KIBQAQ3OPE |
| csr2 | 9KIBQAQ3OPE |
+----------+---------------+

python3 rest5.py --inventory inventory.yml -s -v
+----------+---------------+------------+
| Hostname | serial_number | os_version |
+----------+---------------+------------+
| csr1 | 9KIBQAQ3OPE | 16.6 |
| csr2 | 9KIBQAQ3OPE | 16.6 |
+----------+---------------+------------+
python3 rest5.py --inventory inventory.yml --lldp
+----------+--------------+-----------------+--------------------+
| Hostname | Neighbor | Local Interface | Neighbor Interface |
+----------+--------------+-----------------+--------------------+
| csr1 | csr2.com | Gi1 | Gi1 |
| csr2 | csr1.com | Gi1 | Gi1 |
+----------+--------------+-----------------+--------------------+
python3 rest5.py --inventory inventory.yml --lldp -s -v
+----------+---------------+------------+
| Hostname | serial_number | os_version |
+----------+---------------+------------+
| csr1 | 9KIBQAQ3OPE | 16.6 |
| csr2 | 9KIBQAQ3OPE | 16.6 |
+----------+---------------+------------+
+----------+--------------+-----------------+--------------------+
| Hostname | Neighbor | Local Interface | Neighbor Interface |
+----------+--------------+-----------------+--------------------+
| csr1 | csr2.com | Gi1 | Gi1 |
| csr2 | csr1.com | Gi1 | Gi1 |
+----------+--------------+-----------------+--------------------+

实际输出:

python3 rest5.py --inventory inventory.yml -s
+----------+---------------+
| Hostname | serial_number |
+----------+---------------+
| csr1 | 9KIBQAQ3OPE |
| csr2 | 9KIBQAQ3OPE |
+----------+---------------+
+----------+--------------+-----------------+--------------------+
| Hostname | Neighbor | Local Interface | Neighbor Interface |
+----------+--------------+-----------------+--------------------+
| csr1 | csr2.com | Gi1 | Gi1 |
| csr2 | csr1.com | Gi1 | Gi1 |
+----------+--------------+-----------------+--------------------+

python3 rest5.py --inventory inventory.yml --lldp
+----------+
| Hostname |
+----------+
| csr1 |
| csr2 |
+----------+
+----------+--------------+-----------------+--------------------+
| Hostname | Neighbor | Local Interface | Neighbor Interface |
+----------+--------------+-----------------+--------------------+
| csr1 | csr2.com | Gi1 | Gi1 |
| csr2 | csr1.com | Gi1 | Gi1 |
+----------+--------------+-----------------+--------------------+

最佳答案

当您只想打印一个表时,您的 generate_table 方法始终打印出两个表:

你原来的功能:

def generate_table(inventory):
args = arguments()
...

print(main_table_header)
print(lldp_table_header)

应该简单地更改为:

def generate_table(inventory):
args = arguments()
...

if args.lldp:
print(lldp_table_header)
else:
print(main_table_header)

其他评论者提到了通常可以使代码变得更好的优化,您应该考虑实现这些优化,例如:

  • 仅生成参数一次

  • 仅创建要渲染的表格,而不是创建最终不打印的表格

但最终,您只需几行代码就可以让上述用例按照您希望的方式工作。

关于python - 有没有一种不同的方式可以根据使用 argparse 模块输入的参数显示我的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com