【问题标题】:I don't understand what a YAML tag is我不明白 YAML 标签是什么
【发布时间】:2013-02-20 10:00:16
【问题描述】:

我在某种程度上得到了它,但我还没有看到一个没有提出比答案更多的问题的例子。

http://rhnh.net/2011/01/31/yaml-tutorial

# Set.new([1,2]).to_yaml
--- !ruby/object:Set 
hash: 
  1: true
  2: true

我知道我们正在声明一个 Set 标记。我不明白后续的哈希映射与它有什么关系。我们是在声明一个模式吗?谁能给我看一个有多个标签声明的例子吗?

我已阅读规范:http://yaml.org/spec/1.2/spec.html#id2761292

%TAG ! tag:clarkevans.com,2002:

这是在声明模式吗?为了成功解析文件,解析器还需要做些什么吗?某种类型的架构文件?

http://www.yaml.org/refcard.html

Tag property: # Usually unspecified.
    none    : Unspecified tag (automatically resolved by application).
    '!'     : Non-specific tag (by default, "!!map"/"!!seq"/"!!str").
    '!foo'  : Primary (by convention, means a local "!foo" tag).
    '!!foo' : Secondary (by convention, means "tag:yaml.org,2002:foo").
    '!h!foo': Requires "%TAG !h! <prefix>" (and then means "<prefix>foo").
    '!<foo>': Verbatim tag (always means "foo").

为什么有一个主要和次要标记是相关的,为什么次要标记引用一个 URI?有了这些解决了什么问题?

我似乎看到了很多“它们是什么”,而没有看到“它们为什么在那里”或“它们的用途”。

【问题讨论】:

  • 在你的第一个例子中,# Set.new([1,2]).to_yaml 实际上是一个 comment - 它是一个 ruby​​ 语句,会在它下面输出 YAML。

标签: tags yaml specifications pyyaml


【解决方案1】:

我对 YAML 了解不多,但我会试一试:

标签用于表示类型。正如您从那里的“参考卡”中看到的那样,使用! 声明了一个标签。 %TAG 指令有点像声明标签的快捷方式。

我将使用 PyYaml 进行演示。 PyYaml 可以将!!python/object: 的二级标签解析为实际的python 对象。双感叹号本身就是一个替换,是!tag:yaml.org,2002: 的缩写,它将整个表达式变成!tag:yaml.org,2002:python/object:。每次我们想创建一个对象时都要输入这个表达式有点笨拙,所以我们使用 %TAG 指令给它一个别名:

%TAG !py! tag:yaml.org,2002:python/object:            # declares the tag alias
---
- !py!__main__.MyClass                                # creates an instance of MyClass

- !!python/object:__main__.MyClass                    # equivalent with no alias

- !<tag:yaml.org,2002:python/object:__main__.MyClass> # equivalent using primary tag

如果您没有标签注释,则节点将按其默认类型进行解析。以下是等价的:

- 1: Alex
- !!int "1": !!str "Alex"

这是一个使用 PyYaml 演示标签用法的完整 Python 程序:

import yaml

class Entity:
    def __init__(self, idNum, components):
        self.id = idNum
        self.components = components
    def __repr__(self):
         return "%s(id=%r, components=%r)" % (
             self.__class__.__name__, self.id, self.components)

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

text = """
%TAG !py! tag:yaml.org,2002:python/object:__main__.
---
- !py!Component &transform
  name: Transform
  
- !!python/object:__main__.Component &render
  name: Render

- !<tag:yaml.org,2002:python/object:__main__.Entity>
  id: 123
  components: [*transform, *render]

- !<tag:yaml.org,2002:int> "3"
"""

result = yaml.load(text)

更多信息请访问spec

【讨论】:

  • 根据 YAML 1.2 规范,我相信你的例子,!! 将扩展为 !&lt;tag:yaml.org,2002:&gt;,本质上是 tag:yaml.org,2002:。而不是!tag:yaml.org,2002:!!python/object 相同,将扩展为 tag:yaml.org,2002:python/object:
猜你喜欢
  • 2020-10-23
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
  • 2010-11-08
  • 2018-01-09
  • 2020-01-24
相关资源
最近更新 更多