diff options
author | Sergey M․ <dstftw@gmail.com> | 2016-02-07 06:12:53 +0600 |
---|---|---|
committer | Sergey M․ <dstftw@gmail.com> | 2016-02-07 06:12:53 +0600 |
commit | cbecc9b9039d5166185a41ca4d9d6c4d11595c52 (patch) | |
tree | bc0fc97d71af4a36b5deb236f7e957240ea9efa0 /youtube_dl/utils.py | |
parent | b8b465af3e83fb19c1818c2fa83f0c5f753dd917 (diff) |
[utils] Add dict_get convenience method
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r-- | youtube_dl/utils.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 4262ad6ac..652dba59d 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -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) |