本文整理汇总了Python中networkx.utils.make_str函数的典型用法代码示例。如果您正苦于以下问题:Python make_str函数的具体用法?Python make_str怎么用?Python make_str使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了make_str函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_edges
def add_edges(self, G, graph_element):
def edge_key_data(G):
# helper function to unify multigraph and graph edge iterator
if G.is_multigraph():
for u, v, key, data in G.edges_iter(data=True, keys=True):
edge_data = data.copy()
edge_data.update(key=key)
edge_id = edge_data.pop("id", None)
if edge_id is None:
edge_id = next(self.edge_id)
yield u, v, edge_id, edge_data
else:
for u, v, data in G.edges_iter(data=True):
edge_data = data.copy()
edge_id = edge_data.pop("id", None)
if edge_id is None:
edge_id = next(self.edge_id)
yield u, v, edge_id, edge_data
edges_element = Element("edges")
for u, v, key, edge_data in edge_key_data(G):
kw = {"id": make_str(key)}
edge_weight = edge_data.pop("weight", False)
if edge_weight:
kw["weight"] = make_str(edge_weight)
edge_type = edge_data.pop("type", False)
if edge_type:
kw["type"] = make_str(edge_type)
edge_element = Element("edge", source=make_str(u), target=make_str(v), **kw)
default = G.graph.get("edge_default", {})
edge_data = self.add_viz(edge_element, edge_data)
edge_data = self.add_attributes("edge", edge_element, edge_data, default)
edges_element.append(edge_element)
graph_element.append(edges_element)
示例2: add_graph_element
def add_graph_element(self, G):
"""
Serialize graph G in GraphML to the stream.
"""
if G.is_directed():
default_edge_type = 'directed'
else:
default_edge_type = 'undirected'
graphid = G.graph.pop('id', None)
if graphid is None:
graph_element = self.myElement("graph",
edgedefault=default_edge_type)
else:
graph_element = self.myElement("graph",
edgedefault=default_edge_type,
id=graphid)
default = {}
data = dict((k, v) for k, v in G.graph.items()
if k not in ['node_default', 'edge_default'])
self.add_attributes("graph", graph_element, data, default)
self.add_nodes(G, graph_element)
self.add_edges(G, graph_element)
# self.attributes contains a mapping from XML Objects to a list of
# data that needs to be added to them.
# We postpone processing in order to do type inference/generalization.
# See self.attr_type
for (xml_obj, data) in self.attributes.items():
for (k, v, scope, default) in data:
xml_obj.append(self.add_data(make_str(k),
self.attr_type(k, scope, v),
make_str(v), scope, default))
self.xml.append(graph_element)
示例3: add_nodes
def add_nodes(self, G, graph_element):
nodes_element = Element("nodes")
for node, data in G.nodes_iter(data=True):
node_data = data.copy()
#
node_id = node_data.pop("id", make_str(node))
kw = {"id": node_id}
label = node_data.pop("label", make_str(node))
kw["label"] = label
pid = node_data.pop("pid", False)
if pid:
kw["pid"] = pid
# add node element with attributes
node_element = Element("node", **kw)
# add node element and attr subelements
default = G.graph.get("node_default", {})
node_data = self.add_parents(node_element, node_data)
node_data = self.add_slices(node_element, node_data)
node_data = self.add_viz(node_element, node_data)
node_data = self.add_attributes("node", node_element, node_data, default)
nodes_element.append(node_element)
graph_element.append(nodes_element)
示例4: add_attributes
def add_attributes(self, node_or_edge, xml_obj, data, default):
# Add attrvalues to node or edge
attvalues=Element('attvalues')
if len(data)==0:
return data
if 'start' in data or 'end' in data:
mode='dynamic'
else:
mode='static'
for k,v in data.items():
# rename generic multigraph key to avoid any name conflict
if k == 'key':
k='networkx_key'
attr_id = self.get_attr_id(make_str(k), self.xml_type[type(v)],
node_or_edge, default, mode)
if type(v)==list:
# dynamic data
for val,start,end in v:
e=Element("attvalue")
e.attrib['for']=attr_id
e.attrib['value']=make_str(val)
e.attrib['start']=make_str(start)
e.attrib['end']=make_str(end)
attvalues.append(e)
else:
# static data
e=Element("attvalue")
e.attrib['for']=attr_id
e.attrib['value']=make_str(v)
attvalues.append(e)
xml_obj.append(attvalues)
return data
示例5: add_nodes
def add_nodes(self, G, graph_element):
nodes_element = Element("nodes")
for node, data in G.nodes(data=True):
node_data = data.copy()
node_id = make_str(node_data.pop("id", node))
kw = {"id": node_id}
label = make_str(node_data.pop("label", node))
kw["label"] = label
try:
pid = node_data.pop("pid")
kw["pid"] = make_str(pid)
except KeyError:
pass
# add node element with attributes
node_element = Element("node", **kw)
# add node element and attr subelements
default = G.graph.get("node_default", {})
node_data = self.add_parents(node_element, node_data)
if self.version == "1.1":
node_data = self.add_slices(node_element, node_data)
else:
node_data = self.add_spells(node_element, node_data)
node_data = self.add_viz(node_element, node_data)
node_data = self.add_attributes("node", node_element, node_data, default)
nodes_element.append(node_element)
graph_element.append(nodes_element)
示例6: add_attributes
def add_attributes(self, scope, xml_obj, data, default):
"""Appends attributes to edges or nodes.
"""
for k, v in data.items():
default_value = default.get(k)
obj = self.add_data(make_str(k), type(v), make_str(v), scope=scope, default=default_value)
xml_obj.append(obj)
示例7: add_attributes
def add_attributes(self, scope, xml_obj, data, default):
"""Appends attribute data."""
for k, v in data.items():
data_element = self.add_data(make_str(k),
self.attr_type(make_str(k), scope, v),
make_str(v), scope, default.get(k))
xml_obj.append(data_element)
示例8: add_nodes
def add_nodes(self, G, graph_element):
nodes_element = Element('nodes')
for node, data in G.nodes(data=True):
node_data = data.copy()
node_id = make_str(node_data.pop('id', node))
kw = {'id': node_id}
label = make_str(node_data.pop('label', node))
kw['label'] = label
try:
pid = node_data.pop('pid')
kw['pid'] = make_str(pid)
except KeyError:
pass
# add node element with attributes
node_element = Element('node', **kw)
# add node element and attr subelements
default = G.graph.get('node_default', {})
node_data = self.add_parents(node_element, node_data)
if self.version == '1.1':
node_data = self.add_slices(node_element, node_data)
else:
node_data = self.add_spells(node_element, node_data)
node_data = self.add_viz(node_element, node_data)
node_data = self.add_attributes('node', node_element,
node_data, default)
nodes_element.append(node_element)
graph_element.append(nodes_element)
示例9: add_attributes
def add_attributes(self, node_or_edge, xml_obj, data, default):
# Add attrvalues to node or edge
attvalues = Element("attvalues")
if len(data) == 0:
return data
if "start" in data or "end" in data:
mode = "dynamic"
else:
mode = "static"
for k, v in list(data.items()):
# rename generic multigraph key to avoid any name conflict
if k == "key":
k = "networkx_key"
attr_id = self.get_attr_id(make_str(k), self.xml_type[type(v)], node_or_edge, default, mode)
if type(v) == list:
# dynamic data
for val, start, end in v:
e = Element("attvalue")
e.attrib["for"] = attr_id
e.attrib["value"] = make_str(val)
e.attrib["start"] = make_str(start)
e.attrib["end"] = make_str(end)
attvalues.append(e)
else:
# static data
e = Element("attvalue")
e.attrib["for"] = attr_id
e.attrib["value"] = make_str(v)
attvalues.append(e)
xml_obj.append(attvalues)
return data
示例10: add_nodes
def add_nodes(self, G, graph_element):
nodes_element = Element('nodes')
for node,data in G.nodes_iter(data=True):
node_data=data.copy()
#
node_id=node_data.pop('id',make_str(node))
kw={'id':node_id}
label=node_data.pop('label',make_str(node))
kw['label']=label
pid=node_data.pop('pid',False)
if pid:
kw['pid']=pid
if 'start' in node_data:
kw['start']=make_str(node_data.pop('start'))
if 'end' in node_data:
kw['end']=make_str(node_data.pop('end'))
# add node element with attributes
node_element = Element("node", **kw)
# add node element and attr subelements
default=G.graph.get('node_default',{})
node_data=self.add_parents(node_element, node_data)
if self.version=='1.1':
node_data=self.add_slices(node_element, node_data)
else:
node_data=self.add_spells(node_element, node_data)
node_data=self.add_viz(node_element,node_data)
node_data=self.add_attributes("node", node_element,
node_data, default)
nodes_element.append(node_element)
graph_element.append(nodes_element)
示例11: to_pydot
def to_pydot(N, strict=True):
"""Return a pydot graph from a NetworkX graph N.
Parameters
----------
N : NetworkX graph
A graph created with NetworkX
Examples
--------
>>> K5 = nx.complete_graph(5)
>>> P = nx.nx_pydot.to_pydot(K5)
Notes
-----
"""
import pydotplus
# set Graphviz graph type
if N.is_directed():
graph_type='digraph'
else:
graph_type='graph'
strict=N.number_of_selfloops()==0 and not N.is_multigraph()
name = N.name
graph_defaults=N.graph.get('graph',{})
if name is '':
P = pydotplus.Dot('', graph_type=graph_type, strict=strict,
**graph_defaults)
else:
P = pydotplus.Dot('"%s"'%name, graph_type=graph_type, strict=strict,
**graph_defaults)
try:
P.set_node_defaults(**N.graph['node'])
except KeyError:
pass
try:
P.set_edge_defaults(**N.graph['edge'])
except KeyError:
pass
for n,nodedata in N.nodes_iter(data=True):
str_nodedata=dict((k,make_str(v)) for k,v in nodedata.items())
p=pydotplus.Node(make_str(n),**str_nodedata)
P.add_node(p)
if N.is_multigraph():
for u,v,key,edgedata in N.edges_iter(data=True,keys=True):
str_edgedata=dict((k,make_str(v)) for k,v in edgedata.items())
edge=pydotplus.Edge(make_str(u), make_str(v),
key=make_str(key), **str_edgedata)
P.add_edge(edge)
else:
for u,v,edgedata in N.edges_iter(data=True):
str_edgedata=dict((k,make_str(v)) for k,v in edgedata.items())
edge=pydotplus.Edge(make_str(u),make_str(v),**str_edgedata)
P.add_edge(edge)
return P
示例12: node_link_graph
def node_link_graph(data, directed=False, multigraph=True):
"""Return graph from node-link data format.
Parameters
----------
data : dict
node-link formatted graph data
directed : bool
If True, and direction not specified in data, return a directed graph.
multigraph : bool
If True, and multigraph not specified in data, return a multigraph.
Returns
-------
G : NetworkX graph
A NetworkX graph object
Examples
--------
>>> from networkx.readwrite import json_graph
>>> G = nx.Graph([(1,2)])
>>> data = json_graph.node_link_data(G)
>>> H = json_graph.node_link_graph(data)
See Also
--------
node_link_data, adjacency_data, tree_data
"""
multigraph = data.get('multigraph',multigraph)
directed = data.get('directed',directed)
if multigraph:
graph = nx.MultiGraph()
else:
graph = nx.Graph()
if directed:
graph = graph.to_directed()
mapping=[]
graph.graph = dict(data.get('graph',[]))
c = count()
for d in data['nodes']:
node = d.get('id',next(c))
mapping.append(node)
nodedata = dict((make_str(k),v) for k,v in d.items() if k!='id')
graph.add_node(node, **nodedata)
for d in data['links']:
link_data = d.copy()
source = link_data.pop('source')
target = link_data.pop('target')
edgedata = dict((make_str(k),v) for k,v in d.items()
if k!='source' and k!='target')
graph.add_edge(mapping[source],mapping[target],**edgedata)
return graph
示例13: _place_initial_states
def _place_initial_states(trs_graph, pd_graph, tikz):
init_subg = pydot.Subgraph('initial')
init_subg.set_rank('source')
for node in trs_graph.states.initial:
pd_node = pydot.Node(make_str(node))
init_subg.add_node(pd_node)
phantom_node = 'phantominit' + str(node)
pd_node = pydot.Node(make_str(phantom_node))
init_subg.add_node(pd_node)
pd_graph.add_subgraph(init_subg)
示例14: add_spells
def add_spells(self,node_or_edge_element,node_or_edge_data):
spells=node_or_edge_data.pop('spells',False)
if spells:
spells_element=Element('spells')
for start,end in spells:
e=Element('spell')
if start is not None:
e.attrib['start']=make_str(start)
if end is not None:
e.attrib['end']=make_str(end)
spells_element.append(e)
node_or_edge_element.append(spells_element)
return node_or_edge_data
示例15: add_edges
def add_edges(self, G, graph_element):
if G.is_multigraph():
for u, v, key, data in G.edges_iter(data=True, keys=True):
edge_element = Element("edge", source=make_str(u), target=make_str(v))
default = G.graph.get("edge_default", {})
self.add_attributes("edge", edge_element, data, default)
self.add_attributes("edge", edge_element, {"key": key}, default)
graph_element.append(edge_element)
else:
for u, v, data in G.edges_iter(data=True):
edge_element = Element("edge", source=make_str(u), target=make_str(v))
default = G.graph.get("edge_default", {})
self.add_attributes("edge", edge_element, data, default)
graph_element.append(edge_element)