Junior — Middle
Какие схемы данных вы использовали или создавали с помощью библиотеки Pydantic?
sobes.tech AI
Answer from AI
Pydantic is used for creating data schemas that ensure validation and serialization of data in Python. I usually create models that describe data structures for API requests and responses, configuration files, as well as objects that need to be validated before saving to a database.
Example of a simple user schema:
from pydantic import BaseModel, EmailStr
class User(BaseModel):
id: int
name: str
email: EmailStr
is_active: bool = True
# Usage
user = User(id=1, name='Ivan', email='ivan@example.com')
print(user.dict())
Such schemas help ensure that data conforms to the expected format and types, and also simplify working with JSON and other formats.