mirror of
https://github.com/freqtrade/freqtrade.git
synced 2026-02-20 11:21:44 +00:00
feat: add safe_value_nested function
This commit is contained in:
@@ -125,6 +125,27 @@ def round_dict(d, n):
|
||||
DictMap = dict[str, Any] | Mapping[str, Any]
|
||||
|
||||
|
||||
def safe_value_nested(obj: DictMap, keys: str, default_value=None):
|
||||
"""
|
||||
Search a nested dict for a value.
|
||||
:param obj: dict to search in
|
||||
:param keys: dot separated keys to search for
|
||||
:param default_value: value to return if the key is not found or value is None
|
||||
:return: value found in dict or default_value
|
||||
Sample:
|
||||
>>> d = { 'first' : { 'rows' : { 'pass' : 'dog', 'number' : '1' } } }
|
||||
>>> safe_value_nested(d, "first.rows.pass") == "dog"
|
||||
True
|
||||
"""
|
||||
nested_obj = obj
|
||||
for key in keys.split("."):
|
||||
if key in nested_obj and nested_obj[key] is not None:
|
||||
nested_obj = nested_obj[key]
|
||||
else:
|
||||
return default_value
|
||||
return nested_obj
|
||||
|
||||
|
||||
def safe_value_fallback(obj: DictMap, key1: str, key2: str | None = None, default_value=None):
|
||||
"""
|
||||
Search a value in obj, return this if it's not None.
|
||||
|
||||
Reference in New Issue
Block a user