python小知识

发布时间 2023-08-28 23:52:02作者: 暮雨星辰

Python小知识

浮点数注意事项

# 浮点数精度问题
from decimal import Decimal

print(0.1 + 0.1)  # 0.2
print(0.1 + 0.2)  # 0.30000000000000004

print(Decimal("0.1") + Decimal("0.2"))  # 0.3,注意:参数是字符串

jsonpath提取数据

# pip install jsonpath
from jsonpath import jsonpath

actual = {"data": {"msg": "The verification code is wrong or expired"}, "code": 200}

expected = {"msg": "The verification code is wrong or expired", "code": 200}
for key, value in expected.items():
    print(jsonpath(actual, f"$..{key}"))  # 以列表展示
    print(jsonpath(actual, f"$..{key}")[0])
    assert jsonpath(actual, f"$..{key}")[0] == expected[key]

expected = {"$..msg": "The verification code is wrong or expired", "$..code": 200}
for key, value in expected.items():
    actual_value = jsonpath(actual, key)[0]
    print(actual_value)
    assert actual_value == value

数据替换

import json
import string

# 数据替换
old_headers = '{"Authorization": "new token", "uuid": "new uuid"}'
headers = json.loads(old_headers)
headers["Authorization"] = "adifhsjfnaksldf"
headers["uuid"] = "xxx"
print(headers)

# 新的办法
# old_headers = '{"Authorization": "${token}", "uuid": "${uuid}", "a": "b"}'
old_headers = '{"Authorization": "${token}", "uuid": "old uuid", "a": "${b}"}'
new_data = {"token": "my token", "uuid": "my uuid", "b": "new c"}
t = string.Template(old_headers)
new_headers = t.substitute(new_data)
print(new_headers)


# 封装替换数据的函数
def replace_data(old: str, new: dict):
    t = string.Template(old)
    new_headers = t.substitute(new)
    return new_headers


if __name__ == '__main__':
    print(replace_data(old_headers, new_data))

    old_headers = '{"Authorization": "token", "uuid": "old uuid", "a": "b"}'  # 替换的数据为字符串类型
    new_data = {}
    # 数据替换的用法,如果old_headers没有需要替换的数据,new_data数据为空,得到的new_headers为old_headers数据
    t = string.Template(old_headers)
    new_headers = t.substitute(new_data)
    print(new_headers)
    print(type(new_headers))  # 生成的也是字符串类型化
    print(json.loads(new_headers))  # 转换成字典类型的数据
    print(type(json.loads(new_headers)))

yaml学习

  • 支持的数据类型:字典、列表、字符串、布尔值、整数、浮点数、Null、时间等

  • 基本语法规则:

    • 大小写敏感
    • 使用缩进表示层级关系
    • 相同层级的元素左侧对齐
    • 键值对用冒号 “:” 结构表示,冒号与值之间需用空格分隔
    • 数组前加有 “-” 符号,符号与值之间需用空格分隔
    • None值可用null 和 ~ 表示
    • 多组数据之间使用3横杠---分割
    • #表示注释,但不能在一段代码的行末尾加 #注释,否则会报错

读取yaml文件

# 注释
# 后面的字符串表示建议带引号
# 冒号后面一定要空格
name: "yu"
name1: yu
age: 26

femail: true

# python中的字典
database: { "host": "http://xxx.com", "port: 3306" }
database1:
  host: "http://xxx.com"
  port: 3306

# 列表
hobby: [ "music", "电影" ]
hobby1:
  - "music"
  - "电影"
# pip install pyyaml
import yaml

# 读取yaml
with open("read.yaml", encoding="utf-8") as f:
    # print(f.read())
    data = yaml.safe_load(f)
    print(data, type(data))
# 结果:
# {'name': 'yu', 'name1': 'yu', 'age': 26, 'femail': True, 'database': {'host': 'http://xxx.com', 'port: 3306': None}, 'database1': {'host': 'http://xxx.com', 'port': 3306}, 'hobby': ['music', '电影'], 'hobby1': ['music', '电影']} <class 'dict'>
# pip install pyyaml
import yaml

# 读取yaml
with open("read.yaml", encoding="utf-8") as f:
    result = yaml.load(f, Loader=yaml.FullLoader)
    print(result, type(result))
# 结果:
# {'name': 'yu', 'name1': 'yu', 'age': 26, 'femail': True, 'database': {'host': 'http://xxx.com', 'port: 3306': None}, 'database1': {'host': 'http://xxx.com', 'port': 3306}, 'hobby': ['music', '电影'], 'hobby1': ['music', '电影']} <class 'dict'>