python-3.x 如何在CommentedMap上Map,同时保留注解/样式?

eoxn13cs  于 2022-11-18  发布在  Python
关注(0)|答案(1)|浏览(122)

给定一个ruamel.yamlCommentedMap(),和一些转换函数f: CommentedMap → Any,我想生成一个新的CommentedMap,它具有转换后的键和值,但在其他方面尽可能与原始的相似。
如果我不关心保持风格,我可以这样做:

result = {
    f(key) : f(value)
    for key, value in my_commented_map.items()
}

如果我不需要转换密钥(并且我不关心改变原始密钥),我可以这样做:

for key, value in my_commented_map.items():
    my_commented_map[key] = f(value)
mzillmmw

mzillmmw1#

样式和注解信息都通过特殊的属性附加到CommentedMap中。样式可以复制,但注解部分索引到它们出现的行的关键字,如果您转换该关键字,则还需要转换索引的注解。
在你的第一个例子中,你将f()应用于键和值,我将在我的例子中使用单独的函数,所有的键都是大写的,所有的值都是小写的(当然,这只适用于字符串类型的键和值,所以这是例子的限制,而不是解决方案的限制)

import sys
import ruamel.yaml
from ruamel.yaml.comments import CommentedMap as CM
from ruamel.yaml.comments import Format, Comment

yaml_str = """\
# example YAML document
abc: All Strings are Equal  # but some Strings are more Equal then others
klm: Flying Blue
xYz: the End                # for now
"""

def fkey(s):
    return s.upper()

def fval(s):
    return s.lower()

def transform(data, fk, fv):
    d = CM()
    if hasattr(data, Format.attrib):
        setattr(d, Format.attrib, getattr(data, Format.attrib))
    ca = None
    if hasattr(data, Comment.attrib):
        setattr(d, Comment.attrib, getattr(data, Comment.attrib))
        ca = getattr(d, Comment.attrib)
    # as the key mapping could map new keys on old keys, first gather everything
    key_com = {}
    for k in data:
        new_k = fk(k)
        d[new_k] = fv(data[k])
        if ca is not None and k in ca.items:
            key_com[new_k] = ca.items.pop(k)
    if ca is not None:
        assert len(ca.items) == 0
        ca._items = key_com  # the attribute, not the read-only property
    return d

yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_str)

# the following will print any new CommentedMap with curly braces, this just here to check
# if the style attribute copying is working correctly, remove from real code
yaml.default_flow_style = True

data = transform(data, fkey, fval)
yaml.dump(data, sys.stdout)

其给出:

# example YAML document
ABC: all strings are equal  # but some Strings are more Equal then others
KLM: flying blue
XYZ: the end                # for now

请注意:

  • 以上尝试(并且成功)在原始列中开始注解,如果这是不可能的,例如,当变换的键或值占用更多空间时,它被进一步推到右边。
  • 如果你有一个更复杂的数据结构,递归地遍历树,向下遍历Map和序列。在这种情况下,存储(key, value, comment)元组比存储pop()所有键并重新插入存储的值(而不是重建树)可能更容易。

相关问题