masti
masti

Reputation: 77

TypeError when iterate over DiGraph()

Hii! I want to get the time execution of my function ( test(G) ). when I use Timer I need to write the type of my object : "test(% ?? )" %G which is DiGraph here. How can I do that?

from networkx import nx

def test(G):
    for e in G.edges_iter():
        print(e)

if __name__=='__main__':
    from timeit import Timer
    G = nx.DiGraph()
    G.add_edges_from([(1,2),(4,5)])
    t = Timer("test(% ?? )"%G,"from __main__ import test")
    print( t.timeit(1))

Upvotes: 0

Views: 124

Answers (1)

John La Rooy
John La Rooy

Reputation: 304473

You should import G from __main__ as well

import networkx as nx

def test(G):
    for e in G.edges_iter():
        print(e)

if __name__=='__main__':
    from timeit import Timer
    G = nx.DiGraph()
    G.add_edges_from([(1,2),(4,5)])
    t = Timer("test(G)","from __main__ import test,G")
    print( t.timeit(1))

Note that I fixed the import statement also.

Upvotes: 1

Related Questions