开发者

test for node membership in pydot graph

开发者 https://www.devze.com 2023-03-11 12:11 出处:网络
pydot has a huge number of bound methods for getting and setting every little thing in a dot graph, reading and writing, you-name-it, but I can\'t seem to find a simple membership test.

pydot has a huge number of bound methods for getting and setting every little thing in a dot graph, reading and writing, you-name-it, but I can't seem to find a simple membership test.

>>> d = pydot.Dot()
>>> n = pydot.Node('foobar')
>>> d.add_node(n)

>>> n in d.get_nodes()
False

is just one of many things that didn't work. It appears that nodes, once added to a 开发者_高级运维graph, acquire a new identity

>>> d.get_nodes()[0]
<pydot.Node object at 0x171d6b0>
>>> n
<pydot.Node object at 0x1534650>

Can anyone suggest a way to create a node and test to see if it's in a graph before adding it so you could do something like this:

d = pydot.Dot()
n = pydot.Node('foobar')
if n not in d:
    d.add_node(n)


Looking through the source code, http://code.google.com/p/pydot/source/browse/trunk/pydot.py, it seems that node names are unique values, used as the keys to locate the nodes within a graph's node dictionary (though, interestingly, rather than return an error for an existing node, it simply adds the attributes of the new node to those of the existing one).

So unless you want to add an implementation of __contains__() to one of the classes in the pydot.py file that does the following, you can just do the following in your code:

if n.get_name() not in d.obj_dict['nodes'].keys():
    d.add_node(n)
0

精彩评论

暂无评论...
验证码 换一张
取 消