YAML+PyYAML笔记 6 | PyYAML源码之yaml.scan(),yaml.parse(),yaml.compose

举报
虫无涯 发表于 2023/08/01 09:10:29 2023/08/01
【摘要】 0 yaml文档以下示例来源于网络,便于后续学习用, 文档为config_yaml.yaml。{ name: John Doe, age: 28, hobbies: [hiking, cooking, fishing], address: { city: New York, state: NY, ...

0 yaml文档

  • 以下示例来源于网络,便于后续学习用, 文档为config_yaml.yaml。
{
    name: John Doe,
    age: 28,
    hobbies: [hiking, cooking, fishing],
    address:
        {
            city: New York,
            state: NY,
            street: 100 Main St,
            location:
                {
                 longitude: 40.712776,
                 latitude: -74.005974
                }
        },
    family:
        [
            {
                name: Jane,
                age: 25,
                relation: spouse
            },
            {
                name: Joe,
                age: 3,
                relation: son
            }
        ]
}

1 yaml.scan()

  • 源码:
    在这里插入图片描述
  • 作用:对给定的stream,生成一个tokens序列;

由于在yaml与其他对象互相转化的过程中,yaml是要经过若干个逻辑阶段,所以yaml中有events和tokens序列的概念。

  • 解析:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/28 
# 文件名称:pyyaml_scan.py
# 作用:pyyaml源码学习
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import yaml

document = """
---
{
    name: John Doe,
    age: 28,
    hobbies: [hiking, cooking, fishing],
    address:
        {
            city: New York,
            state: NY,
            street: 100 Main St,
            location:
                {
                 longitude: 40.712776,
                 latitude: -74.005974
                }
        },
    family:
        [
            {
                name: Jane,
                age: 25,
                relation: spouse
            },
            {
                name: Joe,
                age: 3,
                relation: son
            }
        ]
}
"""

for token in yaml.scan(document):
    print(token)
  • 输出:
StreamStartToken(encoding=None)
DocumentStartToken()
FlowMappingStartToken()
KeyToken()
ScalarToken(plain=True, style=None, value='name')
ValueToken()
ScalarToken(plain=True, style=None, value='John Doe')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='age')
ValueToken()
ScalarToken(plain=True, style=None, value='28')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='hobbies')
ValueToken()
FlowSequenceStartToken()
ScalarToken(plain=True, style=None, value='hiking')
FlowEntryToken()
ScalarToken(plain=True, style=None, value='cooking')
FlowEntryToken()
ScalarToken(plain=True, style=None, value='fishing')
FlowSequenceEndToken()
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='address')
ValueToken()
FlowMappingStartToken()
KeyToken()
ScalarToken(plain=True, style=None, value='city')
ValueToken()
ScalarToken(plain=True, style=None, value='New York')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='state')
ValueToken()
ScalarToken(plain=True, style=None, value='NY')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='street')
ValueToken()
ScalarToken(plain=True, style=None, value='100 Main St')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='location')
ValueToken()
FlowMappingStartToken()
KeyToken()
ScalarToken(plain=True, style=None, value='longitude')
ValueToken()
ScalarToken(plain=True, style=None, value='40.712776')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='latitude')
ValueToken()
ScalarToken(plain=True, style=None, value='-74.005974')
FlowMappingEndToken()
FlowMappingEndToken()
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='family')
ValueToken()
FlowSequenceStartToken()
FlowMappingStartToken()
KeyToken()
ScalarToken(plain=True, style=None, value='name')
ValueToken()
ScalarToken(plain=True, style=None, value='Jane')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='age')
ValueToken()
ScalarToken(plain=True, style=None, value='25')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='relation')
ValueToken()
ScalarToken(plain=True, style=None, value='spouse')
FlowMappingEndToken()
FlowEntryToken()
FlowMappingStartToken()
KeyToken()
ScalarToken(plain=True, style=None, value='name')
ValueToken()
ScalarToken(plain=True, style=None, value='Joe')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='age')
ValueToken()
ScalarToken(plain=True, style=None, value='3')
FlowEntryToken()
KeyToken()
ScalarToken(plain=True, style=None, value='relation')
ValueToken()
ScalarToken(plain=True, style=None, value='son')
FlowMappingEndToken()
FlowSequenceEndToken()
FlowMappingEndToken()
StreamEndToken()

2 yaml.parse()

  • 源码:
    在这里插入图片描述
  • 作用:parse对给定的yaml stream,生成一个events序列;

由于在yaml与其他对象互相转化的过程中,yaml是要经过若干个逻辑阶段,所以yaml中有events和tokens序列的概念。

  • 解析:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/28 
# 文件名称:pyyaml_parse.py
# 作用:yaml.parse()
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import yaml

document = """
---
{
    name: John Doe,
    age: 28,
    hobbies: [hiking, cooking, fishing],
    address:
        {
            city: New York,
            state: NY,
            street: 100 Main St,
            location:
                {
                 longitude: 40.712776,
                 latitude: -74.005974
                }
        },
    family:
        [
            {
                name: Jane,
                age: 25,
                relation: spouse
            },
            {
                name: Joe,
                age: 3,
                relation: son
            }
        ]
}
"""

for event in yaml.parse(document):
    print(event)
  • 输出:
StreamStartEvent()
DocumentStartEvent()
MappingStartEvent(anchor=None, tag=None, implicit=True)
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='name')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='John Doe')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='age')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='28')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='hobbies')
SequenceStartEvent(anchor=None, tag=None, implicit=True)
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='hiking')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='cooking')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='fishing')
SequenceEndEvent()
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='address')
MappingStartEvent(anchor=None, tag=None, implicit=True)
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='city')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='New York')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='state')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='NY')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='street')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='100 Main St')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='location')
MappingStartEvent(anchor=None, tag=None, implicit=True)
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='longitude')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='40.712776')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='latitude')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='-74.005974')
MappingEndEvent()
MappingEndEvent()
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='family')
SequenceStartEvent(anchor=None, tag=None, implicit=True)
MappingStartEvent(anchor=None, tag=None, implicit=True)
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='name')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='Jane')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='age')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='25')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='relation')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='spouse')
MappingEndEvent()
MappingStartEvent(anchor=None, tag=None, implicit=True)
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='name')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='Joe')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='age')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='3')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='relation')
ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='son')
MappingEndEvent()
SequenceEndEvent()
MappingEndEvent()
DocumentEndEvent()
StreamEndEvent()

3 yaml.compose()

  • 源码:
    在这里插入图片描述
  • 作用:解析流中的第一个YAML文档,并产生相应的表示树;
  • 解析:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/28 
# 文件名称:pyyaml_compose.py
# 作用:yaml.compose()
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import yaml

document = """
---
{
    name: John Doe,
    age: 28,
    hobbies: [hiking, cooking, fishing],
    address:
        {
            city: New York,
            state: NY,
            street: 100 Main St,
            location:
                {
                 longitude: 40.712776,
                 latitude: -74.005974
                }
        },
    family:
        [
            {
                name: Jane,
                age: 25,
                relation: spouse
            },
            {
                name: Joe,
                age: 3,
                relation: son
            }
        ]
}
"""

tree = yaml.compose(document)
print(tree)
  • 输出:
MappingNode(tag='tag:yaml.org,2002:map', value=
[(ScalarNode(tag='tag:yaml.org,2002:str', value='name'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='John Doe')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='age'), 
ScalarNode(tag='tag:yaml.org,2002:int', value='28')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='hobbies'), 
SequenceNode(tag='tag:yaml.org,2002:seq', value=
[ScalarNode(tag='tag:yaml.org,2002:str', value='hiking'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='cooking'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='fishing')])), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='address'), 
MappingNode(tag='tag:yaml.org,2002:map', value=
[(ScalarNode(tag='tag:yaml.org,2002:str', value='city'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='New York')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='state'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='NY')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='street'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='100 Main St')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='location'), 
MappingNode(tag='tag:yaml.org,2002:map', value=
[(ScalarNode(tag='tag:yaml.org,2002:str', value='longitude'), 
ScalarNode(tag='tag:yaml.org,2002:float', value='40.712776')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='latitude'), 
ScalarNode(tag='tag:yaml.org,2002:float', value='-74.005974'))]))])), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='family'), 
SequenceNode(tag='tag:yaml.org,2002:seq', value=
[MappingNode(tag='tag:yaml.org,2002:map', value=
[(ScalarNode(tag='tag:yaml.org,2002:str', value='name'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='Jane')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='age'), 
ScalarNode(tag='tag:yaml.org,2002:int', value='25')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='relation'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='spouse'))]), 
MappingNode(tag='tag:yaml.org,2002:map', value=
[(ScalarNode(tag='tag:yaml.org,2002:str', value='name'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='Joe')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='age'), 
ScalarNode(tag='tag:yaml.org,2002:int', value='3')), 
(ScalarNode(tag='tag:yaml.org,2002:str', value='relation'), 
ScalarNode(tag='tag:yaml.org,2002:str', value='son'))])]))])
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。