Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 has_edge(self, u, v=None, key=None):
"""Return True an edge u-v is in the graph or False if not.
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.has_edge('a','b')
True
Optional key argument will restrict match to edges (u,v,key).
"""
if v is None:
(u,v)=u # no v given, assume u is an edge tuple
try:
Edge(self,u,v,key)
return True
except KeyError:
return False
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
except KeyError:
continue
eh=gv.agfstout(self.handle,nh)
while eh is not None:
try:
vh = Node(self, v).handle
except:
self.add_node(v)
vh = Node(self, v).handle
if key is not None:
if not is_string_like(key):
key = str(key)
key = key.encode(self.encoding)
try:
# new
eh = gv.agedge(self.handle, uh, vh, key, _Action.create)
except KeyError:
# for strict graph, or already added
eh = gv.agedge(self.handle, uh, vh, key, _Action.find)
e = Edge(self, eh=eh)
e.attr.update(**attr)
eh = gv.agnxtout(self.handle, eh)
except StopIteration:
return
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
except KeyError:
continue
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
try:
eh = gv.agnxtout(self.handle, eh)
except StopIteration:
break
except TypeError:
raise TypeError("Subgraph name must be a string: %s" % name.decode(self.encoding))
H = self.__class__(strict=self.strict,
directed=self.directed,
handle=handle, name=name,
**attr)
if nbunch is None: return H
# add induced subgraph on nodes in nbunch
bunch = self.prepare_nbunch(nbunch)
for n in bunch:
node = Node(self, n)
nh = gv.agsubnode(handle, node.handle, _Action.create)
for (u, v, k) in self.edges(keys=True):
if u in H and v in H:
edge = Edge(self, u, v, k)
eh = gv.agsubedge(handle, edge.handle, _Action.create)
return H
def remove_edge(self, u, v=None, key=None):
"""Remove edge between nodes u and v from the graph.
With optional key argument will only remove an edge
matching (u,v,key).
"""
if v is None:
(u, v) = u # no v given, assume u is an edge tuple
e = Edge(self, u, v, key)
try:
gv.agdeledge(self.handle, e.handle)
except KeyError:
raise KeyError("Edge %s-%s not in graph." % (u, v))
def in_edges_iter(self, nbunch=None, keys=False):
"""Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges
adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over
edges may produce unpredictable results. Use in_edges()
as an alternative.
"""
if nbunch is None: # all nodes
nh=gv.agfstnode(self.handle)
while nh is not None:
eh=gv.agfstin(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.agnxtin(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.agfstin(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
def in_edges_iter(self, nbunch=None, keys=False):
"""Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges
adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over
edges may produce unpredictable results. Use in_edges()
as an alternative.
"""
if nbunch is None: # all nodes
nh = gv.agfstnode(self.handle)
while nh is not None:
eh = gv.agfstin(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
try:
eh = gv.agnxtin(self.handle, eh)
except StopIteration:
break
try:
nh = gv.agnxtnode(self.handle, nh)
except StopIteration:
return
elif nbunch in self: # if nbunch is a single node
n = Node(self, nbunch)
nh = n.handle
eh = gv.agfstin(self.handle, nh)