gpt4 book ai didi

python - 多文档 YAML 流中是否可以有跨越所有文档的别名?

转载 作者:太空宇宙 更新时间:2023-11-03 15:56:52 25 4
gpt4 key购买 nike

这就是我想要做的(代码是Python 3):

import ruamel.yaml as yaml
from print import pprint

yaml_document_with_aliases = """
title: test
choices: &C
a: one
b: two
c: three
---
title: test 2
choices: *C
"""

items = list(yaml.load_all(yaml_document_with_aliases))

结果是:

ComposerError: found undefined alias 'C'

当我使用非基于文档的 YAML 文件时,这会按预期工作:

import ruamel.yaml as yaml
from print import pprint

yaml_nodes_with_aliases = """
-
title: test
choices: &C
a: one
b: two
c: three
-
title: test 2
choices: *C
"""

items = yaml.load(yaml_nodes_with_aliases)

pprint(items)

结果:

[{'choices': {'a': 'one', 'b': 'two', 'c': 'three'}, 'title': 'test'},
{'choices': {'a': 'one', 'b': 'two', 'c': 'three'}, 'title': 'test 2'}]

(无论如何我想完成的事情)

<小时/>

由于现在不可能,我正在使用以下脆弱的解决方法:

def yaml_load_all_with_aliases(yaml_text):
if not yaml_text.startswith('---'):
yaml_text = '---\n' + yaml_text
for pat, repl in [('^', ' '), ('^\s*---\s*$', '-'), ('^\s+\.{3}$\n', '')]:
yaml_text = re.sub(pat, repl, yaml_text, flags=re.MULTILINE)
yaml_text = yaml_text.strip()
return yaml.safe_load(yaml_text)

最佳答案

这里的问题是:

title: test
choices: &C
a: one
b: two
c: three
---
title: test 2
choices: *C

不是一个文档,它们是一个文件中的两个 YAML 文档。 anchor 定义 &C 不会从一个 YAML 文档传递到另一个 YAML 文档,它只能在文档分隔符 --- 之前使用。

如果您愿意将所有 anchor “继承”到单个 YAML 流中的以下文档,您可以在 Composer 类上移植一个新的 compose_document 方法(即猴子修补它):

import sys
import ruamel.yaml

yaml_str = """\
title: test
choices: &C
a: one
b: two
c: three
---
title: test 2
choices: *C
"""


def my_compose_document(self):
self.get_event()
node = self.compose_node(None, None)
self.get_event()
# this prevents cleaning of anchors between documents in **one stream**
# self.anchors = {}
return node

ruamel.yaml.composer.Composer.compose_document = my_compose_document

datas = []
for data in ruamel.yaml.safe_load_all(yaml_str):
datas.append(data)

datas[0]['choices']['a'] = 1
for data in datas:
ruamel.yaml.round_trip_dump(data, sys.stdout, explicit_start=True)

给出:

---
title: test
choices:
a: 1
b: two
c: three
---
title: test 2
choices:
a: one
b: two
c: three

请注意,这将为您提供带有键 abc 的字典的副本

(如果关键排序和注释的保存很重要,请使用 round_trip_load_all 而不是 safe_load_all)

关于python - 多文档 YAML 流中是否可以有跨越所有文档的别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701983/

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