Extracts a value from a nested set of dictionaries 'doc' based on a 'key' string. The key string is expected to be of the format 'x.y.z' where each component in the string is a key in a dictionary separated by '.' to denote the next key is in a nested dictionary. Returns th
(doc, key)
| 38 | |
| 39 | |
| 40 | def _get_value_simple(doc, key): |
| 41 | """ |
| 42 | Extracts a value from a nested set of dictionaries 'doc' based on |
| 43 | a 'key' string. |
| 44 | The key string is expected to be of the format 'x.y.z' |
| 45 | where each component in the string is a key in a dictionary separated |
| 46 | by '.' to denote the next key is in a nested dictionary. |
| 47 | |
| 48 | Returns the extracted value from the key specified (if found) |
| 49 | Returns None if the key can not be found |
| 50 | """ |
| 51 | split_key = key.split(".") |
| 52 | if not split_key: |
| 53 | return None |
| 54 | |
| 55 | value = doc |
| 56 | for k in split_key: |
| 57 | if isinstance(value, dict) and k in value: |
| 58 | value = value[k] |
| 59 | else: |
| 60 | return None |
| 61 | return value |
| 62 | |
| 63 | |
| 64 | def _get_value_complex(doc, key): |