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

Method add_nodes

easygraph/classes/directed_graph.py:522–575  ·  view source on GitHub ↗

Add nodes with a list of nodes. Parameters ---------- nodes_for_adding : list nodes_attr : list of dict The corresponding attribute for each of *nodes_for_adding*. See Also -------- add_node Examples --------

(self, nodes_for_adding: list, nodes_attr: List[Dict] = [])

Source from the content-addressed store, hash-verified

520 self._add_one_node(node_for_adding, node_attr)
521
522 def add_nodes(self, nodes_for_adding: list, nodes_attr: List[Dict] = []):
523 """Add nodes with a list of nodes.
524
525 Parameters
526 ----------
527 nodes_for_adding : list
528
529 nodes_attr : list of dict
530 The corresponding attribute for each of *nodes_for_adding*.
531
532 See Also
533 --------
534 add_node
535
536 Examples
537 --------
538 Add nodes with a list of nodes.
539 You can add with node attributes using a list of Python dict type,
540 each of which is the attribute of each node, respectively.
541
542 >>> G.add_nodes([1, 2, 'a', 'b'])
543 >>> G.add_nodes(range(1, 200))
544
545 >>> G.add_nodes(['Jack', 'Tom', 'Lily'], nodes_attr=[
546 ... {
547 ... 'age': 10,
548 ... 'gender': 'M'
549 ... },
550 ... {
551 ... 'age': 11,
552 ... 'gender': 'M'
553 ... },
554 ... {
555 ... 'age': 10,
556 ... 'gender': 'F'
557 ... }
558 ... ])
559
560 """
561 if nodes_attr is None:
562 nodes_attr = []
563 if not len(nodes_attr) == 0: # Nodes attributes included in input
564 assert len(nodes_for_adding) == len(
565 nodes_attr
566 ), "Nodes and Attributes lists must have same length."
567 else: # Set empty attribute for each node
568 nodes_attr = [dict() for i in range(len(nodes_for_adding))]
569
570 for i in range(len(nodes_for_adding)):
571 try:
572 self._add_one_node(nodes_for_adding[i], nodes_attr[i])
573 except Exception as err:
574 print(err)
575 pass
576
577 def add_nodes_from(self, nodes_for_adding, **attr):
578 """Add multiple nodes.

Callers 1

condensationFunction · 0.95

Calls 1

_add_one_nodeMethod · 0.95

Tested by

no test coverage detected