Python错误“索引器错误:列表索引超出范围”

2024-04-26 00:46:04 发布

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

我在编译和运行代码时遇到了这个奇怪的错误。 我想要的是生成一个图,其中一些节点根据它们的标签被着色。代码如下:

#!/usr/bin/env python
# Code V1.1 
# Reduce NetDepGraph 

import os
import pydot
import sys
import networkx as nx 
from networkx import dfs_edges
import matplotlib.pyplot as plot
from itertools import islice

def is_secure(nodo):

    insecure_functions = [
        "write",
        "read",
        "send",
        "recv",
        "recvfrom",
        "sendto"
    ]

    try:
        x = int(nodo)
        return True

    except:
        pass

    for funcao in insecure_functions:
        if nodo.lower().find(funcao) != -1:
            return False
    return True

def colore_subgrafo(nodo):
    print("colorindo nodo {}".format(nodo['label']))
    if nodo['label'].find("write") != -1 or nodo['label'].find("send") != -1 or nodo['label'].find("sendto") != -1:
        nodo["color"] = "blue"
        nodo["style"] = "filled"
        return "blue"
    else:
        nodo["color"] = "green"
        nodo["style"] = "filled"
        return "green"

def getAttributes(idNode , grafo):

    for nodo,attributes in grafo.nodes(data=True):
        if idNode == nodo:
            return (nodo,attributes)

def colore_vizinhos(grafo,lista,cor):
    for n in lista:
        no = getAttributes(n,grafo)
        (node,attributes) = no
        attributes['color'] = cor
        attributes["style"] = "filled"
        sucessores = list( vizinho for eu,vizinho in grafo.out_edges(node))
        if len(sucessores) != 0 :
            colore_vizinhos(grafo,sucessores,cor) 
        return 0

def removeNodesInsignificantes(grafo):
    nodes = ["perror","unreachable"]
    for nodo,attributes in grafo.nodes(data=True): 
        for n in nodes:
            if attributes['label'].find(n) != -1: 
                suc = list( vizinho for eu,vizinho in grafo.out_edges(nodo))
                pre = list( vizinho for eu,vizinho in grafo.in_edges(nodo))
                all_n = suc+pre+[nodo]
                grafo.remove_nodes_from(all_n) 
            if attributes['label'].find("") and grafo.edges(nodo) <= 1:
                grafo.remove_node(nodo)

def remove_flow_through(graph):
    for n,node in graph.nodes(data=True):
        pred = graph.predecessors(n)
        succ = graph.successors(n)
        if node['label'] == " " or node['label'] == "unrecheable":
            if len(pred) == len(succ) == 1:
                graph.remove_node(n)
                graph.add_edge(pred[0], succ[0])



def main():

    grafodot = pydot.graph_from_dot_file( sys.argv[1] )
    grafo = nx.DiGraph( nx.drawing.from_pydot( grafodot ) )
    i = 0
    for nodo,attributes in grafo.nodes(data=True): 
        if not is_secure( attributes['label'] ):
            cor_pai = colore_subgrafo(attributes) 
            sucessores = list( vizinho for eu,vizinho in grafo.out_edges(nodo))
            colore_vizinhos(grafo,sucessores,cor_pai)


    # while True:
    #     prev_len = len(grafo)
    #     remove_flow_through(grafo)
    #     if len(grafo) == prev_len:
    #         break

    removeNodesInsignificantes(grafo)    

    desenhaPng = nx.to_pydot(grafo, strict=False) 
    desenhaPng.write_png('{}.png'.format(sys.argv[2]))


if __name__ == "__main__":
    main()

我用这个命令运行:

^{pr2}$

。。。我得到了一个错误的输出:

colorindo nodo "Const:recv"
colorindo nodo "Call recv"
colorindo nodo "Const:tttrecv"
colorindo nodo "Call send"
colorindo nodo "Const:tttsend"
colorindo nodo "Call tttrecv"
colorindo nodo "Call tttsend"
colorindo nodo "Net Write: tttsend 6"
colorindo nodo "Net Read: tttrecv 3"
colorindo nodo "Const:send"
colorindo nodo "Net Write: send 7"
Traceback (most recent call last):
  File "secdepgraph_2.py", line 112, in <module>
    main()
  File "secdepgraph_2.py", line 108, in main
    desenhaPng.write_png('{}.png'.format(sys.argv[2]))
IndexError: list index out of range

我做错什么了? 谢谢你的帮助。。。在


Tags: inimportnodetrueforreturnifdef