parameters: (pairlist)
return_statement: return topsort_levels_core(num_parents, children)
docstring: Topologically sort a list of (parent, child) pairs into depth levels.
  
      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.
  
      >>> dependency_pairs = [(1,2), (3,4), (5,6), (1,3), (1,5), (1,6), (2,5)]
      >>> for level in iter(topsort_levels( dependency_pairs )):
      ...    print level
      [1]
      [2, 3]
      [4, 5]
      [6]
  
      >>> dependency_pairs = [(1,2), (1,3), (2,4), (3,4), (5,6), (4,5)]
      >>> for level in iter(topsort_levels( dependency_pairs )):
      ...    print level
      [1]
      [2, 3]
      [4]
      [5]
      [6]
  
      >>> dependency_pairs = [(1,2), (2,3), (3,4), (4, 3)]
      >>> try:
      ...     for level in iter(topsort_levels( dependency_pairs )):
      ...         print level
      ... except CycleError, exc:
      ...     print 'CycleError:', exc
      [1]
      [2]
      CycleError: ({3: 1, 4: 1}, {3: [4], 4: [3]})
  
  
      The cycle error should look like.
      CycleError: ({3: 1, 4: 1}, {3: [4], 4: [3]})
      # todo: Make the doctest more robust (i.e. handle arbitrary dict order).
docstring_summary: Topologically sort a list of (parent, child) pairs into depth levels.
docstring_tokens: Topologically sort a list of ( parent child ) pairs into depth levels .
function: def topsort_levels(pairlist):
      """Topologically sort a list of (parent, child) pairs into depth levels.
  
      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.
  
      >>> dependency_pairs = [(1,2), (3,4), (5,6), (1,3), (1,5), (1,6), (2,5)]
      >>> for level in iter(topsort_levels( dependency_pairs )):
      ...    print level
      [1]
      [2, 3]
      [4, 5]
      [6]
  
      >>> dependency_pairs = [(1,2), (1,3), (2,4), (3,4), (5,6), (4,5)]
      >>> for level in iter(topsort_levels( dependency_pairs )):
      ...    print level
      [1]
      [2, 3]
      [4]
      [5]
      [6]
  
      >>> dependency_pairs = [(1,2), (2,3), (3,4), (4, 3)]
      >>> try:
      ...     for level in iter(topsort_levels( dependency_pairs )):
      ...         print level
      ... except CycleError, exc:
      ...     print 'CycleError:', exc
      [1]
      [2]
      CycleError: ({3: 1, 4: 1}, {3: [4], 4: [3]})
  
  
      The cycle error should look like.
      CycleError: ({3: 1, 4: 1}, {3: [4], 4: [3]})
      # todo: Make the doctest more robust (i.e. handle arbitrary dict order).
  
      """
      num_parents = {}  # element -> # of predecessors 
      children = {}  # element -> list of successors 
      for parent, child in pairlist: 
          # Make sure every element is a key in num_parents.
          if not num_parents.has_key( parent ): 
              num_parents[parent] = 0 
          if not num_parents.has_key( child ): 
              num_parents[child] = 0 
  
          # Since child has a parent, increment child's num_parents count.
          num_parents[child] += 1
  
          # ... and parent gains a child.
          children.setdefault(parent, []).append(child)
  
      return topsort_levels_core(num_parents, children)
function_tokens: def topsort_levels ( pairlist ) : num_parents = { } # element -> # of predecessors  children = { } # element -> list of successors  for parent , child in pairlist : # Make sure every element is a key in num_parents. if not num_parents . has_key ( parent ) : num_parents [ parent ] = 0 if not num_parents . has_key ( child ) : num_parents [ child ] = 0 # Since child has a parent, increment child's num_parents count. num_parents [ child ] += 1 # ... and parent gains a child. children . setdefault ( parent , [ ] ) . append ( child ) return topsort_levels_core ( num_parents , children )