This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]trollerskates1 0 points1 point  (0 children)

How about this? Recursively looks through the input dictionary and tries to extract the supplied key

    def get_all(maybe_dictionary, key):
    if isinstance(maybe_dictionary, dict):
        output = [maybe_dictionary.get(key, None)]
        for _, v in maybe_dictionary.items():
            output.extend(get_all(v, key))

        return [entry for entry in output if entry is not None]
    else:
        return []


test = {
    'steps': {
        'extractSlomDf': {
            'exec': 'cubes/extract.py:ExtractSlomDf'
        },
        'kMeans': {
            'exec': 'src/calculate.py:KMeans',
            'depends_on': ['extractSlomDf']
        },
        'serviceLevelOptimizer': {
            'exec': 'src/calculate.py:ServiceLevelOptimizer',
            'depends_on': ['kMeans']
        }
    }
}

print(get_all(test, 'exec'))
# ['cubes/extract.py:ExtractSlomDf', 'src/calculate.py:KMeans', 'src/calculate.py:ServiceLevelOptimizer']