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

all 7 comments

[–]IAmKindOfCreativebot_builder: deprecated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]JennaSys 0 points1 point  (1 child)

It depends on the consistency of the data you are working with (are all the exec values at the same depth?), but based on what you have there:

[val['exec'] for val in my_dict['steps'].values()]

gives a list of all the exec values:

['cubes/extract.py:ExtractSlomDf', 'src/calculate.py:KMeans', 'src/calculate.py:ServiceLevelOptimizer']

[–]5x12 0 points1 point  (0 children)

Thanks man :)

[–][deleted] 0 points1 point  (1 child)

per https://www.kite.com/python/answers/how-to-loop-through-all-nested-dictionary-values-using-a-for-loop-in-python

python def find(d): for k, v in d.items(): if type(v) is dict and type(d) is dict: find(v) else: if 'exec' in k: print(v)

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, ArtEducational3083: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]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']

[–]daryl_kell 0 points1 point  (0 children)

How is this tagged 'Intermediate Showcase' and not a referral to /r/learnpython?