python - Pydantic 获取字段类型提示
问题描述
我想将我的 ML 模型的元数据存储在 pydantic 中。是否有正确的方法来访问字段类型?我知道你可以做到BaseModel.__fields__['my_field'].type_
,但我认为有更好的方法。
我想这样做,以便如果 aBaseModel
无法实例化,则非常清楚创建此缺少的字段需要哪些数据以及要使用哪些方法。像这样的东西:
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)
解决方案
如果您不需要处理嵌套类,这应该可以
from pydantic import BaseModel, ValidationError
import typing
class PeaksPerDayType(float):
data_required = 123.22
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 required_data
metadata_json = {}
try:
Metadata(**metadata_json)
except ValidationError as e:
field_to_type = typing.get_type_hints(Metadata)
missing_fields = []
for error in e.errors():
if error['type']=='value_error.missing':
missing_fields.append(error['loc'][0])
else:
raise
new_data = {}
for field in missing_fields:
type_ = field_to_type[field]
new_data[field] = get_data(type_.data_type, type_.data_required)
print(Metadata(**metadata_json, **new_data))
peaks_per_day=123.22
我不太确定data_type
or的意义何在get_data
,但我认为它是您想要添加的一些内部逻辑
推荐阅读
- r - 在 R 中安装“systemfit”包时出现问题
- android - 如何在android TextView中水平和垂直居中html文本
- c# - 如何解决窗口服务问题
- php - Google chrome 版本 84.0.4147.125(官方构建)(64 位)在重定向来自第三方的回调函数时破坏应用程序会话
- angular - 无法更改工具提示的自定义位置,而不是其默认位置在剑道中的顶部、左侧、右侧、底部 - Angular
- php - 在 Codeigniter 中选择包含逗号和引号的列
- azure-devops - Azure DevOps Pipeline 的代码格式化任务
- sql-server - SQL 查询(SQL 服务器)将 EST 日期时间转换为 UTC 日期时间
- swift - 扬声器问题 - WebRTC 和 iOS
- mysql - H2 抱怨语法错误,MySQL 接受它 - 但是,错误的语法给出了正确的结果:列 ... 必须在 GROUP BY 列表中;