def dfs(self, direction, *jobs, stop=(lambda job: False), post=True):
    'Perform depth-first traversal of the DAG.'
    visited = set()

    def _dfs(job):
        'Inner function for DFS traversal.'
        if stop(job):
            return
        if (not post):
            (yield job)
        for job_ in direction[job]:
            if (not (job_ in visited)):
                visited.add(job_)
                for j in _dfs(job_):
                    (yield j)
        if post:
            (yield job)
    for job in jobs:
        for job_ in self._dfs(direction, job, visited, stop=stop, post=post):
            (yield job_)
Perform depth-first traversal of the DAG.
def dfs(self, direction, *jobs, stop=(lambda job: False), post=True):
    
    visited = set()

    def _dfs(job):
        'Inner function for DFS traversal.'
        if stop(job):
            return
        if (not post):
            (yield job)
        for job_ in direction[job]:
            if (not (job_ in visited)):
                visited.add(job_)
                for j in _dfs(job_):
                    (yield j)
        if post:
            (yield job)
    for job in jobs:
        for job_ in self._dfs(direction, job, visited, stop=stop, post=post):
            (yield job_)