将锚点和别名组合在一行中触发 parserError: 'Expected <block end>, but found '<alias>' [英] Combining anchor and alias in one line in triggers parserError: 'Expected <block end>, but found '<alias>'

查看:16
本文介绍了将锚点和别名组合在一行中触发 parserError: 'Expected <block end>, but found '<alias>'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试组合锚点和别名,以便将值重用到不同名称的多个容器中.

I am trying to combine anchors and aliases in order to reuse values into several containers, under different names.

我尝试了以下代码:

FWL_GV_NANSEN: &fwl_gv_nansen
  dtype: float
  value: 2715.0

FWL_GV_E3_2: &fwl_gv_e32 *fwl_gv_nansen

目标只是让另一个变量 FWL_GV_E3_2 包含与 FWL_GV_NANSEN 相同的信息,我稍后可以参考.

the goal is simply to have another variable FWL_GV_E3_2 containing the same information than FWL_GV_NANSEN that I could refer to later on.

与在 Python(或其他)中定义相同:

Just the same than defining in Python (or other):

a = 5.0
b = a
c = b

但这会触发以下错误消息:

But this triggers the following error message:

yaml.parser.ParserError: 解析块映射时在fwl_2.yml",第 7 行,第 3 列预期 <块结束>,但发现'<别名>'

yaml.parser.ParserError: while parsing a block mapping in "fwl_2.yml", line 7, column 3 expected < block end >, but found '< alias >'

有没有办法将别名的内容分配给用于定义新锚点的变量(通过不同的变量传播初始值?

Is there no way to assign the content of aliases to variable used to defined a new anchor (propagating the initial values through different variables?

PS:也许 YAML 不是最好的语言,因为例如使用 python 的变量会很简单,但我必须使用 YAML

PS: maybe YAML is not the best language for this since it would be trivial using python's variables for example but I have to use YAML

推荐答案

YAML 节点属性(即标签和锚点)只能出现在真实"节点上:集合(块或流)或标量.

The YAML node properties (i.e. tags and anchors) can only occur on "real" nodes: collections (block or flow) or scalars.

您可以从生产规则中看到 c-ns-properties 只能在那里使用,但别名部分也可以使用 明确声明:

You can see from the production rules that c-ns-properties can be used only there, but section for aliases also states explicitly:

请注意,别名节点不得指定任何属性 [...]

Note that an alias node must not specify any properties [...]

<小时>

如果您的 YAML 解析器允许您访问原始锚点/别名,或一些锚点到节点/对象的映射(例如我的 ruamel.yaml 用于往返的 Python 包),是使用标记的标量:


What is possible if your YAML parser gives you access to the original anchor/aliases, or some anchor-to-node/object mapping (such as my ruamel.yaml package for Python when used for round-tripping), is to use a tagged scalar:

FWL_GV_NANSEN: &fwl_gv_nansen
  dtype: float
  value: 2715.0

FWL_GV_E3_2: &fwl_gv_e32 !ref fwl_gv_nansen

使用 !ref 对象的构造函数解析标量 fwl_gv_nansen,以及一些 应用程序 特定的代码.通常,锚点和别名事件由作曲家加载步骤在之前进行解析(如果这样,则该信息不再可用).

with the constructor of the !ref object resolving the scalar fwl_gv_nansen, with some application specific code. Normally the anchor and alias events are resolved by the composer loader step before composing (and if so that information is no longer available).

这篇关于将锚点和别名组合在一行中触发 parserError: 'Expected &lt;block end&gt;, but found '&lt;alias&gt;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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