create a python dictionary based on another dictionary

I have the the following dict that contains the following data:

response = {"status":"ERROR","email":"EMAIL_INVALID","name":"NAME_INVALID"}

I am trying to create a new dict based on 'response' that is suposed to look like the following:

{'api_error': {'list': [{'converted_value': 'No special characters allowed.',
                         'field': 'name',
                         'value': 'NAME_INVALID'},
                        {'converted_value': 'invalid email',
                         'field': 'email',
                         'value': 'EMAIL_INVALID'}],
               'status': 'ERROR'},
 'email': 'EMAIL_INVALID',
 'email_label': 'invalid email',
 'name': 'NAME_INVALID',
 'name_label': 'No special characters allowed.',
 'status': 'ERROR'}

So far I have been able to do the following:

ret = {}
for k in response:
        if k != 'status':
                ret[k+"_label"] = convert(response[k])
                ret[k] = response[k]
        else:
            ret[k] = convert(response[k])

where 'convert' function translates each value of response. for example NAME_INVALID is converted to 'No special characters allowed.' and so on. Here is the output of what above code is doing:

{"status":"ERROR","name_label":"No special characters allowed.",
"email_label":"invalid email","name":"NAME_INVALID","email":"EMAIL_INVALID"}

I am getting problem creating the rest of the dictionary. The one whose key is 'api_error'. What will be the most efficient way of doing that?

Answers


import pprint

response = {"status": "ERROR", "email": "EMAIL_INVALID", "name":
            "NAME_INVALID"}


def convert(label):
    return {'NAME_INVALID': 'No special characters allowed',
            'EMAIL_INVALID': 'invalid email',
            'ERROR': 'ERROR'}[label]

ret = {}
for k in response:
    if k != 'status':
        ret[k + "_label"] = convert(response[k])
        ret[k] = response[k]
        info = {'converted_value': ret[k + "_label"],
                'field': k,
                'value': response[k]}
        (ret.setdefault('api_error', {})
            .setdefault('list', [])
            .append(info))
    else:
        ret[k] = convert(response[k])
        ret.setdefault('api_error', {})['status'] = ret[k]
pprint.pprint(ret)

yields

{'api_error': {'list': [{'converted_value': 'invalid email',
                         'field': 'email',
                         'value': 'EMAIL_INVALID'},
                        {'converted_value': 'No special characters allowed',
                         'field': 'name',
                         'value': 'NAME_INVALID'}],
               'status': 'ERROR'},
 'email': 'EMAIL_INVALID',
 'email_label': 'invalid email',
 'name': 'NAME_INVALID',
 'name_label': 'No special characters allowed',
 'status': 'ERROR'}

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.