def value_at_keypath(obj: Any, keypath: str) -> Any:
  """
  Returns value at given key path which follows dotted-path notation.

    >>> x = dict(a=1, b=2, c=dict(d=3, e=4, f=[2,dict(x='foo', y='bar'),5]))
    >>> assert value_at_keypath(x, 'a') == 1
    >>> assert value_at_keypath(x, 'b') == 2
    >>> assert value_at_keypath(x, 'c.d') == 3
    >>> assert value_at_keypath(x, 'c.e') == 4
    >>> assert value_at_keypath(x, 'c.f.0') == 2
    >>> assert value_at_keypath(x, 'c.f.-1') == 5
    >>> assert value_at_keypath(x, 'c.f.1.y') == 'bar'

  """
  for part in keypath.split('.'):
    if isinstance(obj, dict):
      obj = obj.get(part, {})
    elif type(obj) in [tuple, list]:
      obj = obj[int(part)]
    else:
      obj = getattr(obj, part, {})
  return obj

  Returns value at given key path which follows dotted-path notation.

    >>> x = dict(a=1, b=2, c=dict(d=3, e=4, f=[2,dict(x='foo', y='bar'),5]))
    >>> assert value_at_keypath(x, 'a') == 1
    >>> assert value_at_keypath(x, 'b') == 2
    >>> assert value_at_keypath(x, 'c.d') == 3
    >>> assert value_at_keypath(x, 'c.e') == 4
    >>> assert value_at_keypath(x, 'c.f.0') == 2
    >>> assert value_at_keypath(x, 'c.f.-1') == 5
    >>> assert value_at_keypath(x, 'c.f.1.y') == 'bar'

  
pyinstrument/vendor/keypath.py