...
Item | Notes |
---|---|
Studying unit-bearing fields in pydantic (quantity of list vs. list of quantity vs. arrays) |
|
What will our objects look like? |
|
Action items |
|
Potential Model Design
Code Block |
---|
import numpy
from pint import unit
from pydantic import BaseModel, Field
class MyModel(BaseModel):
_field_1: FloatQuantity["angstrom"] = Field(..., description="")
_field_2: ArrayQuantity["kilojoule / mole"] = Field(..., description="")
def field_1_raw(self) -> float:
return self._field_1
def field_2_raw(self) -> numpy.ndarray:
return self._field_2
@property
def field_1(self) -> Quantity:
return self._field_1 * unit.angstrom
@property
def field_2(self) -> Quantity:
return self._field_2 * unit.kilojoule / unit.mole
my_model = MyModel(
field_1=0.05 * unit.nanometer,
field_2=numpy.zeros((1, 5)) * unit.kilojoule / unit.mole
)
# my_model.json() ->
#
# {
# field_1: 0.5
# field_2: [0, 0, 0, 0, 0]
# } |