def add_node(self, n, **attr): """Add a single node n. If n is not a string, conversion to a string will be attempted. String conversion will work if n has valid string representation (try str(n) if you are unsure). >>> G=AGraph() >>> G.add_node('a') >>> G.nodes() ['a'] >>> G.add_node(1) # will be converted to a string >>> G.nodes() ['a', '1'] Attributes can be added to nodes on creation (attribute values must be strings) >>> G.add_node(2,color='red') See http://www.graphviz.org/doc/info/attrs.html for a list of attributes. Anonymous Graphviz nodes are currently not implemented. """ if not self._is_string_like(n): n=str(n) n=n.encode(self.charset) try: nh=gv.agnode(self.handle,n,_Action.find) except KeyError: nh=gv.agnode(self.handle,n,_Action.create) node=Node(self,nh=nh) node.attr.update(**attr)
def __new__(self,graph,name=None,nh=None): if nh is not None: n=str.__new__(self,gv.agnameof(nh)) else: n=str.__new__(self,name) try: nh=gv.agnode(graph.handle,n,_Action.find) except KeyError: raise KeyError("Node %s not in graph."%n) n.ghandle=graph.handle n.attr=ItemAttribute(nh,1) n.handle=nh return n
def remove_node(self,n): """Remove the single node n. Attempting to remove a node that isn't in the graph will produce an error. >>> G=AGraph() >>> G.add_node('a') >>> G.remove_node('a') """ if not self._is_string_like(n): n=str(n) try: nh=gv.agnode(self.handle,n,_Action.find) gv.agdelnode(self.handle,nh) except KeyError: raise KeyError("Node %s not in graph."%n)
def get_handle(self): """Return pointer to graphviz node object.""" return gv.agnode(self.ghandle,self,_Action.find)