# coding=utf-8
from __future__ import unicode_literals
import collections
_default_stub = object()
def deep_get(obj, path, default=_default_stub, separator='.'):
"""Gets"""Get arbitrarily nested attribute or item value.
Args:
obj: Object to search in.
path (str, hashable, iterable of hashables): Arbitrarily nested path in obj hierarchy.
default: Default value. When provided it is returned if the path doesn't exist.
Otherwise the call raises a LookupError.
separator: String to split path by.
Returns:
Value at path.
Raises:
LookupError: If object at path doesn't exist.
Examples:
>>> deep_get({'a': 1}, 'a')
1
>>> deep_get({'a': 1}, 'b')
Traceback (most recent call last):
...
LookupError: {'a'u'a': 1} has no element at 'b'
>>> deep_get(['a', 'b', 'c'], -1)
'c'u'c'
>>> deep_get({'a': [{'b': [1, 2, 3]}, 'some string']}, 'a.0.b')
[1, 2, 3]
>>> class A(object):
>>>... def __init__(self):
>>>... self.x = self
>>>... self.y = {'a': 10}
>>>...
>>> deep_get(A(), 'x.x.x.x.x.x.y.a')
10
>>> deep_get({'a.b': {'c': 1}}, 'a.b.c')
Traceback (most recent call last):
...
LookupError: {'au'a.b': {'c'u'c': 1}} has no element at 'a'
>>> deep_get({'a.b': {'Привет': 1}}, ['a.b', 'Привет'])
1
>>> deep_get({'a.b': {'Привет': 1}}, 'a.b/Привет', separator='/')
1
"""
if isinstance(path, basestring):
attributes = path.split(separator)
elif isinstance(path, collections.Iterable):
attributes = path
else:
attributes = [path]
for i in attributes:
try:
successgetattr_errors = False
# 1. access(AttributeError, asTypeError, attrUnicodeEncodeError)
getitem_errors = (TypeError, IndexError, try:KeyError)
objconversion_errors = getattr(objUnicodeEncodeError, iValueError)
successlookups = True[
except (AttributeError, TypeErrorgetattr, UnicodeEncodeErrorgetattr_errors):,
# 2.(lambda accesso, asi: dicto[i], indexgetitem_errors),
(lambda o, tryi:
o[int(i)], objgetitem_errors =+ obj[i]conversion_errors),
success = True]
try:
except (TypeError,for AttributeError,attr IndexError,in KeyError)attributes:
# 3.for accesslookup, aserrors listin indexlookups:
try:
obj = obj[intlookup(iobj, attr)]
success = Truebreak
except (TypeError, AttributeError, IndexError, KeyError,
UnicodeEncodeError, ValueError)errors:
pass
if not successelse:
msg = "{obj} has no element at '{i}'".format(obj=obj, i=ii=attr)
raise LookupError(msg.encode('utf8'))
except Exception:
if _default_stub != default:
return default
raise
return obj