code: def posort(l, *cmps):
      """
      Partially ordered sort with multiple comparators.
  
      Given a list of comparators, orders the elements in l so that the
      comparators are satisfied as much as possible giving precedence to
      earlier comparators.
  
      Parameters
      ----------
      l
          An iterable of nodes in a graph.
      cmps
          A sequence of comparator functions that describe which nodes should
          come before which others.
  
      Returns
      -------
      list
          A list of nodes which satisfy the comparators as much as possible.
  
      Notes
      -----
      Implemented with _toposort.
  
      Examples
      --------
      >>> lower_tens = lambda a, b: a/10 - b/10 # prefer lower numbers div 10
      >>> prefer evens = lambda a, b: a%2 - b%2 # prefer even numbers
      >>> posort(list(range(20)), lower_tens, prefer_evens)
      [0, 8, 2, 4, 6, 1, 3, 5, 7, 9, 16, 18, 10, 12, 14, 17, 19, 11, 13, 15]
  
      """
      comes_before = dict((a, set()) for a in l)
      comes_after = dict((a, set()) for a in l)
  
      def add_links(a, b):  # b depends on a
          comes_after[a].add(b)
          comes_after[a].update(comes_after[b])
          for c in comes_before[a]:
              comes_after[c].update(comes_after[a])
          comes_before[b].add(a)
          comes_before[b].update(comes_before[a])
          for c in comes_after[b]:
              comes_before[c].update(comes_before[b])
  
      def check():
          """
          Tests for cycles in manufactured edges.
  
          """
          for a in l:
              for b in l:
                  assert not (
                      b in comes_after[a] and a in comes_after[b]
                  )
  
      for cmp_fn in cmps:
          for a in l:
              for b in l:
                  if cmp_fn(a, b) < 0:  # a wants to come before b
                      # if this wouldn't cause a cycle and isn't already known
                      if (
                          b not in comes_before[a]
                          and b not in comes_after[a]
                      ):
                          add_links(a, b)
      # check() # debug code
  
      return _toposort(comes_after)
  
file_path: Theano/theano/gof/sched.py
imports: from __future__ import absolute_import, print_function, division
  from collections import defaultdict
  from six import iteritems
  from theano.gof.graph import list_of_nodes
  from theano.compat import cmp
label_desc: Use image node_id 6 for calling a global function with example usage: posort(l) and returns: _toposort