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

Method add_edges

easygraph/classes/directed_graph.py:689–739  ·  view source on GitHub ↗

Add a list of edges. Parameters ---------- edges_for_adding : list of 2-element tuple The edges for adding. Each element is a (u, v) tuple, and u, v are start end and destination end, respectively. edges_attr : list of dict, optional

(self, edges_for_adding, edges_attr: List[Dict] = [])

Source from the content-addressed store, hash-verified

687 self._add_one_edge(u_of_edge, v_of_edge, edge_attr={"weight": weight})
688
689 def add_edges(self, edges_for_adding, edges_attr: List[Dict] = []):
690 """Add a list of edges.
691
692 Parameters
693 ----------
694 edges_for_adding : list of 2-element tuple
695 The edges for adding. Each element is a (u, v) tuple, and u, v are
696 start end and destination end, respectively.
697
698 edges_attr : list of dict, optional
699 The corresponding attributes for each edge in *edges_for_adding*.
700
701 Examples
702 --------
703 Add a list of edges into *G*
704
705 >>> G.add_edges([
706 ... (1, 2),
707 ... (3, 4),
708 ... ('Jack', 'Tom')
709 ... ])
710
711 Add edge with attributes, for example, edge weight,
712
713 >>> G.add_edges([(1,2), (2, 3)], edges_attr=[
714 ... {
715 ... 'weight': 20
716 ... },
717 ... {
718 ... 'weight': 15
719 ... }
720 ... ])
721
722 """
723 if edges_attr is None:
724 edges_attr = []
725 if not len(edges_attr) == 0: # Edges attributes included in input
726 assert len(edges_for_adding) == len(
727 edges_attr
728 ), "Edges and Attributes lists must have same length."
729 else: # Set empty attribute for each edge
730 edges_attr = [dict() for i in range(len(edges_for_adding))]
731
732 for i in range(len(edges_for_adding)):
733 try:
734 edge = edges_for_adding[i]
735 attr = edges_attr[i]
736 assert len(edge) == 2, "Edge tuple {} must be 2-tuple.".format(edge)
737 self._add_one_edge(edge[0], edge[1], attr)
738 except Exception as err:
739 print(err)
740
741 def add_edges_from(self, ebunch_to_add, **attr):
742 """Add all the edges in ebunch_to_add.

Callers

nothing calls this directly

Calls 1

_add_one_edgeMethod · 0.95

Tested by

no test coverage detected