0

I was planning to try to use this code in order to do a critical path analysis. When running this code I got the following error but I have no idea what it means (because I don't now how the code works).

Traceback (most recent call last): File "/Users/PeterVanvoorden/Desktop/test.py", line 22, in G.add_edge('A','B',1) File "/Library/Python/2.7/site-packages/python_graph_core-1.8.2-py2.7.egg/pygraph/classes/digraph.py", line 161, in add_edge u, v = edge ValueError: need more than 1 value to unpack

# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com>
# License: MIT (see COPYING file)


import sys
sys.path.append('..')
import pygraph
from pygraph.classes.digraph import digraph
from pygraph.algorithms.critical import transitive_edges, critical_path

#demo of the critical path algorithm and the transitivity detection algorithm

G = digraph()

G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_node('F')

G.add_edge('A','B',1)
G.add_edge('A','C',2)
G.add_edge('B','C',10)
G.add_edge('B','D',2)
G.add_edge('B','E',8)
G.add_edge('C','D',7)
G.add_edge('C','E',3)
G.add_edge('E','D',1)
G.add_edge('D','F',3)
G.add_edge('E','F',1)
#add this edge to add a cycle
#G.add_edge('E','A',1)

print transitive_edges(G)
print critical_path(G)

I know it is kind of stupid just to copy code without understanding it but I thought I'd first try the example code in order to see if the package is working but apparently it's not working. Now I wonder if it's just because of a little mistake in the example code or if it's a more fundamental problem.

Peter Vanvoorden
  • 181
  • 1
  • 2
  • 11
  • I've never used `pygraph` but reading the source code it looks like you are supposed to be doing `G.add_edge(('A', 'B'), 1)` (note the extra pair of parentheses). Can you change those lines and see if that fixes it? – Two-Bit Alchemist Mar 31 '14 at 21:41
  • It runs now! Awesome! In which file did you find this information? I'm not finding any decent 'how to' documentation.. – Peter Vanvoorden Mar 31 '14 at 21:46
  • Unfortunately I didn't which is why I left you a comment instead of an answer, because I only know why you are getting the `ValueError` and not how this is _really_ supposed to be used. I just peeked at [line 161 of add_edge](http://code.google.com/p/python-graph/source/browse/trunk/core/pygraph/classes/digraph.py) where your error occurred and saw it was trying to unpack the first argument of this function as a 2-tuple. – Two-Bit Alchemist Mar 31 '14 at 21:48
  • Can I mark your comment as an answer or isn't that possible? Or should I delete this question? – Peter Vanvoorden Mar 31 '14 at 21:52
  • Posted as an answer so you could accept. I looked and could not find a good pygraph tutorial. Sorry! – Two-Bit Alchemist Mar 31 '14 at 21:54

1 Answers1

1

I peeked at the source code for this and see that add_edge is trying to unpack the first positional argument as a 2-tuple.

If you change these lines:

G.add_edge('A','B',1)
G.add_edge('A','C',2)
...

to:

G.add_edge(('A', 'B'), 1)  # note the extra parens
G.add_edge(('A', 'C'), 2)
...

it should work. However, I have not used pygraph before so this may still not produce the desired results.

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82