use*_*114 6 python recursion inheritance dictionary
从yaml配置我得到一个python字典,如下所示:
conf = {
'cc0': {
'subselect':
{'roi_spectra': [0], 'roi_x_pixel_spec': 'slice(400, 1200)'},
'spec':
{'subselect': {'x_property': 'wavenumber'}},
'trace':
{'subselect': {'something': 'jaja', 'roi_spectra': [1, 2]}}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,关键字"subselect"对于所有子级别都是通用的,其值始终是dict,但它的存在是可选的.嵌套量可能会改变.我正在寻找一个功能,允许我执行以下操作:
# desired function that uses recursion I belive.
collect_config(conf, 'trace', 'subselect')
Run Code Online (Sandbox Code Playgroud)
其中'trace'是dicts字典的关键,可能是'subselect'字典作为值.
它应该回来
{'subselect':{
'something': 'jaja',
'roi_spectra': [1, 2],
'roi_x_pixel_spec':
'slice(400, 1200)'
}
Run Code Online (Sandbox Code Playgroud)
或者如果我要求
collect_config(conf, "spec", "subselect")
Run Code Online (Sandbox Code Playgroud)
它应该回来
{'subselect':{
'roi_spectra': [0],
'roi_x_pixel_spec':
'slice(400, 1200)',
'x_property': 'wavenumber'
}
Run Code Online (Sandbox Code Playgroud)
我基本上想要的是一种方法,将键的值从顶层传递到较低层,并使较低层覆盖顶层值. 很像类的继承,但有字典.
所以我需要一个横跨dict的函数,找到一个到达所需键的路径(这里是"trace"或"spec"并用更高级别的值填充它的值(这里是"subselect"),但只有更高的值级别值不存在.
我目前有一种看起来如下的实现.
# This traverses the dict and gives me the path to get there as a list.
def traverse(dic, path=None):
if not path:
path=[]
if isinstance(dic, dict):
for x in dic.keys():
local_path = path[:]
local_path.append(x)
for b in traverse(dic[x], local_path):
yield b
else:
yield path, dic
# Traverses through config and searches for the property(keyword) prop.
# higher levels will update the return
# once we reached the level of the name (max_depth) only
# the path with name in it is of interes. All other paths are to
# be ignored.
def collect_config(config, name, prop, max_depth):
ret = {}
for x in traverse(config):
path = x[0]
kwg = path[-1]
value = x[1]
current_depth = len(path)
# We only care about the given property.
if prop not in path:
continue
if current_depth < max_depth or (current_depth == max_depth and name in path):
ret.update({kwg: value})
return ret
Run Code Online (Sandbox Code Playgroud)
我可以随叫它
read_config(conf, "trace", 'subselect', 4)
Run Code Online (Sandbox Code Playgroud)
得到
{'roi_spectra': [0],
'roi_x_pixel_spec': 'slice(400, 1200)',
'something': 'jaja'}
Run Code Online (Sandbox Code Playgroud)
jdehesa几乎就在那里,但我也可以有一个看起来像这样的配置:
conf = {
'subselect': {'test': 'jaja'}
'bg0': {
'subselect': {'roi_spectra': [0, 1, 2]}},
'bg1': {
'subselect': {'test': 'nene'}},
}
collect_config(conf, 'bg0', 'subselect')
{'roi_spectra': [0, 1, 2]}
Run Code Online (Sandbox Code Playgroud)
代替
{'roi_spectra': [0, 1, 2], 'test': 'jaja'}
Run Code Online (Sandbox Code Playgroud)
这是我的看法:
def collect_config(conf, key, prop, max_depth=-1):
prop_val = conf.get(prop, {}).copy()
if key in conf:
prop_val.update(conf[key].get(prop, {}))
return prop_val
if max_depth == 0:
return None
for k, v in conf.items():
if not isinstance(v, dict):
continue
prop_subval = collect_config(v, key, prop, max_depth - 1)
if prop_subval is not None:
prop_val.update(prop_subval)
return prop_val
return None
conf = {
'cc0': {
'subselect':
{'roi_spectra': [0], 'roi_x_pixel_spec': 'slice(400, 1200)'},
'spec':
{'subselect': {'x_property': 'wavenumber'}},
'trace':
{'subselect': {'something': 'jaja', 'roi_spectra': [1, 2]}}
}
}
print(collect_config(conf, "trace", 'subselect', 4))
>>> {'roi_x_pixel_spec': 'slice(400, 1200)',
'roi_spectra': [1, 2],
'something': 'jaja'}
conf = {
'subselect': {'test': 'jaja'},
'bg0': {
'subselect': {'roi_spectra': [0, 1, 2]}},
'bg1': {
'subselect': {'test': 'nene'}},
}
print(collect_config(conf, 'bg0', 'subselect'))
>>> {'roi_spectra': [0, 1, 2], 'test': 'jaja'}
Run Code Online (Sandbox Code Playgroud)
离开max_depth时-1将conf无深度限制地穿越。
| 归档时间: |
|
| 查看次数: |
189 次 |
| 最近记录: |