【发布时间】:2012-03-20 13:47:06
【问题描述】:
我想将一个类的实例存储在graph-tool 图形中,每个节点一个对象(或图形工具调用它们的“顶点”)。我正在尝试使用顶点属性,因为它似乎是 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
现在我想读回我的类对象,例如遍历所有节点/顶点并打印它们的标题:
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'
另外,如何从图中获取具有特定标题的类对象?一种方法似乎是使用graph.set_edge_filter(...) 创建一个顶点过滤器并应用它——考虑到我想要的只是取回一个对象,这似乎是一项相当昂贵的操作。我真的不想像 IMO 一样维护自己的对象标题/顶点索引映射,这是图表的任务之一。
我在这里缺少一些基本的东西吗?
【问题讨论】:
标签: python graph vertices graph-tool