On 06/04/2012 08:48 AM, jarino wrote:
Hi,
I am wondering: is it possible to generate a vertex variable name from a string.
Let me explain: I need to read a set of vertices from a csv file, and a set of arcs from another file. My vertices have names of the form XYZ, and my arcs then indicate whether there is a connection between XYZ and, say, ABC.
Of course, I could define a property "name", assign it to the vertex, then find the indices of the origin and end vertices when I browsing my connection file.
But I am wondering.. From the examples, I would like to do something like
v1 = g.add_vertex() v2 = g.add_vertex() e = g.add_edge(v1, v2)
but where v1, v2, etc., would be replaced by vertexName[i] (in a loop). Probably it is my incompetence with python, but I don't seem to be able to do that. A call to
for i in range(1, len(vertexName)): vertexName[i] = g.add_vertex()
does not give an error. But when I try to assign an edge using the "same" method,
for i in range(1, len(listVertices)): e = g.add_edge(origVertex[i], destVertex[i])
I get
Traceback (innermost last): File "<stdin>", line 1, in <module> File "import_graph.py", line 59, in <module> e = g.add_edge(origVertex[i], destVertex[i]) File "/usr/local/lib/python2.7/dist-packages/graph_tool/__init__.py", line 1109, in add_edge e = libcore.add_edge(weakref.ref(self), source, target) TypeError: No registered converter was able to extract a C++ reference to type graph_tool::PythonVertex from this Python object of type str
I'm slightly confused by what you are attempting to achieve, but I think you simply want a mapping from a string to a vertex object, correct? All you have to do is keep a dictionary: vmap = {} ... name = "foo" # you get this from your files vmap[name] = g.add_vertex() ... for i in xrange(0, len(listVertices)): # you should start from 0, and use xrange! e = g.add_edge(vmap[origVertex[i]], vmap[destVertex[i]]) Cheers, Tiago -- Tiago de Paula Peixoto <tiago@skewed.de>