目 录CONTENT

文章目录

Why use Pydantic

Administrator
2024-12-23 / 0 评论 / 0 点赞 / 8 阅读 / 0 字

定义

在 Pydantic 中,术语 “验证” 是指实例化符合指定类型和约束的模型(或其他类型)的过程。Pydantic 保证输出的类型和约束,而不是输入数据。当考虑到当数据无法成功解析为模型实例时会引发 Pydantic 的 ValidationError 时,这种区别就变得很明显了。

虽然术语“parse”和“validation”以前可以互换使用,但展望未来,我们的目标是专门使用“validate”,而“parse”专门用于与 JSON 解析相关的讨论。

性能

Pydantic's core validation logic is implemented in a separate package (pydantic-core), where validation for most types is implemented in Rust. As a result, Pydantic is among the fastest data validation libraries for Python.

序列化

3种序列化方式:

  1. To a Python dict made up of the associated Python objects.

  2. To a Python dict made up only of "jsonable" types.

  3. To a JSON string.

from datetime import datetime

from pydantic import BaseModel


class Meeting(BaseModel):
    when: datetime
    where: bytes
    why: str = 'No idea'


m = Meeting(when='2020-01-01T12:00', where='home')
print(m.model_dump(exclude_unset=True))
#> {'when': datetime.datetime(2020, 1, 1, 12, 0), 'where': b'home'}
print(m.model_dump(exclude={'where'}, mode='json'))
#> {'when': '2020-01-01T12:00:00', 'why': 'No idea'}
print(m.model_dump_json(exclude_defaults=True))
#> {"when":"2020-01-01T12:00:00","where":"home"}

JSON Schema

from datetime import datetime

from pydantic import BaseModel


class Address(BaseModel):
    street: str
    city: str
    zipcode: str


class Meeting(BaseModel):
    when: datetime
    where: Address
    why: str = 'No idea'


print(Meeting.model_json_schema())

Strict mode and data coercion

"Untrusted data can be passed to a model and, after parsing and validation, Pydantic guarantees that the fields of the resultant model instance will conform to the field types defined on the model."

Dataclasses,TypeDicts and more

  1. BaseModel — Pydantic's own super class with many common utilities available via instance methods.

  2. Pydantic dataclasses — a wrapper around standard dataclasses with additional validation performed.

  3. TypeAdapter — a general way to adapt any type for validation and serialization. This allows types like TypedDict and NamedTuple to be validated as well as simple types (like int or timedelta) — all types supported can be used with TypeAdapter.

  4. validate_call — a decorator to perform validation when calling a function.

当然!以下是四种方法的举例说明:

1. BaseModel

BaseModel 是 Pydantic 的核心类,提供了许多实用方法来进行数据验证、序列化和反序列化。

from pydantic import BaseModel, ValidationError

class User(BaseModel):
    id: int
    name: str

# 创建一个 User 实例
user = User(id=1, name="John Doe")
print(user)  # 输出: id=1 name='John Doe'

# 尝试创建一个无效的 User 实例
try:
    invalid_user = User(id="not an int", name="John Doe")
except ValidationError as e:
    print(e)  # 输出: 1 validation error for User id value is not a valid integer

2. Pydantic Dataclasses

Pydantic 的 dataclasses 是对标准 dataclasses 的封装,提供了额外的验证功能。

from pydantic.dataclasses import dataclass

@dataclass
class User:
    id: int
    name: str

# 创建一个 User 实例
user = User(id=1, name="John Doe")
print(user)  # 输出: User(id=1, name='John Doe')

# 尝试创建一个无效的 User 实例
try:
    invalid_user = User(id="not an int", name="John Doe")
except ValidationError as e:
    print(e)  # 输出: 1 validation error for User id value is not a valid integer

3. TypeAdapter

TypeAdapter 是一种通用的方式来适应任何类型进行验证和序列化。它可以处理 TypedDictNamedTuple 以及简单类型。

from pydantic import TypeAdapter

# 定义一个 TypedDict
from typing import TypedDict

class UserDict(TypedDict):
    id: int
    name: str

# 使用 TypeAdapter 进行验证
user_adapter = TypeAdapter(UserDict)

# 验证一个有效的 UserDict
valid_user = {"id": 1, "name": "John Doe"}
user_adapter.validate_python(valid_user)  # 通过验证

# 验证一个无效的 UserDict
invalid_user = {"id": "not an int", "name": "John Doe"}
try:
    user_adapter.validate_python(invalid_user)
except ValidationError as e:
    print(e)  # 输出: 1 validation error for UserDict id value is not a valid integer

4. validate_call

validate_call 是一个装饰器,用于在调用函数时执行参数验证。

from pydantic import validate_call

@validate_call
def greet(name: str, age: int) -> str:
    return f"Hello {name}, you are {age} years old!"

# 调用函数
print(greet("John Doe", 30))  # 输出: Hello John Doe, you are 30 years old!

# 尝试调用函数时传递无效参数
try:
    print(greet("John Doe", "not an int"))
except ValidationError as e:
    print(e)  # 输出: 1 validation error for greet age value is not a valid integer

这些例子展示了 Pydantic 提供的四种主要方法,分别是 BaseModelPydantic DataclassesTypeAdaptervalidate_call,它们各自在不同的场景下提供了强大的数据验证和序列化功能。

文献

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区