code: def kruskal_mst_edges(
      G,
      minimum,
      weight="weight",
      keys=True,
      data=True,
      ignore_nan=False,
      partition=None,
  ):
      """
      Iterate over edge of a Kruskal's algorithm min/max spanning tree.
  
      Parameters
      ----------
      G : NetworkX Graph
          The graph holding the tree of interest.
  
      minimum : bool (default: True)
          Find the minimum (True) or maximum (False) spanning tree.
  
      weight : string (default: 'weight')
          The name of the edge attribute holding the edge weights.
  
      keys : bool (default: True)
          If `G` is a multigraph, `keys` controls whether edge keys ar yielded.
          Otherwise `keys` is ignored.
  
      data : bool (default: True)
          Flag for whether to yield edge attribute dicts.
          If True, yield edges `(u, v, d)`, where `d` is the attribute dict.
          If False, yield edges `(u, v)`.
  
      ignore_nan : bool (default: False)
          If a NaN is found as an edge weight normally an exception is raised.
          If `ignore_nan is True` then that edge is ignored instead.
  
      partition : string (default: None)
          The name of the edge attribute holding the partition data, if it exists.
          Partition data is written to the edges using the `EdgePartition` enum.
          If a partition exists, all included edges and none of the excluded edges
          will appear in the final tree. Open edges may or may not be used.
  
      Yields
      ------
      edge tuple
          The edges as discovered by Kruskal's method. Each edge can
          take the following forms: `(u, v)`, `(u, v, d)` or `(u, v, k, d)`
          depending on the `key` and `data` parameters
      """
      subtrees = UnionFind()
      if G.is_multigraph():
          edges = G.edges(keys=True, data=True)
      else:
          edges = G.edges(data=True)
  
      """
      Sort the edges of the graph with respect to the partition data. 
      Edges are returned in the following order:
  
      * Included edges
      * Open edges from smallest to largest weight
      * Excluded edges
      """
      included_edges = []
      open_edges = []
      for e in edges:
          d = e[-1]
          wt = d.get(weight, 1)
          if isnan(wt):
              if ignore_nan:
                  continue
              raise ValueError(f"NaN found as an edge weight. Edge {e}")
  
          edge = (wt,) + e
          if d.get(partition) == EdgePartition.INCLUDED:
              included_edges.append(edge)
          elif d.get(partition) == EdgePartition.EXCLUDED:
              continue
          else:
              open_edges.append(edge)
  
      if minimum:
          sorted_open_edges = sorted(open_edges, key=itemgetter(0))
      else:
          sorted_open_edges = sorted(
              open_edges, key=itemgetter(0), reverse=True
          )
  
      # Condense the lists into one
      included_edges.extend(sorted_open_edges)
      sorted_edges = included_edges
      del open_edges, sorted_open_edges, included_edges
  
      # Multigraphs need to handle edge keys in addition to edge data.
      if G.is_multigraph():
          for wt, u, v, k, d in sorted_edges:
              if subtrees[u] != subtrees[v]:
                  if keys:
                      if data:
                          yield u, v, k, d
                      else:
                          yield u, v, k
                  else:
                      if data:
                          yield u, v, d
                      else:
                          yield u, v
                  subtrees.union(u, v)
      else:
          for wt, u, v, d in sorted_edges:
              if subtrees[u] != subtrees[v]:
                  if data:
                      yield u, v, d
                  else:
                      yield u, v
                  subtrees.union(u, v)
  
imports: from dataclasses import dataclass, field
  from enum import Enum
  from heapq import heappop, heappush
  from itertools import count
  from math import isnan
  from operator import itemgetter
  from queue import PriorityQueue
  import networkx
  from networkx.utils import UnionFind, not_implemented_for, py_random_state
label_desc: Use image node_id 2 for calling a global function with example usage: kruskal_mst_edges(G, minimum, weight, keys, data, ignore_nan, partition) without return types