code: def reorder_dag(sequence,
                  depends_getter=lambda x: x.depends_on,
                  name_getter=lambda x: x.app_name,
                  impatience_max=100):
      
  
      jobs = collections.defaultdict(list)
      map_ = {}
      _count_roots = 0
      for each in sequence:
          name = name_getter(each)
          depends_on = depends_getter(each)
          if depends_on is None:
              depends_on = []
          elif isinstance(depends_on, tuple):
              depends_on = list(depends_on)
          elif not isinstance(depends_on, list):
              depends_on = [depends_on]
          if not depends_on:
              _count_roots += 1
          jobs[name] += depends_on
          map_[name] = each
  
      if not _count_roots:
          raise CircularDAGError("No job is at the root")
  
      try:
          jobs = dict(zip(jobs.keys(), map(set, jobs.values())))
          ordered_jobs = list(toposort_flatten(jobs))
      except ValueError, e:
          raise CircularDAGError(e)
  
      return [map_[x] for x in ordered_jobs if x in map_]
docs: 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.