Fre*_*red 34 tags yaml specifications pyyaml
我在某种程度上得到它,但我还没有看到一个没有提出更多问题而不是答案的例子.
http://rhnh.net/2011/01/31/yaml-tutorial
# Set.new([1,2]).to_yaml
--- !ruby/object:Set
hash:
1: true
2: true
Run Code Online (Sandbox Code Playgroud)
我知道我们正在声明一个Set标签.我不知道后续哈希映射与它有什么关系.我们是在宣布架构吗?有人能给我看一个带有多个标签声明的例子吗?
我已经阅读了规范:http://yaml.org/spec/1.2/spec.html#id2761292
%TAG ! tag:clarkevans.com,2002:
Run Code Online (Sandbox Code Playgroud)
这是宣告架构吗?为了成功解析文件,解析器还需要做些什么吗?某种类型的模式文件?
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").
Run Code Online (Sandbox Code Playgroud)
为什么与主标记和辅助标记相关,为什么辅助标记引用URI?有这些问题正在解决什么问题?
我似乎看到很多"他们是什么",没有"为什么他们在那里",或"他们用于什么".
Ale*_*ill 19
我对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: # 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
Run Code Online (Sandbox Code Playgroud)
如果没有标记注释,则按其默认类型解析节点.以下是等效的:
- 1: Alex
- !!int "1": !!str "Alex"
Run Code Online (Sandbox Code Playgroud)
这是一个完整的Python程序,使用PyYaml演示标记用法:
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)
Run Code Online (Sandbox Code Playgroud)
规格中提供了更多信息
| 归档时间: |
|
| 查看次数: |
14609 次 |
| 最近记录: |