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.

-------------Problems Reply------------

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

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

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

Category:python Views:0 Time:2011-05-15
Tags: python

Related post

  • Fastest way of deleting certain keys from dict in Python 2010-06-19

    I'm looking for most fastest/effective way of deleting certain keys in a python dict Here are some options for k in somedict.keys(): if k.startswith("someprefix"): del somedict[k] or dict((k, v) for (k, v) in somedict.iteritems() if not k.startswith(

  • Memory efficient int-int dict in Python 2010-10-26

    I need a memory efficient int-int dict in Python that would support the following operations in O(log n) time: d[k] = v # replace if present v = d[k] # None or a negative number if not present I need to hold ~250M pairs, so it really has to be tight.

  • Deep copy of a dict in python 2011-02-24

    I would like to make a deep copy of a dict in python. Unfortunately the .deepcopy() method doesn't exist for the dict. How do I do that? >>> my_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]} >>> my_copy = my_dict.deepcopy() Traceback (most

  • Sorting a dict in Python 2011-10-05

    I want to sort the dict in python. As I am new I don't know where I am going wrong. The below code does sort but the first two entries only. Pls advice scorecard ={} result_f = open("results.txt") for line in result_f: (name,score) =line.split() scor

  • Value-sorted dict for Python? 2012-01-29

    I am interested in a dict implementation for Python that provides an iterating interface to sorted values. I.e., a dict with a "sortedvalues()" function. Naively one can do sorted(dict.values()) but that's not what I want. Every time items are insert

  • is there putAll like method for dict in python? 2012-03-27

    Is there a method like putAll() for dict in python? I did not find a method in the document that seems to do this. Sure I can iterate over the key set and put every thing in the new dict, but I think there might be a better way. Also (this might be a

  • What's the best way to initialize a dict of dicts in Python? 2009-03-16

    This question already has an answer here: What is the best way to implement nested dictionaries in Python? 18 answers A lot of times in Perl, I'll do something like this: $myhash{foo}{bar}{baz} = 1 How would I translate this to Python? So far I have:

  • How to use dict in python? 2009-06-26

    10 5 -1 -1 -1 1 1 0 2 ... If I want to count the number of occurrences of each number in a file, how do I use python to do it? --------------Solutions------------- This is almost the exact same algorithm described in Anurag Uniyal's answer, except us

  • Key-ordered dict in python 2009-08-23

    I am looking for a solid implementation of an ordered associative array (in terms of keys, not of insertion order). More precisely, I am looking for a space-efficent implementation of a int-to-float (or string-to-float for another use case) mapping s

  • Inverse Dict in Python 2009-09-11

    I am trying to create a new dict using a list of values of an existing dict as individual keys. So for example: dict1 = dict({'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}) and I would like to obtain: dict2 = dict({1:['a','b','c'], 2:['a','b','c'], 3:['a','

  • Is there a way to extract a dict in Python into the local namespace? 2010-02-23

    This question already has an answer here: Adding dynamic class to callers namespace 1 answer PHP has a function called extract() which takes an associative array as the argument and creates local variables out of the keys whose values are assigned to

  • How to create new list of dicts in python by consolidating dicts in another list? 2010-09-22

    In Python, I have a list of dicts as follows: orig_list = [ {'first_name': u'Jake', 'last_name': u'Sarson', 'team': u'TeamOne', 'display_name': u'AVG', 'value': 7.0}, {'first_name': u'Mike', 'last_name': u'Walsh', 'team': u'TeamTwo', 'display_name':

  • Convert a space-delimited tree to useful dict in python 2010-11-24

    I have an output (a list) of items, like such: Root Branch1 LeafA LeafB Branch2 LeafC LeafZ LeafD They are all two-space delimited. I want build a logical representation of this list without the leading spaces, and retain the parent-child relationshi

  • How to compare two lists of dicts in Python? 2011-06-13

    How do I compare two lists of dict? The result should be the odd ones out from the list of dict B. Example: ldA = [{'user':"nameA", 'a':7.6, 'b':100.0, 'c':45.5, 'd':48.9}, {'user':"nameB", 'a':46.7, 'b':67.3, 'c':0.0, 'd':5.5}] ldB =[{'user':"nameA"

  • How to traverse a dict in python and make two separate dict out of it? 2011-06-15

    data = "GEOMETRYCOLLECTION (POINT (-8.9648437500000000 -4.1308593750000000), POINT (2.0214843750000000 -2.6367187500000000), POINT (-1.4062500000000000 -11.1621093750000000), POINT (-11.9531250000000000,-10.8984375000000000), POLYGON ((-21.6210937500

  • What is the proper way to format a multi-line dict in Python? 2011-06-17

    In Python, I want to write a multi-line dict in my code. There are a couple of ways one could format it. Here are a few that I could think of: mydict = { "key1": 1, "key2": 2, "key3": 3, } mydict = { "key1": 1, "key2": 2, "key3": 3, } mydict = { "key

  • Sorting a list of dicts in python, using another list as a "filter"? 2011-07-13

    b = [{'id': 'a'}, {'id': 'c'}, {'id': 'b'}, {'id': 'e'}] I need this to become: b = [{'id': 'a'}, {'id': 'c'}, {'id': 'e'}, {'id': 'b'}] This new order is defined by another list. my_filter = ['a', 'c', 'e', 'b'] ...as you can see, the list with dict

  • Funny behavior using urlretrieve on a dict in Python 2011-07-18

    I wrote some code to download some files from URLs, which I put in a dict, however, when I run the script, everything runs fine, except in the end when it goes to download the files, it creates creates one file with one of the names, then I see that

  • Creating a new dict in Python 2011-12-08

    I want to build up a dictionary in python and all of example that i see are instantiating a dictionary from a list, etc . . How do I create a new empty dictionary in python --------------Solutions------------- try to call dict with no parameters new_

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.160 (s). 11 q(s)