Dict¶
Utility functions for dictionaries.
- disseminate.utils.dict.find_entry(dicts, *keys, suffix=None, sep='_')¶
Search dictionaries for entries with string keys constructed from ordred combinations of the specified keys.
Dictionaries are searched in order of preference. String keys are constructed with parts constructed by joining the parts with the sep string.
- Parameters
- dictsUnion[dict, Tuple[dict], List[dict]]
One or more dictionaries to find entries in.
- keysstr
One or more items to construct keys.
- suffixOptional[str]
If specified, try appending the given suffix to generate the combined key before trying the combined key itself.
- sepOptional[str]
The separator for joining key parts into a key.
- Returns
- value
The value from the dict for the best match.
- Raises
- KeyError
Raised if a key was not found.
Examples
>>> d = {'starter_middle1_term': 1, ... 'starter_middle1': 2, ... 'starter_middle2_term': 3, ... 'starter_middle2': 4, ... 'starter_term': 5, ... 'starter': 6} >>> find_entry(d, 'starter', 'middle1', suffix='term') 1 >>> find_entry(d, 'starter', 'middle1') 2 >>> find_entry(d, 'starter', 'middle2', suffix='term') 3 >>> find_entry(d, 'starter', 'middle2' ) 4 >>> find_entry(d, 'starter', 'term') 5 >>> find_entry(d, 'starter') 6