gpt4 book ai didi

python - 配置字典中的继承

转载 作者:太空狗 更新时间:2023-10-30 00:18:49 27 4
gpt4 key购买 nike

我想要什么

从 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”对所有子级别都是通用的,它的值始终是一个字典,但它的存在是可选的。嵌套的数量可能会改变。我正在寻找一个函数,它允许我执行以下操作:

# desired function that uses recursion I belive.
collect_config(conf, 'trace', 'subselect')

其中 'trace' 是一个字典的键,可能有一个 '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'}

最佳答案

这是我的看法:

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

关于python - 配置字典中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45977322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com