MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / add_nodes_from

Method add_nodes_from

easygraph/classes/directed_graph.py:577–636  ·  view source on GitHub ↗

Add multiple nodes. Parameters ---------- nodes_for_adding : iterable container A container of nodes (list, dict, set, etc.). OR A container of (node, attribute dict) tuples. Node attributes are updated using the attribute dict

(self, nodes_for_adding, **attr)

Source from the content-addressed store, hash-verified

575 pass
576
577 def add_nodes_from(self, nodes_for_adding, **attr):
578 """Add multiple nodes.
579
580 Parameters
581 ----------
582 nodes_for_adding : iterable container
583 A container of nodes (list, dict, set, etc.).
584 OR
585 A container of (node, attribute dict) tuples.
586 Node attributes are updated using the attribute dict.
587 attr : keyword arguments, optional (default= no attributes)
588 Update attributes for all nodes in nodes.
589 Node attributes specified in nodes as a tuple take
590 precedence over attributes specified via keyword arguments.
591
592 See Also
593 --------
594 add_node
595
596 Examples
597 --------
598 >>> G = eg.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
599 >>> G.add_nodes_from("Hello")
600 >>> K3 = eg.Graph([(0, 1), (1, 2), (2, 0)])
601 >>> G.add_nodes_from(K3)
602 >>> sorted(G.nodes(), key=str)
603 [0, 1, 2, 'H', 'e', 'l', 'o']
604
605 Use keywords to update specific node attributes for every node.
606
607 >>> G.add_nodes_from([1, 2], size=10)
608 >>> G.add_nodes_from([3, 4], weight=0.4)
609
610 Use (node, attrdict) tuples to update attributes for specific nodes.
611
612 >>> G.add_nodes_from([(1, dict(size=11)), (2, {"color": "blue"})])
613 >>> G.nodes[1]["size"]
614 11
615 >>> H = eg.Graph()
616 >>> H.add_nodes_from(G.nodes(data=True))
617 >>> H.nodes[1]["size"]
618 11
619
620 """
621 for n in nodes_for_adding:
622 try:
623 newnode = n not in self._node
624 newdict = attr
625 except TypeError:
626 n, ndict = n
627 newnode = n not in self._node
628 newdict = attr.copy()
629 newdict.update(ndict)
630 if newnode:
631 if n is None:
632 raise ValueError("None cannot be a node")
633 self._adj[n] = self.adjlist_inner_dict_factory()
634 self._pred[n] = self.adjlist_inner_dict_factory()

Callers 1

createAFunction · 0.95

Calls 2

updateMethod · 0.80
copyMethod · 0.45

Tested by

no test coverage detected