Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate dicts in 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'} }

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.

like image 582
Galmi Avatar asked May 15 '11 23:05

Galmi


2 Answers

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

like image 136
anijhaw Avatar answered Oct 15 '22 05:10

anijhaw


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

like image 38
Vidz Avatar answered Oct 15 '22 05:10

Vidz