parameters: (num_parents, children)
docstring: Topologically sort a bunch of interdependent items based on dependency.
  
      This returns a generator.
      Turn this into a an iterator using the iter built-in function.
      (if you iterate over the iterator, each element gets generated when
      it is asked for, rather than generating the whole list up-front.)
  
      Each generated element is a list of items at that dependency level.
  
      >>> list(topsort_levels_core(
      ...          {1: 0, 2: 1, 3: 1, 4: 1, 5: 2, 6: 2},
      ...          {1: [2, 3, 5, 6], 2: [5], 3: [4], 4: [], 5: [6]}))
      [[1], [2, 3], [4, 5], [6]]
  
      >>> list(topsort_levels_core(
      ...          {1: 0, 2: 2, 3: 1},
      ...          {1: [2], 2: [3], 3: [2]}))
      Traceback (most recent call last):
      CycleError: ({2: 1, 3: 1}, {2: [3], 3: [2]})
  
      This function has a more complicated interface than topsort_levels,
      but is useful if the data is easier to generate in this form.
  
      Arguments:
      num_parents -- key: item, value: number of parents (predecessors)
      children -- key: item, value: list of children (successors)
docstring_summary: Topologically sort a bunch of interdependent items based on dependency.
docstring_tokens: Topologically sort a bunch of interdependent items based on dependency .
function: def topsort_levels_core(num_parents, children):
      """Topologically sort a bunch of interdependent items based on dependency.
  
      This returns a generator.
      Turn this into a an iterator using the iter built-in function.
      (if you iterate over the iterator, each element gets generated when
      it is asked for, rather than generating the whole list up-front.)
  
      Each generated element is a list of items at that dependency level.
  
      >>> list(topsort_levels_core(
      ...          {1: 0, 2: 1, 3: 1, 4: 1, 5: 2, 6: 2},
      ...          {1: [2, 3, 5, 6], 2: [5], 3: [4], 4: [], 5: [6]}))
      [[1], [2, 3], [4, 5], [6]]
  
      >>> list(topsort_levels_core(
      ...          {1: 0, 2: 2, 3: 1},
      ...          {1: [2], 2: [3], 3: [2]}))
      Traceback (most recent call last):
      CycleError: ({2: 1, 3: 1}, {2: [3], 3: [2]})
  
      This function has a more complicated interface than topsort_levels,
      but is useful if the data is easier to generate in this form.
  
      Arguments:
      num_parents -- key: item, value: number of parents (predecessors)
      children -- key: item, value: list of children (successors)
  
      """
      while 1:
          # Suck up everything without a predecessor.
          level_parents = [x for x in num_parents.keys() if num_parents[x] == 0]
  
          if not level_parents:
              break
  
          # Offer the next generated item,
          # which is a list of the items at this dependency level.
          yield level_parents
  
          # For everything item in this level,
          # decrement the parent count,
          # since we have accounted for its parent.
          for level_parent in level_parents:
  
              del num_parents[level_parent]
  
              if children.has_key(level_parent):
                  for level_parent_child in children[level_parent]:
                      num_parents[level_parent_child] -= 1
                  del children[level_parent]
          
      if num_parents: 
          # Everything in num_parents has at least one child -> 
          # there's a cycle.
          raise CycleError(num_parents, children)
      else:
          # This is the end of the generator.
          raise StopIteration
function_tokens: def topsort_levels_core ( num_parents , children ) : while 1 : # Suck up everything without a predecessor. level_parents = [ x for x in num_parents . keys ( ) if num_parents [ x ] == 0 ] if not level_parents : break # Offer the next generated item, # which is a list of the items at this dependency level. yield level_parents # For everything item in this level, # decrement the parent count, # since we have accounted for its parent. for level_parent in level_parents : del num_parents [ level_parent ] if children . has_key ( level_parent ) : for level_parent_child in children [ level_parent ] : num_parents [ level_parent_child ] -= 1 del children [ level_parent ] if num_parents : # Everything in num_parents has at least one child ->  # there's a cycle. raise CycleError ( num_parents , children ) else : # This is the end of the generator. raise StopIteration