draw_network_graph.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:pyneng-examples-exercises 作者: natenka 项目源码 文件源码
def draw_topology(topology_dict, out_filename='img/topology', style_dict=styles):
    '''
    topology_dict - ??????? ? ????????? ?????????

    ?????? ??????? topology_dict:
        {('R4', 'Eth0/1'): ('R5', 'Eth0/1'),
         ('R4', 'Eth0/2'): ('R6', 'Eth0/0')}

    ????????????? ?????????:
    [ R5 ]-Eth0/1 --- Eth0/1-[ R4 ]-Eth0/2---Eth0/0-[ R6 ]

    ??????? ?????????? ?????????, ? ??????? svg.
    ? ?????????? ???? topology.svg ? ??????? img.
    '''
    nodes = set([item[0]
                 for item in list(topology_dict.keys())
                           + list(topology_dict.values())])

    graph = gv.Graph(format='svg')

    for node in nodes:
        graph.node(node)

    for key, value in topology_dict.items():
        head, t_label = key
        tail, h_label = value
        graph.edge(head, tail, headlabel=h_label, taillabel=t_label, label=" "*12)

    graph = apply_styles(graph, style_dict)
    filename = graph.render(filename=out_filename)
    print( "Topology saved in", filename )
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号