I would like to store instances of a class in a graph-tool graph, one object per node (or 'vertex' as graph-tool calls them). I am trying to use a vertex property as that seems to the be the way to do this.
class MyClass(object):
def __init__(self, title):
self.title = title
graph = Graph()
my_obj = MyClass('some title')
vertex = graph.add_vertex()
vprop = graph.new_vertex_property('object')
vprop[vertex] = my_obj
Now I would like to read my class objects back out, e.g. iterate over all nodes / vertices and print their titles:
for vertex in self.graph.vertices():
# TODO: how to access titles ? this just prints
# "<Vertex object with index '0' at 0xb1e4bac>"
print repr(vertex) + '\n'
Also, how do I get a class object with a certain title back from the graph ? One way seems to be to create a vertex filter using graph.set_edge_filter(...)
and apply that - which seems a pretty expensive operation considering all I want is to get one single object back. I really don't want to maintain my own object title / vertex index mapping as IMO, that is one of the tasks of the graph.
Am I missing something fundamental here ?