Pydantic Post Init
python
import pydantic
class HyperParameters(pydantic.BaseModel):
int = 32
batch_size:
@property
def upload_batch_counts(self):
return 1_000_000 // self.batch_size
class Config: # will not allow undefined fields
= pydantic.Extra.forbid extra
If you want to update an existing property, you can do this:
class User(BaseModel):
str
name: int
age:
def __init__(self, **data):
if data.get("name") == "Charlie":
"age"] = data.get("age", 0) + 30 # increment age by 30 if name is "Charlie"
data[super().__init__(**data)