def __new__(self,graph,source=None,target=None,key=None,eh=None): # edge handle given, reconstruct node object if eh is not None: (source,target)=(gv.agtail(eh),gv.aghead(eh)) s=Node(graph,nh=source) t=Node(graph,nh=target) # no edge handle, search for edge and construct object else: s=Node(graph,source) t=Node(graph,target) try: eh=gv.agedge(graph.handle, s.handle, t.handle, key, _Action.find) except KeyError: raise KeyError("Edge %s-%s not in graph."%(source,target)) tp=tuple.__new__(self,(s,t)) tp.ghandle=graph.handle tp.handle=eh tp.attr=ItemAttribute(eh,3) tp.key=key return tp
def add_edge(self,u,v=None,key=None,**attr): """Add a single edge between nodes u and v. If the nodes u and v are not in the graph they will added. If u and v are not strings, conversion to a string will be attempted. String conversion will work if u and v have valid string representation (try str(u) if you are unsure). >>> G=AGraph() >>> G.add_edge('a','b') >>> G.edges() [('a', 'b')] The optional key argument allows assignment of a key to the edge. This is especially useful to distinguish between parallel edges in multi-edge graphs (strict=False). >>> G=AGraph(strict=False) >>> G.add_edge('a','b','first') >>> G.add_edge('a','b','second') >>> sorted(G.edges()) [('a', 'b', 'first'), ('a', 'b', 'second')] Attributes can be added when edges are created >>> G.add_edge('a','b',color='green') Attributes must be valid strings. See http://www.graphviz.org/doc/info/attrs.html for a list of attributes. """ if v is None: (u,v)=u # no v given, assume u is an edge tuple try: uh=Node(self,u).handle except: self.add_node(u) uh=Node(self,u).handle try: vh=Node(self,v).handle except: self.add_node(v) vh=Node(self,v).handle try: if key is not None: if not self._is_string_like(key): key=str(key) eh=gv.agedge(self.handle,uh,vh,key,_Action.create) e=Edge(self,eh=eh) e.attr.update(**attr) except KeyError: return None # silent failure for strict graph, already added