2020-12-04 Unit package/serialization working session
Date
Dec 4, 2020
Participants
@Jeffrey Wagner
@Matt Thompson
@Simon Boothroyd
Discussion topics
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 | @Matt Thompson Try wiring up init-kwarg-for-private-attribute-that-instead-takes-its-public-name @Matt Thompson See if a single type can handle implicit- and explicit-unit quanitites (are serialize efficiently if unit is the default) @Matt Thompson Come up with different possible design patterns against which Simon’s pydantic-quantity strategy can be compared. |
Potential Model Design
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]
# }