6 | PyYAML源码之yaml.scan,yaml.parse, yaml.compose
- 0 yaml文档
- 1 yaml.scan()
- 2 yaml.parse()
- 3 yaml.compose()
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'))])]))])