我正在向我的 Python 代码添加类型提示,并且想知道对加载的 YAML 文件进行类型提示的正确方法是什么,因为它是任意数量的字典的字典。
有没有比输入提示返回加载的 YAML 文件更好的方法Dict[str, Dict[str, Any]]?
这是功能:
def load_yaml(yaml_in: str) -> Dict[str, Dict[str, Any]]:
return yaml.load(open(yaml_in), Loader=yaml.FullLoader)
这是正在加载的 YAML 文件的示例:
VariableMap:
var1: 'time'
var2: 'param_name'
GlobalVariables:
limits:
x-min:
x-max:
y-min:
y-max:
Plots:
plot1:
file:
x_data: 'date'
y_data: [{param: 'param1', label: "param1", color: 'red', linestyle: '-'},
{param: 'param2', label: "param2", color: 'black', linestyle: '--'}]
labels:
title: {label: 'title', fontsize: '9'}
x-axis: {xlabel: 'x-label', fontsize: '9'}
y-axis: {ylabel: 'y-label', fontsize: '9'}
limits:
x-min: 0
x-max: 100
y-min:
y-max:
Figures:
fig1:
shape: [1, 1]
size: [6, 8]
plots: ['plot1']