Pydantic使用

Pydantic可以在代码运行时提供类型提示, 数据校验失败时提供友好的错误提示, 使用Python的类型注解来进行数据校验和settings管理

一般使用

from datetime import datetime
from typing import List
from typing import Optional

from pydantic import BaseModel


# 1 定义模型
class User(BaseModel):
    id: int  # 必须字段
    name: str = "John Snow"  # 有默认值,选填字段
    signup_ts: Optional[datetime] = None
    friends: List[int] = []  # 列表中元素是int类型或者可以直接转换成int类型


external_data = {
    "id": "123",
    "signup_ts": "2020-12-22 12:22",
    "friends": [1, 2, "3"],  # "3"是可以int("3")的
}
# 2 检验数据
user = User(**external_data)
# 3 访问数据
print(user.id, user.friends)  # 实例化后调用属性
print(repr(user.signup_ts))
# .dict 方法, 返回数据字典
print(user.dict())

校验失败处理

假如数据检验不通过, 会抛出pydantic.ValidationError

from datetime import datetime

from typing import List
from typing import Optional

from pydantic import BaseModel, ValidationError


class User(BaseModel):
    id: int  # 必须字段
    name: str = "John Snow"  # 有默认值,选填字段
    signup_ts: Optional[datetime] = None
    friends: List[int] = []  # 列表中元素是int类型或者可以直接转换成int类型


external_data = {
    "id": "123",
    "signup_ts": "2020-12-22 12:22",
    "friends": [1, 2, "3"],  # "3"是可以int("3")的
}

try:
    User(id=1, signup_ts=datetime.today(), friends=[1, 2, "not number"])
except ValidationError as e:
    print(e.json())
    """
    [
      {
        "loc": [
          "friends",
          2
        ],
        "msg": "value is not a valid integer",
        "type": "type_error.integer"
      }
    ]
    """

模型类的的属性和方法

from datetime import datetime
from pathlib import Path
from typing import List
from typing import Optional

from pydantic import BaseModel


class User(BaseModel):
    id: int  # 必须字段
    name: str = "John Snow"  # 有默认值,选填字段
    signup_ts: Optional[datetime] = None
    friends: List[int] = []  # 列表中元素是int类型或者可以直接转换成int类型


external_data = {
    "id": "123",
    "signup_ts": "2020-12-22 12:22",
    "friends": [1, 2, "3"],  # "3"是可以int("3")的
}
user = User(**external_data)

# 获得已检验数据的字典数据
print(user.dict())

# 获得已检验数据的json数据
print(user.json())

# 这里是浅拷贝
print(user.copy())

# 通过对象解析
print(User.parse_obj(external_data))

# 通过字符串解析
print(User.parse_raw('{"id": "123", "signup_ts": "2020-12-22 12:22", "friends": [1, 2, "3"]}'))

path = Path('pydantic_tutorial.json')
path.write_text('{"id": "123", "signup_ts": "2020-12-22 12:22", "friends": [1, 2, "3"]}')
# 通过文本解析
print(User.parse_file(path))

# 获得对象 概要
# {'title': 'User', 'type': 'object', 'properties': {'id': ...} }
print(user.schema())
# 获得对象 概要json
# {'title': 'User', 'type': 'object', 'properties': {'id': ...} }
print(user.schema_json())

user_data = {"id": "error", "signup_ts": "2020-12-22 12 22", "friends": [1, 2, 3]}  # id是字符串 是错误的
# 不检验数据直接创建模型类,不建议在construct方法中传入未经验证的数据
print(User.construct(**user_data))

# 获得所有字段
# 定义模型类的时候,所有字段都注明类型,字段顺序就不会乱
print(User.__fields__.keys())

模型嵌套

from datetime import datetime, date

from typing import List
from typing import Optional

from pydantic import BaseModel


class User(BaseModel):
    id: int  # 必须字段
    name: str = "John Snow"  # 有默认值,选填字段
    signup_ts: Optional[datetime] = None
    friends: List[int] = []  # 列表中元素是int类型或者可以直接转换成int类型


external_data = {
    "id": "123",
    "signup_ts": "2020-12-22 12:22",
    "friends": [1, 2, "3"],  # "3"是可以int("3")的
}
user = User(**external_data)


class Sound(BaseModel):
    sound: str


class Dog(BaseModel):
    birthday: date
    weight: float = Optional[None]
    sound: List[Sound]  # 不同的狗有不同的叫声。递归模型(Recursive Models)就是指一个嵌套一个


dogs = Dog(birthday=date.today(), weight=6.66, sound=[{"sound": "wang wang ~"}, {"sound": "ying ying ~"}])
print(dogs.dict())

与ORM结合

from datetime import datetime

from typing import List
from typing import Optional

from pydantic import BaseModel
from pydantic import constr
from sqlalchemy import Column, Integer, String
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class CompanyOrm(Base):
    __tablename__ = 'companies'
    id = Column(Integer, primary_key=True, nullable=False)
    public_key = Column(String(20), index=True, nullable=False, unique=True)
    name = Column(String(63), unique=True)
    domains = Column(ARRAY(String(255)))


class CompanyModel(BaseModel):
    id: int
    # constr用于约束字符串
    public_key: constr(max_length=20)
    name: constr(max_length=63)
    domains: List[constr(max_length=255)]

    class Config:
        orm_mode = True


co_orm = CompanyOrm(
    id=123,
    public_key='foobar',
    name='Testing',
    domains=['example.com', 'foobar.com'],
)

print(CompanyModel.from_orm(co_orm))

验证器

如何使用及参数见passwords_match方法

from pydantic import BaseModel, ValidationError, validator


class UserModel(BaseModel):
    name: str
    username: str
    password1: str
    password2: str

    @validator('name')
    def name_must_contain_space(cls, v, **kwargs):
        if ' ' not in v:
            raise ValueError('must contain a space')
        return v.title()

    @validator('password2')
    def passwords_match(cls, v, values, **kwargs):
        """
        :param v: 当前字段的值: zxcvbn2
        :param values: 已经验证的数据: {'username': 'scolvin', 'password1': 'zxcvbn'}
        :param kwargs: {'field': ModelField(name='password2', type=str, required=True), 
                            'config': <class '__main__.Config'>}
        :return:
        """

        if 'password1' in values and v != values['password1']:
            raise ValueError('passwords do not match')
        return v

    @validator('username')
    def username_alphanumeric(cls, v):
        assert v.isalnum(), 'must be alphanumeric'
        return v


try:
    UserModel(
        name='samuel',
        username='scolvin',
        password1='zxcvbn',
        password2='zxcvbn2',
    )
except ValidationError as e:
    print(e)
    """
    2 validation errors for UserModel
    name
      must contain a space (type=value_error)
    password2
      passwords do not match (type=value_error)
    """

全部字段类型

见官方文档: Field Types

更多其他使用方法见: pydantic-docs

posted @ 2021-12-29 23:34  403·Forbidden  阅读(287)  评论(0编辑  收藏  举报