验证Python中的字典 [英] Validate dicts in Python

查看:79
本文介绍了验证Python中的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在寻找工具或用于验证python中字典的示例.
例如,我有字典:


i looking for tool, or examples to/how to validate dictionaries in python.
For example, i have dict:

test = {'foo' : 'bar', 'nested' : {'foo1' : 'bar1', 'foo2' : 'bar2'} }

现在我必须对其进行验证.可以说,键foo的值必须为布尔值 False 或非空字符串.接下来,如果键foo1的值为bar1,则该键foo2 必须必须位于范围1..10中.我写了简单的函数来做到这一点,但这不是我真正想要的.是的,可以,我可以用if..else测试字典中的每个项目,但是如果字典中有> 50个元素,那就有点不舒服了.

And now i must validate it. Lets say, value for key foo must be boolean False or non-empty string. Next, if key foo1 have value bar1, that key foo2 must be int in range 1..10. I wrote simple function to do this, but this is not what i exactly want. Yea, sure, i can test every single item in dict with if..else, but if dict have >50 elements, then it is a bit not comfortable.

在Python中是否有任何好的工具/库可以做到这一点?我不是在寻找解析器,只是在寻找 fast 是实现此目的的有效方法.

Is there any good tool/lib to do this in Python? I not looking for parsers, only fast and effective way to do this right.

推荐答案

Voluptous是一个很好的工具,可以做到这一点 http://pypi.python.org/pypi/voluptuous

Voluptous is a nice tool that does this http://pypi.python.org/pypi/voluptuous

这篇关于验证Python中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆