nested_alter isn't truly recursive
Created by: danhje
nested_alter fails to update elements deep in the hierarchy if the structure of the hierarchy higher up has been altered in the same call. Example:
from nested_lookup import nested_alter
def rename_subkeys(data):
try:
data['renamed'] = data.pop('rename me')
except KeyError:
pass
return data
document = {'key': {'rename me': 1}, 'sub': {'key': {'rename me': 1}}}
altered_document = nested_alter(document, 'key', rename_subkeys)
# The above call works, since renaming the first key doesn't alter how the second key is accessed. Result:
# {'key': {'renamed': 1}, 'sub': {'key': {'renamed': 1}}}
document = {'key': {'rename me': {'rename me': 1}}}
altered_document = nested_alter(document, 'key', rename_subkeys)
# This does not work, only one of the keys are renamed. Result:
# {'key': {'renamed': {'rename me': 1}}}