Skip to content

Instantly share code, notes, and snippets.

@magixx
Created January 30, 2016 01:32
Show Gist options
  • Save magixx/1fd2d4b84d0b17bc6093 to your computer and use it in GitHub Desktop.
Save magixx/1fd2d4b84d0b17bc6093 to your computer and use it in GitHub Desktop.
update keys with path
import collections
from pprint import pprint as pp
from copy import copy
smp = {'nine': {'eee': True, 'fff': {'ooo': 3, 'type': 'hsm'}, 'type': 'pc'},
'one': {'bar': 6, 'foo': 5},
'two': {'four': {'bar': 3, 'zzz': 3, 'type': 'g5', 'ge': {'v': 3}}, 'three': {'bar': 9, 'zzz': 9}}}
def find_resource_type(item):
# If the item has an hsm type then this will a a resource of that type
if 'type' in item:
return item['type']
# Check this items parents for clues as to what resource type it is
print 'Unknown type for item: %s' % item
return 'Unknown'
def update_values_with_key(item, parents=''):
for k, v in item.iteritems():
if isinstance(v, collections.Mapping):
print parents
result = update_values_with_key(v, parents + k)
result.update({'__item__': k, '__parents__': copy(parents)})
#res_type = find_resource_type(result)
return item
# def update(d, u):
# for k, v in u.iteritems():
# if isinstance(v, collections.Mapping):
# r = update(d.get(k, {}), v)
# d[k] = r
# else:
# d[k] = u[k]
# return d
pp(update_values_with_key(smp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment