Created
May 24, 2023 11:32
-
-
Save laurent-laporte-pro/2a9ad1f463c9791ab61840c73b7b4cc8 to your computer and use it in GitHub Desktop.
Advanced Pydantic model -- example of "product" with INI parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum | |
from pydantic import Field | |
from antarest.study.business.utils import FormFieldsBaseModel | |
class SizeEnum(str, Enum): | |
SMALL = "Small" | |
MEDIUM = "Medium" | |
LARGE = "Large" | |
class Product(FormFieldsBaseModel): | |
name: str = Field(description="The name of the product") | |
price: float = Field( | |
gt=0, | |
description="The price of the product (in euro)", | |
ini_path="product-price", | |
) | |
quantity: int = Field( | |
1, | |
gt=0, | |
description="The available quantity of the product", | |
ini_path="qty", | |
start_version=810, | |
) | |
product_size: SizeEnum = Field( | |
SizeEnum.SMALL, | |
description="The size of the product", | |
start_version=810, | |
end_version=820, | |
ini_path="product-size", | |
) | |
# Example usage | |
product_data = { | |
"name": "Example Product", | |
"price": 9.99, | |
"quantity": 100, | |
"productSize": "Medium", | |
} | |
product = Product(**product_data) | |
print(product) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment