在 YAML 和 python 中 - 如何定义地图中的类型?

发布于 2024-11-17 08:23:27 字数 344 浏览 2 评论 0原文

我很困惑。这就是我想要将我的文档构造为:

release: JULY
files:
    # files
    /foo/config.py:       1.6
    /bar/dao.py:                1.19

然后我想根据 python 中给出的版本返回文件。这部分很简单,但它以浮点数形式返回值,这会带来问题。我想将它们强制转换为字符串而不进行预处理。 (我不想打开文件,循环遍历版本,并在每个条目前面添加 !!python/str )

我认为标签是执行此操作的方法,但我无法对文档进行头尾处理就我如何/在哪里定义标签而言。

有什么指点吗?

I'm stumped. here's what I want to structure my docs as:

release: JULY
files:
    # files
    /foo/config.py:       1.6
    /bar/dao.py:                1.19

I then want to return files based on the release given in python. That part's easy, but it's returning the Values as floats, which poses a problem. I'd like to force them to strings without preprocessing. (I don't want to open a file, loop through the versions, and prepend !!python/str to each entry)

I assume that tags are the way to do this, but I can't make heads or tails of the documentation in terms of how/where I can define a tag.

Any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-11-24 08:23:27

您不需要指定字符串的类型,如果您想将 float 值转储为字符串,只需先对它们调用 str()

>>> d = {'release': 'JULY', 'files': {'/path/file': str(1.6), '/path/file2': str(1.9)}}

>>> print yaml.dump(d, default_flow_style=False)
files:
  /path/file: '1.6'
  /path/file2: '1.9'
release: JULY

如果您想要要序列化/反序列化自定义对象,您可以在某种程度上做到这一点,只需在类中定义 __repr__ 即可:

class Foo:
    def __init__(self, x):
        self.x = x
    def __repr__(self):
        return "%s(%r)" % (self.__class__.__name__, self.x)

>>> f = Foo(0)
>>> foo = yaml.dump(f)
>>> yaml.load(foo)
<<< Foo(0)

You shouldn't need to specify the type for a string, if you want to dump the float values as strings, just call str() on them first:

>>> d = {'release': 'JULY', 'files': {'/path/file': str(1.6), '/path/file2': str(1.9)}}

>>> print yaml.dump(d, default_flow_style=False)
files:
  /path/file: '1.6'
  /path/file2: '1.9'
release: JULY

If you want to serialize/deserialize custom objects, you can do that to some extent, just by defining __repr__ in your class:

class Foo:
    def __init__(self, x):
        self.x = x
    def __repr__(self):
        return "%s(%r)" % (self.__class__.__name__, self.x)

>>> f = Foo(0)
>>> foo = yaml.dump(f)
>>> yaml.load(foo)
<<< Foo(0)
热情消退 2024-11-24 08:23:27

看起来您想要做的是子类 Loader 并调用 add_path_resolver,如下所示:

class MyLoader(yaml.Loader):
    pass
MyLoader.add_path_resolver(SOME_TAG, SOME_PATH, str)

f = open('foo.yaml')
the_data = yaml.load(f, Loader=MyLoader)

但我找不到有关 add_path_resolver 的任何文档以及 SOME_TAGSOME_PATH 应该是什么。

It looks like what you want to do is subclass Loader and call add_path_resolver, something like this:

class MyLoader(yaml.Loader):
    pass
MyLoader.add_path_resolver(SOME_TAG, SOME_PATH, str)

f = open('foo.yaml')
the_data = yaml.load(f, Loader=MyLoader)

But I can't find any documentation on add_path_resolver and what the SOME_TAG and SOME_PATH should be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文