libb.trace_key

trace_key(d, attrname)[source]

Trace dictionary key in nested dictionary.

Parameters:
  • d (dict) – Dictionary to search.

  • attrname (str) – Key name to find.

Returns:

List of paths (as lists) to the key.

Return type:

list[list]

Raises:

AttributeError – If key is not found.

Basic Usage:

>>> l=dict(a=dict(b=dict(c=dict(d=dict(e=dict(f=1))))))
>>> trace_key(l,'f')
[['a', 'b', 'c', 'd', 'e', 'f']]

Multiple Locations:

>>> l=dict(a=dict(b=dict(c=dict(d=dict(e=dict(f=1))))), f=2)
>>> trace_key(l,'f')
[['a', 'b', 'c', 'd', 'e', 'f'], ['f']]

With Missing Key:

>>> trace_key(l, 'g')
Traceback (most recent call last):
...
AttributeError: g