Graphic tool: how to access properties?

I would like to save class instances in a graph-tool graph , one object per node (or the 'vertex' as a graphical tool calls them). I am trying to use the vertex property as this seems like a 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, for example. iterate over all vertices / vertices and print their names:

 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 specific title from the graph? One way, apparently, is to create a vertex filter using graph.set_edge_filter(...) and apply it, which seems like a pretty expensive operation, considering all I want is to return one object back. I really do not want to support the object's own name / vertex index transformation as IMO, this is one of the tasks of the graph.

Did I miss something fundamental here?

+4
source share
1 answer

To access property values, you use the same syntax that you used to set its values, i.e.

  for vertex in graph.vertices(): print vprop[vertex].title 

will do what you want.

If you want to get vertices with a given property value, you have no option but to search for it on the chart. Remember that property values ​​do not have to be unique, so there is no way to perform backward matching in an inexpensive way. In addition, there must be a reverse mapping for each property map, which will be expensive from memory. However, you can easily do this yourself using the dict object.

+4
source

Source: https://habr.com/ru/post/1399293/


All Articles