code: def find_loops( record, index, stop_types = STOP_TYPES, open=None, seen = None ):
      
      if open is None:
          open = []
      if seen is None:
          seen = set()
      for child in children( record, index, stop_types = stop_types ):
          if child['type'] in stop_types or child['type'] == LOOP_TYPE:
              continue
          if child['address'] in open:
              # loop has been found 
              start = open.index( child['address'] )
              new = frozenset( open[start:] )
              if new not in seen:
                  seen.add(new)
                  yield new
          elif child['address'] in seen:
              continue 
          else:
              seen.add( child['address'])
              open.append( child['address'] )
              for loop in find_loops( child, index, stop_types=stop_types, open=open, seen=seen ):
                  yield loop 
              open.pop( -1 )
docs: Find all loops within the index and replace with loop records