Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def successors_iter(self, n):
"""Return iterator over successor nodes of n.
Note: modifying the graph structure while iterating over
node successors may produce unpredictable results. Use
successors() as an alternative.
"""
n = Node(self, n)
nh = n.handle
eh = gv.agfstout(self.handle, nh)
while eh is not None:
(s, t) = Edge(self, eh=eh)
if s == n:
yield Node(self, t)
else:
yield Node(self, s)
try:
eh = gv.agnxtout(self.handle, eh)
except StopIteration:
return
def to_undirected(self):
"""Return undirected copy of graph."""
if not self.directed:
return self.copy()
else:
U=AGraph(strict=self.strict)
U.graph_attr.update(self.graph_attr)
U.node_attr.update(self.node_attr)
U.edge_attr.update(self.edge_attr)
for n in self.nodes():
U.add_node(n)
new_n=Node(U,n)
new_n.attr.update(n.attr)
for e in self.edges():
(u,v)=e
U.add_edge(u,v)
uv=U.get_edge(u,v)
uv.attr.update(e.attr)
return U
def to_directed(self, **kwds):
"""Return directed copy of graph.
Each undirected edge u-v is represented as two directed
edges u->v and v->u.
"""
if not self.directed:
D = AGraph(strict=self.strict, directed=True)
D.graph_attr.update(self.graph_attr)
D.node_attr.update(self.node_attr)
D.edge_attr.update(self.edge_attr)
for n in self.nodes():
D.add_node(n)
new_n = Node(D, n)
new_n.attr.update(n.attr)
for e in self.edges():
(u, v) = e
D.add_edge(u, v)
D.add_edge(v, u)
uv = D.get_edge(u, v)
vu = D.get_edge(v, u)
uv.attr.update(e.attr)
uv.attr.update(e.attr)
vu.attr.update(e.attr)
return D
else:
return self.copy()
def predecessors_iter(self,n):
"""Return iterator over predecessor nodes of n.
Note: modifying the graph structure while iterating over
node predecessors may produce unpredictable results. Use
predecessors() as an alternative.
"""
n=Node(self,n)
nh=n.handle
eh=gv.agfstin(self.handle,nh)
while eh is not None:
(s,t)=Edge(self,eh=eh)
if s==n:
yield Node(self,t)
else:
yield Node(self,s)
eh=gv.agnxtin(self.handle,eh)
raise StopIteration
def neighbors_iter(self, n):
"""Return iterator over the nodes attached to n.
Note: modifying the graph structure while iterating over
node neighbors may produce unpredictable results. Use neighbors()
as an alternative.
"""
n = Node(self, n)
nh = n.handle
eh = gv.agfstedge(self.handle, nh)
while eh is not None:
(s, t) = Edge(self, eh=eh)
if s == n:
yield Node(self, t)
else:
yield Node(self, s)
try:
eh = gv.agnxtedge(self.handle, eh, nh)
except StopIteration:
return
def predecessors_iter(self, n):
"""Return iterator over predecessor nodes of n.
Note: modifying the graph structure while iterating over
node predecessors may produce unpredictable results. Use
predecessors() as an alternative.
"""
n = Node(self, n)
nh = n.handle
eh = gv.agfstin(self.handle, nh)
while eh is not None:
(s, t) = Edge(self, eh=eh)
if s == n:
yield Node(self, t)
else:
yield Node(self, s)
try:
eh = gv.agnxtin(self.handle, eh)
except StopIteration:
return
"""
if nbunch is None: # all nodes
nh=gv.agfstnode(self.handle)
while nh is not None:
eh=gv.agfstout(self.handle,nh)
while eh is not None:
e=Edge(self,eh=eh)
if keys:
yield (e[0],e[1],e.name)
else:
yield e
eh=gv.agnxtout(self.handle,eh)
nh=gv.agnxtnode(self.handle,nh)
elif nbunch in self: # if nbunch is a single node
n=Node(self,nbunch)
nh=n.handle
eh=gv.agfstout(self.handle,nh)
while eh is not None:
e=Edge(self,eh=eh)
if keys:
yield (e[0],e[1],e.name)
else:
yield e
eh=gv.agnxtout(self.handle,eh)
else: # if nbunch is a sequence of nodes
try: bunch=[n for n in nbunch if n in self]
except TypeError:
raise TypeError("nbunch is not a node or a sequence of nodes.")
for n in nbunch:
try:
nh=Node(self,n).handle
def prepare_nbunch(self, nbunch=None):
# private function to build bunch from nbunch
if nbunch is None: # include all nodes via iterator
bunch = self.nodes_iter()
elif nbunch in self: # if nbunch is a single node
bunch = [Node(self, nbunch)]
else: # if nbunch is a sequence of nodes
try: # capture error for nonsequence/iterator entries.
bunch = [Node(self, n) for n in nbunch if n in self]
# bunch=(n for n in nbunch if n in self) # need python 2.4
except TypeError:
raise TypeError("nbunch is not a node or a sequence of nodes.")
return bunch
def successors_iter(self, n):
"""Return iterator over successor nodes of n.
Note: modifying the graph structure while iterating over
node successors may produce unpredictable results. Use
successors() as an alternative.
"""
n = Node(self, n)
nh = n.handle
eh = gv.agfstout(self.handle, nh)
while eh is not None:
(s, t) = Edge(self, eh=eh)
if s == n:
yield Node(self, t)
else:
yield Node(self, s)
try:
eh = gv.agnxtout(self.handle, eh)
except StopIteration:
return
def predecessors_iter(self, n):
"""Return iterator over predecessor nodes of n.
Note: modifying the graph structure while iterating over
node predecessors may produce unpredictable results. Use
predecessors() as an alternative.
"""
n = Node(self, n)
nh = n.handle
eh = gv.agfstin(self.handle, nh)
while eh is not None:
(s, t) = Edge(self, eh=eh)
if s == n:
yield Node(self, t)
else:
yield Node(self, s)
try:
eh = gv.agnxtin(self.handle, eh)
except StopIteration:
return