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

1 yaml.full_load()

  • 源码: 在这里插入图片描述
  • 作用:解析流中的第一个YAML文档并生成相应的Python对象;解析所有标记,不包括那些在不受信任的输入中已知不安全的标记。
  • 解析:
# 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(xxxxx)
# 博客:https://blog.csdn.net/NoamaNelson

import yaml

with open('config_pyyaml02.yaml', 'r') as f:
    data = yaml.full_load(f)
print(data)
  • 输出:
{'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()

  • 源码: 在这里插入图片描述
  • 作用:解析流中的所有YAML文档并生成相应的Python对象;解析所有标记,不包括那些在不受信任的输入中已知不安全的标记。
  • 解析:
# 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(xxxxx)
# 博客: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()

  • 输出:
{'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()

  • 源码: 在这里插入图片描述

  • 作用:

解析流中的第一个YAML文档,并生成相应的Python对象。只解析基本的YAML标记。这是已知的对于不可信的输入是安全的。

  • 解析:
# 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(xxxxx)
# 博客:https://blog.csdn.net/NoamaNelson

import yaml

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

  • 源码: 在这里插入图片描述
  • 作用:

解析流中的第一个YAML文档,并生成相应的Python对象。解析所有标签,即使是已知的不受信任的输入不安全。

5 yaml.unsafe_load_all()

  • 源码: 在这里插入图片描述
  • 作用:

分析流中的所有YAML文档,并生成相应的Python对象。解析所有标签,即使是已知的不受信任的输入不安全。