验证Python中的字典 [英] Validate dicts in 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屋!