libb.trace_value

trace_value(d, attrname)[source]

Get values at all locations of a key in nested dictionary.

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

  • attrname (str) – Key name to find.

Returns:

List of values found at each key location.

Return type:

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_value(l, 'f')
[1]

Multiple Locations:

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

With Missing Key:

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