how to safely eval an urlencode-ed dictionary in Python?
Question: I should communicate with an online service that sends me the following data
urlencoded:

    
    
    data_to_process = {...}
    args = {'args0': data_to_process, 'action': 'save'}
    
    result = urllib2.urlopen(..., urllib.urlencode(args), ...)
    

The main data of interest is stored in the `data_to_process` dictionary. I
would like to get back the original dictionary that's supposed to contain only
string, numeric and boolean values. How can you achieve this?

I've tried

    
    
    eval(dict_str,{'__builtins__': None})
    

but this fails for `False` values (and who knows what else).

thanks for any ideas!


Answer: Use `ast.literal_eval`:

    
    
    >>> from ast import literal_eval
    >>> test = repr({"ham": True, 42: "spam", "foo": "bar"})
    >>> test
    "{42: 'spam', 'foo': 'bar', 'ham': True}"
    >>> literal_eval(test)
    {42: 'spam', 'foo': 'bar', 'ham': True}
    

