def posort(l, *cmps):
    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)
Override to add additional logic. Does nothing by default.
dataset/ETHPy150Open rizar/attention-lvcsr/libs/Theano/theano/gof/sched.py/posort--crossover with--dataset/ETHPy150Open django-import-export/django-import-export/import_export/resources.py/Resource.after_save_instance