YAML+PyYAML Notes 8 | PyYAML source code full_load(), full_load_all(), safe_load(), unsafe_load(), unsafe_load_all()

8 | PyYAML源码之full_load,full_load_all,safe_load,unsafe_load,unsafe_load_all

1 yaml.full_load()

  • Source code:
    Insert image description here
  • Function: Parses the first YAMLdocument in the stream and generates the corresponding Pythonobject; parses all tags, excluding those known to be unsafe in untrusted input.
  • Analysis:
# config_yaml02.yaml如下:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  namespace: uat
  name: game-test
  labels:
    app: game-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: game-test
  template:
    metadata:
      labels:
        app: game-test
    spec:
      containers:
      - name: game-test
        image: 192.168.1.5:5000/test/game-test:v1
        resources:
          limits:
            cpu: 1000m
            memory: 3000Mi
          requests:
            cpu: 500m
            memory: 1024Mi
        volumeMounts:
        - name: logs
          mountPath: /data/service/game-test/project/logs
        ports:
        - containerPort: 3008
        readinessProbe:
          tcpSocket:
            port: 3008
          initialDelaySeconds: 15
          periodSeconds: 15
        livenessProbe:
          tcpSocket:
            port: 3008
          initialDelaySeconds: 20
          periodSeconds: 20
      volumes:
      - name: logs
        hostPath:
          path: /tmp/logs
# pyyaml_full_load.py如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/28 
# 文件名称:pyyaml_full_load.py
# 作用:yaml.full_load()
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import yaml

with open('config_pyyaml02.yaml', 'r') as f:
    data = yaml.full_load(f)
print(data)
  • Output:
{
    
    'apiVersion': 'extensions/v1beta1', 'kind': 'Deployment', 'metadata': 
{
    
    'namespace': 'uat', 'name': 'game-test', 'labels': {
    
    'app': 'game-test'}}, 
'spec': {
    
    'replicas': 1, 'selector': {
    
    'matchLabels': {
    
    'app': 'game-test'}}, 
'template': {
    
    'metadata': {
    
    'labels': {
    
    'app': 'game-test'}}, 'spec': 
{
    
    'containers': [{
    
    'name': 'game-test', 'image': '192.168.1.5:5000/test/game-test:v1', 'resources': {
    
    'limits': {
    
    'cpu': '1000m', 'memory': '3000Mi'}, 
'requests': {
    
    'cpu': '500m', 'memory': '1024Mi'}}, 'volumeMounts': [{
    
    'name': 'logs', 'mountPath': '/data/service/game-test/project/logs'}], 'ports': 
[{
    
    'containerPort': 3008}], 'readinessProbe': {
    
    'tcpSocket': {
    
    'port': 3008}, 'initialDelaySeconds': 15, 'periodSeconds': 15}, 'livenessProbe': {
    
    'tcpSocket': 
{
    
    'port': 3008}, 'initialDelaySeconds': 20, 'periodSeconds': 20}}], 'volumes': [{
    
    'name': 'logs', 'hostPath': {
    
    'path': '/tmp/logs'}}]}}}}

2 yaml.full_load_all()

  • Source code:
    Insert image description here
  • Role: Parses all documents in the stream YAMLand generates corresponding Pythonobjects; parses all tags, excluding those known to be unsafe in untrusted input.
  • Analysis:
# config_yaml03.yaml如下:
---
user1:
  name: xiaoming
  age: 23
  password: 123456
---
user2:
  name: xiaozhang
  age: 24
  password: 123456
---
user3:
  name: xiaoli
  age: 25
  password: 123456
# pyyaml_full_load_all.py如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/28 
# 文件名称:pyyaml_full_load_all.py
# 作用:yaml.full_load_all()
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import yaml

f = open('config_pyyaml03.yaml', 'r')
docs = yaml.full_load_all(f)
for doc in docs:
    print(doc)
f.close()

  • Output:
{
    
    'user1': {
    
    'name': 'xiaoming', 'age': 23, 'password': 123456}}
{
    
    'user2': {
    
    'name': 'xiaozhang', 'age': 24, 'password': 123456}}
{
    
    'user3': {
    
    'name': 'xiaoli', 'age': 25, 'password': 123456}}

3 yaml.safe_load()

  • Source code:
    Insert image description here

  • effect:

Parses the first YAML document in the stream and generates the corresponding Python object. Only basic YAML markup is parsed. This is known to be safe against untrusted input.

  • Analysis:
# config_pyyaml04.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
            }
        ]
}
# pyyaml_safe_load.py
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/31 
# 文件名称:pyyaml_safe_load.py
# 作用:yaml.safe_load()
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import yaml

with open("config_pyyaml04.yaml") as f:
    data = yaml.safe_load(f)
print(data)
  • Output:
{
    
    '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'}]}

4 yaml.unsafe_load()

  • Source code:
    Insert image description here
  • effect:

Parses the first YAML document in the stream and generates the corresponding Python object. Parse all tags, even known untrusted input to be unsafe.

5 yaml.unsafe_load_all()

  • Source code:
    Insert image description here
  • effect:

All YAML documents in the stream are parsed and corresponding Python objects are generated. Parse all tags, even known untrusted input to be unsafe.

Guess you like

Origin blog.csdn.net/NoamaNelson/article/details/131978975