[extractor/common] Add support multiple getters in try_get
This commit is contained in:
parent
bae1404893
commit
a32a9a7ef5
|
@ -2103,13 +2103,16 @@ def dict_get(d, key_or_keys, default=None, skip_false_values=True):
|
||||||
|
|
||||||
|
|
||||||
def try_get(src, getter, expected_type=None):
|
def try_get(src, getter, expected_type=None):
|
||||||
try:
|
if not isinstance(getter, (list, tuple)):
|
||||||
v = getter(src)
|
getter = [getter]
|
||||||
except (AttributeError, KeyError, TypeError, IndexError):
|
for get in getter:
|
||||||
pass
|
try:
|
||||||
else:
|
v = get(src)
|
||||||
if expected_type is None or isinstance(v, expected_type):
|
except (AttributeError, KeyError, TypeError, IndexError):
|
||||||
return v
|
pass
|
||||||
|
else:
|
||||||
|
if expected_type is None or isinstance(v, expected_type):
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
def encode_compat_str(string, encoding=preferredencoding(), errors='strict'):
|
def encode_compat_str(string, encoding=preferredencoding(), errors='strict'):
|
||||||
|
|
Loading…
Reference in New Issue