2014-01-21 2 views
0

내 코드를 컴파일하고 실행할 때 이상한 오류가 발생합니다. 내가 원하는 것은 일부 노드가 레이블에 따라 색상이 지정된 그래프를 생성하는 것입니다. 나는이 명령을 실행Python 오류 "IndexError : 목록 색인이 범위를 벗어남"

#!/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() 

:이 코드입니다

$ python secdepgraph_2.py serverclient.bc.color.dot 

... 그리고 오류이 출력 얻을 :

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 

내가 잘못하고있는 중이 야 무엇을? 는

답변

3

에만 두 개의 명령 줄 인수가 ... 도움을 주셔서 감사합니다

["secdepgraph_2.py", "serverclient.bc.color.dot"] 

이리스트는 0을 기반으로 기억을, 그래서 첫 번째는 sys.argv[0]이고 두 번째는 sys.argv[1]

입니다
관련 문제