[utils] Add dict_get convenience method
This commit is contained in:
parent
b8b465af3e
commit
cbecc9b903
2 changed files with 23 additions and 0 deletions
|
@ -1717,6 +1717,15 @@ def encode_dict(d, encoding='utf-8'):
|
|||
return dict((encode(k), encode(v)) for k, v in d.items())
|
||||
|
||||
|
||||
def dict_get(d, key_or_keys, default=None):
|
||||
if isinstance(key_or_keys, (list, tuple)):
|
||||
for key in key_or_keys:
|
||||
if d.get(key):
|
||||
return d[key]
|
||||
return default
|
||||
return d.get(key_or_keys, default)
|
||||
|
||||
|
||||
def encode_compat_str(string, encoding=preferredencoding(), errors='strict'):
|
||||
return string if isinstance(string, compat_str) else compat_str(string, encoding, errors)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue