Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self,handle,atype):
self.handle=handle
self.type=atype
self.ghandle=gv.agraphof(handle)
# get the encoding
root_handle=gv.agroot(self.ghandle) # get root graph
try:
ah=gv.agattr(root_handle,0,'charset',None)
self.encoding=gv.agattrdefval(ah)
except KeyError:
self.encoding=_DEFAULT_ENCODING
edge_attr[e]=a
return
elif hasattr(edge_attr,"__call__"):
# call user function
def add_edge(e,a):
return edge_attr(N,e,a)
else:
# just add edge
def add_edge(e,a):
N.add_edge(e[0],e[1])
# graph
add_graph(dict(A.get_all_attr()))
# loop through nodes
for node in A.nodes():
name=pygraphviz.graphviz.agnameof(node.anode)
add_node(name,A.get_all_attr(node=node))
# loop through edges
edges_seen = {}
for edge in A.edges():
if edge in edges_seen: continue
source=pygraphviz.graphviz.agnameof(edge.source().anode)
target=pygraphviz.graphviz.agnameof(edge.target().anode)
edges_seen[edge]=1
add_edge((source,target),A.get_all_attr(edge=(source,target)))
return N
def iteritems(self):
ah = None
while 1:
try:
ah = gv.agnxtattr(self.ghandle, self.type, ah)
value = gv.agxget(self.handle, ah)
try:
defval = gv.agattrdefval(ah) # default value
if defval == value:
continue # don't report default
except: # no default, gv.getattrdefval raised error
pass
# unique value for this edge
yield (gv.agattrname(ah).decode(self.encoding),
value.decode(self.encoding))
except KeyError: # gv.agxget returned KeyError, skip
continue
except StopIteration: # gv.agnxtattr is done, as are we
return
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)
eh=gv.agnxtedge(self.handle,eh,nh)
raise StopIteration
# no handle was specified or created
# get encoding from the "charset" kwarg
self.encoding = attr.get('charset', _DEFAULT_ENCODING)
try:
if name is None:
name = ''
# instantiate a new, empty graph
self.handle = gv.agraphnew(name.encode(self.encoding),
strict, directed)
except TypeError:
raise TypeError("Graph name must be a string: %s" % name)
# encoding is already set but if it was specified explicitly
# as an attr, then set it explicitly for the graph
if 'charset' in attr:
gv.agattr_label(self.handle, 0, 'charset', self.encoding)
# if data is specified, populate the newly created graph
if data is not None:
# load from dict of dicts or dict of lists
for node in data:
for nbr in data[node]:
self.add_edge(node, nbr)
self.add_nodes_from(data.keys())
# throw away the charset attribute, if one exists,
# since we've already set it, and now it should not be changed
if 'charset' in attr:
del attr['charset']
# assign any attributes specified through keywords
self.graph_attr = Attribute(self.handle, 0) # default graph attributes
# the handle was specified or created
# get the encoding from the "charset" graph attribute
item=gv.agget(self.handle,'charset')
if item is not None:
self.encoding = item
else:
self.encoding = _DEFAULT_ENCODING
else:
# no handle was specified or created
# get encoding from the "charset" kwarg
self.encoding = attr.get('charset', _DEFAULT_ENCODING)
try:
if name is None:
name = ''
# instantiate a new, empty graph
self.handle=gv.agraphnew(name,
strict,directed)
except TypeError:
raise TypeError("Graph name must be a string: %s"%name)
# encoding is already set but if it was specified explicitly
# as an attr, then set it explicitly for the graph
if 'charset' in attr:
gv.agattr_label(self.handle,0, 'charset', self.encoding)
# if data is specified, populate the newly created graph
if data is not None:
# load from dict of dicts or dict of lists
for node in data:
for nbr in data[node]:
self.add_edge(node,nbr)
self.add_nodes_from(data.keys())
def add_subgraph(self, nbunch=None, name=None, **attr):
"""Return subgraph induced by nodes in nbunch.
Overrides (but does the same thing as) pygraphviz's
AGraph.add_subgraph method.
"""
name = name.encode(self.encoding)
handle = pygraphviz.graphviz.agsubg(
self.handle, name, 1)
subgraph = pygraphviz.AGraph(
handle=handle, name=name,
strict=self.strict, directed=self.directed,
**attr
)
nodes = self.prepare_nbunch(nbunch)
subgraph.add_nodes_from(nodes)
return subgraph