【发布时间】:2021-10-29 20:36:15
【问题描述】:
我想在 pydantic 中存储我的 ML 模型的元数据。是否有正确的方法来访问字段类型?我知道你可以做到BaseModel.__fields__['my_field'].type_,但我认为有更好的方法。
我想这样做,如果BaseModel 无法实例化,则非常清楚创建此缺失字段需要哪些数据以及使用哪些方法。像这样:
from pydantic import BaseModel
import pandas as pd
# basic model
class Metadata(BaseModel):
peaks_per_day: float
class PeaksPerDayType(float):
data_required = pd.Timedelta("180D")
data_type = "foo"
@classmethod
def determine(cls, data):
return cls(data)
# use our custom float
class Metadata(BaseModel):
peaks_per_day: PeaksPerDayType
def get_data(data_type, required_data):
# get enough of the appropriate data type
return [1]
# Initial data we have
metadata_json = {}
try:
metadata = Metadata(**metadata_json)
# peaks per day is missing
except Exception as e:
error_msg = e
missing_fields = error_msg.errors()
missing_fields = [missing_field['loc'][0] for missing_field in missing_fields]
# For each missing field use its type hint to find what data is required to
# determine it and access the method to determine the value
new_data = {}
for missing_field in missing_fields:
req_data = Metadata[missing_field].data_required
data_type = Metadata[missing_field].data_type
data = get_data(data_type=data_type, required_data=req_data)
new_data[missing_field] = Metadata[missing_field].determine(data)
metadata = Metadata(**metadata_json, **new_data)
【问题讨论】:
-
需要支持嵌套字段吗?
-
您的意思是例如
Dict[str,Dict[str,float]]吗?我不需要支持所有字段都是str, float, int, List[float]之一。