Pydantic Use Cases in Python
Being pydantic in programming often means being overly concerned with correctness, detail, and specificity. In Python, the pydantic
library is an excellent tool that enforces data validation and type enforcement, making it perfect for such scenarios.
Pydantic is a data validation and settings management library for Python, using Python type annotations. It helps to: - Validate input data. - Enforce type constraints. - Simplify data parsing and transformation.
pip install pydantic
from pydantic import BaseModel, ValidationError
# Define a model
class User(BaseModel):
id: int
name: str
email: str
age: int
# Valid input
try:
user = User(id=1, name="John Doe", email="john.doe@example.com", age=30)
print(user)
except ValidationError as e:
print(e)
# Invalid input
try:
invalid_user = User(id="one", name="John Doe", email="john.doe@example.com", age="thirty")
except ValidationError as e:
print(e)
Output:
1. For valid input:
id=1 name='John Doe' email='john.doe@example.com' age=30
2. For invalid input:
2 validation errors for User
id
value is not a valid integer (type=type_error.integer)
age
value is not a valid integer (type=type_error.integer)
from typing import List
from pydantic import BaseModel
class Address(BaseModel):
city: str
zip_code: str
class User(BaseModel):
id: int
name: str
addresses: List[Address]
data = {
"id": 1,
"name": "John Doe",
"addresses": [
{"city": "New York", "zip_code": "10001"},
{"city": "Los Angeles", "zip_code": "90001"}
]
}
user = User(**data)
print(user)
Output:
id=1 name='John Doe' addresses=[Address(city='New York', zip_code='10001'), Address(city='Los Angeles', zip_code='90001')]
from pydantic import BaseModel, Field
class Product(BaseModel):
name: str
price: float = Field(..., gt=0, description="Price must be greater than zero")
quantity: int = Field(..., ge=0, description="Quantity must be zero or more")
# Valid input
product = Product(name="Laptop", price=999.99, quantity=10)
print(product)
# Invalid input
try:
invalid_product = Product(name="Laptop", price=-10, quantity=-5)
except ValidationError as e:
print(e)
Output:
1. For valid input:
name='Laptop' price=999.99 quantity=10
2. For invalid input:
2 validation errors for Product
price
ensure this value is greater than 0 (type=value_error.number.not_gt; limit_value=0)
quantity
ensure this value is greater than or equal to 0 (type=value_error.number.not_ge; limit_value=0)
gt
, ge
, lt
, le
) to enforce stricter rules.ValidationError
to provide user-friendly feedback.Pydantic is a powerful library for creating strict, pedantic Python applications. It ensures data integrity and type correctness, reducing runtime errors and enhancing application robustness.
Would you like me to build a more advanced real-world example or focus on a specific use case like API integration?