A new decorator for pydantic allowing you to define dynamic fields that are computed from other properties
Project description
pydantic-computed
A new decorator for pydantic allowing you to define dynamic fields that are computed from other properties.
Installation
Install the package by running
pip install pydantic_computed
Examples and use cases
A computed integer property
from pydantic import BaseModel
from pydantic_computed import Computed, computed
class ExampleModel(BaseModel):
a: int
b: int
c: Computed[int]
@computed('c')
def calculate_c(a: int, **kwargs):
return a + 1
model = ExampleModel(a=1, b=2)
print(model.c) # Outputs 2
Multiple properties as parameters
from pydantic import BaseModel
from pydantic_computed import Computed, computed
class ExampleModel(BaseModel):
a: int
b: int
c: Computed[int]
@computed('c')
def calculate_c(a: int, b: int):
return a + 1
model = ExampleModel(a=1, b=2)
print(model.c) # Outputs 2
Since all properties are passed as **kwargs to calculate_c, we can use the property names for the parameters
Automatic type conversion
from pydantic import BaseModel
from pydantic_computed import Computed, computed
class ExampleModel(BaseModel):
a: int
b: int
c: Computed[str]
@computed('c')
def calculate_c(a: int, b: int):
return a + b
model = ExampleModel(a=1, b=2)
print(model.c) # Outputs '3' as string
Automatic type conversion happens for the returned value
Complex types
Suppose you set up a FastAPI application where you have users and orders stored in a database. All Models in the database have an automatically generated id. Now you want to be able to dynamically generate links to those objects. E.g. the user with id=3 is accessible on the endpoint http://my-api/users/3 Instead of storing those links in the database you can simply generate them with the computed decorator. example:
from pydantic import BaseModel, Field
from pydantic_computed import Computed, computed
class Link(BaseModel):
href: str
method: str
class SchemaLinked(BaseModel):
id: int
base_url: str
link: Computed[Link]
@computed('link')
def compute_link( id: int, base_url: str):
return Link(href=f'{base_url}/{id}', method='GET')
class User(SchemaLinked):
base_url: str = Field('/users', exclude=True)
username: str
class Order(SchemaLinked):
base_url: str = Field('/orders', exclude=True)
user: User
user = User(id=3, username='exampleuser')
user.json()
"""
{
id: 3,
username: "exampleuser",
link: {
href: "/users/3",
method: "GET"
}
}
"""
order = Order(id=2, user=user)
order.json()
"""
{
id: 2,
link: {
href: "/orders/2",
method: "GET"
},
user: {
id: 3,
username: "exampleuser",
link: {
href: "/users/3",
method: "GET"
}
}
}
"""
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pydantic_computed-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cc3d635267dff306cf1da708582358ebe0f76ed11b1a1d550d37ef7e7c6852a |
|
MD5 | 9ed77260ca4b3afa4ce6a3d155b24954 |
|
BLAKE2b-256 | 8c598e4cb1a92d82107d7c2bdfb61caf5c2b031a309d246f3721ac7d4655489f |