Jinja2模板的类型推断。
jinja2schema的Python项目详细描述
为Jinja2模板提供启发式类型推理算法的库。
>>>fromjinja2schemaimportinfer,to_json_schema>>>s=infer('{{ (x.a.b|first).name }}')>>>s{'x':{'a':{'b':[{'name':<scalar>}]}}>>>s=infer(''' ... {% for x in xs %} ... {{ x }} ... {% endfor %} ''')>>>s{'xs':[<scalar>]}>>>to_json_schema(s){'type':'object','required':['xs'],'properties':{'xs':{'type':'array''title':'xs','items':{'title':'x','anyOf':[{'type':'string'},{'type':'number'},{'type':'boolean'},{'type':'null'}],},}}}
更多的例子可以在demo page找到。
安装
pip install jinja2schema