Created
January 30, 2016 01:32
-
-
Save magixx/1fd2d4b84d0b17bc6093 to your computer and use it in GitHub Desktop.
update keys with path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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