with open(yaml_source) as yaml_file: reader = yaml.safe_load(yaml_file) for key in value,split('.'): try: reader = reader[key] except (KeyError, TypeError): break return reader
i
, x
are terrible variable names - and your code is messy and inconsistent. enumerate
is redundant
- You will never get
IndexError
withget
- Use
with
operator to open file
- PS When you get to a level that the value is not a
dict
- you may break out of the loop
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.