从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]}}
}
}
正如您所看到的,关键字“subselect”对于所有子级别都是通用的,并且其值始终是dict,但它的存在是可选的。嵌套量可能会改变。 我正在搜索一个函数,它允许我执行以下操作:
# desired function that uses recursion I belive.
collect_config(conf, 'trace', 'subselect')
其中'trace'是dicts字典的关键,可能是'subselect'字典作为值。
它应该返回
{'subselect':{
'something': 'jaja',
'roi_spectra': [1, 2],
'roi_x_pixel_spec':
'slice(400, 1200)'
}
或者如果我要求
collect_config(conf, "spec", "subselect")
它应该返回
{'subselect':{
'roi_spectra': [0],
'roi_x_pixel_spec':
'slice(400, 1200)',
'x_property': 'wavenumber'
}
我基本上想要的是一种将键值从顶层传递到较低层并使较低层覆盖顶层值的方法。 非常类似于类的继承,但是使用字典。
所以我需要一个横跨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
然后我可以用
来调用它read_config(conf, "trace", 'subselect', 4)
并获取
{'roi_spectra': [0],
'roi_x_pixel_spec': 'slice(400, 1200)',
'something': 'jaja'}
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]}
而不是
{'roi_spectra': [0, 1, 2], 'test': 'jaja'}
答案 0 :(得分:0)
这是我的看法:
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'}
将max_depth
作为-1
,将在没有深度限制的情况下遍历conf
。