简体   繁体   English

isinstance 与 ScalarNode、SequenceNode 和 MappingNode 结合是什么意思?

[英]What does isinstance mean combined with ScalarNode, SequenceNode, and MappingNode?

I have googled without success so I would like what does isinstance means combined with yaml ScalarNode, SequenceNode and MappingNode?我用谷歌搜索没有成功,所以我想结合yaml isinstance 、SequenceNode 和 MappingNode 是什么意思? (I know already what isinstance is) (我已经知道isinstance是什么)

For example例如

if isinstance(v,yaml.ScalarNode):
  #do something
elif isinstance(v,yaml.SequenceNode):
  #something else
elif isinstance(v, yaml.MappingNode):
  #another thing

Node types are a part of YAML's Representation data structure.节点类型是 YAML表示数据结构的一部分。 YAML defines its (de)serialization pipeline as follows: YAML 定义其(反)序列化管道如下:

The Representation is a, potentially cyclic, graph of nodes.表示是一个潜在的循环节点图。 In it, anchors and aliases have been resolved.在其中,锚点和别名已得到解决。 In PyYAML, you typically use subgraphs of this data structure to implement custom constructors and representers that generate your native objects, as indicated by the arrows in the diagram.在 PyYAML 中,您通常使用此数据结构的子图来实现生成本机对象的自定义构造函数和表示器,如图中的箭头所示。

A ScalarNode is a node representing a single scalar in the YAML source or output. ScalarNode是代表 YAML 源或 output 中的单个标量的节点。 A single scalar can be a plain scalar (eg foo ), a quoted scalar ( 'foo' or "foo" ), or a block scalar (starting with | or > ).单个标量可以是普通标量(例如foo )、带引号的标量( 'foo'"foo" )或块标量(以|>开头)。 The scalar content, with escape sequences, newlines, indentation already processed, is available in the field .value as string.标量内容,已处理转义序列、换行符、缩进,可在字段.value中作为字符串使用。 This is even true for values that are by default constructed into non-strings.对于默认构造为非字符串的值也是如此。 For example, true will by default generate a boolean value, but as ScalarNode, it contains the value "true" as string.例如,默认情况下, true将生成 boolean 值,但作为 ScalarNode,它包含值"true"作为字符串。

SequenceNode is a node representing a sequence. SequenceNode是表示序列的节点。 The value field of a SequenceNode contains a list of nodes that correspond to the items in the sequence. SequenceNodevalue字段包含与序列中的项目相对应的节点列表。

MappingNode is a node representing a mapping. MappingNode是表示映射的节点。 The value field of a MappingNode contains a list of tuples, where each tuple consists of the key node and the value node. MappingNodevalue字段包含一个元组列表,其中每个元组由键节点和值节点组成。

All nodes have a field tag that contains the resolved tag of the node.所有节点都有一个字段tag ,其中包含节点的已解析标签。 ie a ScalarNode with value true would typically have the tag yaml.org,2002:bool .即,值为trueScalarNode通常具有标签yaml.org,2002:bool The resolved tag depends on the loader you use, for example if you use PyYAML's BaseLoader , true will resolve to a normal string, which is yaml.org,2002:str .解析后的标签取决于您使用的加载器,例如,如果您使用 PyYAML 的BaseLoader ,则true将解析为普通字符串,即yaml.org,2002:str In any case, if there was an explicit tag on the node (eg !!str , that tag will be in the tag field.在任何情况下,如果节点上有明确的标签(例如!!str ,该标签将在tag字段中。


Coming back to the question, this kind of code is typically used in custom constructors.回到问题上来,这种代码通常用在自定义构造函数中。 They get a node as input and are to produce a native value and return it.他们获取一个节点作为输入,并生成一个本机值并返回它。 Usually, a custom constructor expects a specific kind of node but if you want to do proper error reporting, you still want to check whether you actually got the kind of node you need.通常,自定义构造函数需要一种特定类型的节点,但如果您想要进行正确的错误报告,您仍然需要检查您是否确实获得了所需的节点类型。 For this, you use the code you posted.为此,您使用您发布的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM