如何访问Python Graph-Tool属性映射中的“类"字段? [英] How do I access Class fields in Python Graph-Tool property maps?

查看:80
本文介绍了如何访问Python Graph-Tool属性映射中的“类"字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个具有作为顶点属性的类的图形.如何在vertex_text设置为它们包含的类的名称字段的情况下绘制图形?

I'm trying to draw a graph with a class as a vertex property. How do I draw the graph with the vertex_text set to the name field of the classes they contain?

from graph_tool.all import *

class Node(object):
    def __init__(self, name, age):
        self.symbol = name
        self.named_entity = age


#create your graph object
g = Graph()

#add the property to vertex object
vprop = g.new_vertex_property("object") 

#add vertex 
v1 = g.add_vertex() #here you create a vertex
v2 = g.add_vertex() #here you create a vertex

#set the value to the vertex property
vx1 = Node("John", 15)
vx2 = Node("Sarah", 22)

vprop[v1] = vx1
vprop[v2] = vx2

#assign properties as a dic value
g.vertex_properties["node"]=vprop 

#add edge
g.add_edge(vertex_1,vertex_2) #add an edge 

#draw you graph 
graph_draw(
    g,
    vertex_text=g.vertex_properties["node"].name,
    vertex_font_size=18,
    output_size=(200, 200), 
)

推荐答案

您提供的代码示例充满了基本错误.您定义vprop,但随后使用v_prop.该代码在任何情况下都不起作用,因为属性映射被定义为字符串类型,但是您要为其设置一个类对象.

The code example you give is full of basic errors. You define vprop but then you use v_prop. The code would not work in any case, as the property map is defined to be of string type, but you are setting to it a class object.

但是对基本问题的答案只是将名称设置为字符串属性映射并使用它.

But answer to the underlying question is simply to set a the names to a string property map and use that.

这篇关于如何访问Python Graph-Tool属性映射中的“类"字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆