Validate dicts in Python

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:


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'} }

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.

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.

回答1:

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



回答2:

You can also try the link below:
https://github.com/sunlightlabs/validictory
Its a great package that helps in validation in an easier way



回答3:

I highly recommend Cerberus for its readability or jsonschema because it uses the JSON Schema standard



回答4:

Webster is a pypi package that does dictionary validation and value regex validation.. this allows you to insure that the dictionary has all the keys its supposed to and the values are more or less what you would expect.

https://pypi.python.org/pypi/Webster



转载请标明出处:Validate dicts in Python
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!