with the new Entity Resolution in Alexa, nested dictionaries become very nested. what's the most pythonic way to refer to a deeply nested value? how do i write the code keeping within 79 characters per line?
this is what i currently have, and while it works, i'm pretty sure there is a better way:
if 'VolumeQuantity' in intent['slots']:
if 'resolutions' in intent['slots']['VolumeQuantity']:
half_decibels = intent['slots']['VolumeQuantity']['resolutions']['resolutionsPerAuthority'][0]['values'][0]['value']['name'].strip()
elif 'value' in intent['slots']['VolumeQuantity']:
half_decibels = intent['slots']['VolumeQuantity']['value'].strip()
Here is a partial sample of the json from alexa
{
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.9a...11",
"timestamp": "2018-03-28T20:37:21Z",
"locale": "en-US",
"intent": {
"name": "RelativeVolumeIntent",
"confirmationStatus": "NONE",
"slots": {
"VolumeQuantity": {
"name": "VolumeQuantity",
"confirmationStatus": "NONE"
},
"VolumeDirection": {
"name": "VolumeDirection",
"value": "softer",
"resolutions": {
"resolutionsPerAuthority": [
{
"authority": "amzn1.er-authority.echo-blah-blah-blah",
"status": {
"code": "ER_SUCCESS_MATCH"
},
"values": [
{
"value": {
"name": "down",
"id": "down"
}
}
]
}
]
},
"confirmationStatus": "NONE"
}
}
},
"dialogState": "STARTED"
}
You are probably refering to nested dictionaries, lists only accept integer indices.
Anyway, (ab?)using the implied line continuation inside parentheses, I think this is pretty readable:
>>> d = {'a':{'b':{'c':'value'}}}
>>> (d
... ['a']
... ['b']
... ['c']
... )
'value'
or alternatively
>>> (d['a']
... ['b']
... ['c'])
'value'