:100 } } 从配置里面取数据很简单嘛,把json转成dict,直接data["字段名"]就可以取数据,为了规范化,方便以后随时能知道接口有哪些字段,我使用了dataclass来定义,并用dacite 库加载数据类 定义dataclass from dataclasses import dataclass import dacite @dataclass class Config: id 解决问题 看了一下dacite库的源码,直接当标注类型和数据类型不一致时,强制转换类型,这样就可以把数据标注成自己想要的类型,且自动实现强制转换了,代码如下: from typing import Type , Any import dacite from dacite import Config from dacite.core import _build_value def __build_type _build_value = __build_type 总结 成功通过替换dacite的函数,实现了类型强制转换的效果,舒服了n_n 改第三方库或者内置库有风险,改之前请务必知道自己的行为会产生什么后果
requirements.txt 出现Successfully installed Faker-23.1.0 bs4-0.0.2 colorlog-6.8.2 commonmark-0.9.1 cssselect-1.2.0 dacite
self.json_template = yaml.safe_load(expand_preprocessors(self.yaml_template)) self.template = dacite.from_dict ( Template, hyphen_to_underscore(self.json_template), config=dacite.Config(cast
docker h5py matplotlib numpy pyyaml psutil scikit-learn jinja2 pytest dataclasses-json==0.5.7 dacite