def reorder_dag(sequence,<EOL>depends_getter=lambda x: x.depends_on,<EOL>name_getter=lambda x: x.app_name,<EOL>impatience_max=<NUM_LIT:100>):
jobs = collections.defaultdict(list)<EOL>map_ = {}<EOL>_count_roots = <NUM_LIT:0><EOL>for each in sequence:<EOL><INDENT>name = name_getter(each)<EOL>depends_on = depends_getter(each)<EOL>if depends_on is None:<EOL><INDENT>depends_on = []<EOL><DEDENT>elif isinstance(depends_on, tuple):<EOL><INDENT>depends_on = list(depends_on)<EOL><DEDENT>elif not isinstance(depends_on, list):<EOL><INDENT>depends_on = [depends_on]<EOL><DEDENT>if not depends_on:<EOL><INDENT>_count_roots += <NUM_LIT:1><EOL><DEDENT>jobs[name] += depends_on<EOL>map_[name] = each<EOL><DEDENT>if not _count_roots:<EOL><INDENT>raise CircularDAGError("<STR_LIT>")<EOL><DEDENT>try:<EOL><INDENT>jobs = dict(list(zip(list(jobs.keys()), list(map(set, list(jobs.values()))))))<EOL>ordered_jobs = list(toposort_flatten(jobs))<EOL><DEDENT>except ValueError as e:<EOL><INDENT>raise CircularDAGError(e)<EOL><DEDENT>return [map_[x] for x in ordered_jobs if x in map_]<EOL>
DAG = Directed Acyclic Graph
If we have something like:
    C depends on B
    B depends on A
    A doesn't depend on any

Given the order of [C, B, A] expect it to return [A, B, C]

parameters:

    :sequence: some sort of iterable list

    :depends_getter: a callable that extracts the depends on sub-list

    :name_getter: a callable that extracts the name

    :impatience_max: a max count that is reached before we end up in
                     an infinite loop.