我不明白 YAML 标签是什么 [英] I don't understand what a YAML tag is

查看:35
本文介绍了我不明白 YAML 标签是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一定程度上理解它,但我还没有看到一个没有提出比答案更多的问题的例子.

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

# Set.new([1,2]).to_yaml--- !ruby/object:Set哈希:1:真的2:真的

我知道我们正在声明一个 Set 标签.我不明白后续的哈希映射与它有什么关系.我们是在声明模式吗?有人可以给我看一个包含多个标签声明的示例吗?

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

%TAG !标签:clarkevans.com,2002:

这是在声明模式吗?为了成功解析文件,解析器是否还需要做其他事情?某种类型的模式文件?

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

Tag 属性:# 通常未指定.none :未指定的标签(由应用程序自动解决).'!:非特定标签(默认情况下,!!map"/!!seq"/!!str").'!foo' : Primary(按照惯例,是指本地的!foo"标签).'!!foo' :次要(按照惯例,意思是tag:yaml.org,2002:foo").'!h!foo': 需要 "%TAG !h! 

"(然后表示

foo").'!<foo>':逐字标记(始终表示foo").

为什么有一个主要和次要标签是相关的,为什么次要标签指的是一个 URI?拥有这些可以解决什么问题?

我似乎看到了很多它们是什么",而没有看到它们为什么在那里"或它们用于什么".

解决方案

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

标签用于表示类型.标签是使用 ! 声明的,正如您在refcard"中看到的那样.那里.%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: # 声明标签别名---- !py!__main__.MyClass # 创建 MyClass 的一个实例- !!python/object:__main__.MyClass # 没有别名的等价物- !<tag:yaml.org,2002:python/object:__main__.MyClass># 等价于使用主标签

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

- 1:亚历克斯- !!int "1": !!str "Alex";

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

import yaml类实体:def __init__(self, idNum, components):self.id = idNumself.components = 组件def __repr__(self):返回%s(id=%r, components=%r)";% (self.__class__.__name__, self.id, self.components)类组件:def __init__(self, name):self.name = 姓名def __repr__(self):返回%s(name=%r)"% (self.__class__.__name__, self.name)文本=""%TAG !py!标签:yaml.org,2002:python/object:__main__.---- !py!Component &transform名称:变换- !!python/object:__main__.Component &render名称:渲染- !<tag:yaml.org,2002:python/object:__main__.Entity>编号:123组件:[*变换,*渲染]- !<tag:yaml.org,2002:int>3""结果 = yaml.load(text)

规范

中提供了更多信息

I get it on some level, but I have yet to see an example that didn't bring up more questions than answers.

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

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

I get that we're declaring a Set tag. I don't get what the subsequent hash mapping has to do with it. Are we declaring a schema? Can someone show me an example with multiple tag declarations?

I've read through the spec: http://yaml.org/spec/1.2/spec.html#id2761292

%TAG ! tag:clarkevans.com,2002:

Is this declaring a schema? Is there something else a parser has to do in order to successfully parse the file? A schema file of some type?

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").

Why is it relevant to have a primary and secondary tag, and why does a secondary tag refer to a URI? What problem is being solved by having these?

I seem to see a lot of "what they are", and no "why are they there", or "what are they used for".

解决方案

I don't know a lot about YAML but I'll give it a shot:

Tags are used to denote types. A tag is declared using ! as you have seen from the "refcard" there. The %TAG directive is kind of like declaring a shortcut to a tag.

I'll demonstrate with PyYaml. PyYaml can parse the secondary tag of !!python/object: as an actual python object. The double exclamation mark is a substitution in itself, short for !tag:yaml.org,2002:, which turns the whole expression into !tag:yaml.org,2002:python/object:. This expression is a little unwieldy to be typing out every time we want to create an object, so we give it an alias using the %TAG directive:

%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

Nodes are parsed by their default type if you have no tag annotations. The following are equivalent:

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

Here is a complete Python program using PyYaml demonstrating tag usage:

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)

More information is available in the spec

这篇关于我不明白 YAML 标签是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆