Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions luxonis_ml/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,37 +170,44 @@ def _merge_recursive(
else:
data[index] = parsed_value
elif isinstance(data, list):
raise ValueError(
"Only int keys are allowed for list values"
)
if key == "+":
data.append(parsed_value)
else:
raise ValueError(
"Only int keys are allowed for list values"
)
else:
data[key] = parsed_value

return

key_tail = ".".join(tail)

if key.isdecimal():
index = int(key)
if key.isdecimal() or key == "+":
if not isinstance(data, list):
raise ValueError(
"int keys are not allowed for non-list values"
)
if index >= len(data):
index = len(data)
if data:
data.append(type(data[0])())
_merge_recursive(data[index], key_tail, value)
else:
# Try to guess type, backtrack if fails
data.append([])
try:
_merge_recursive(data[index], key_tail, value)
except Exception:
data[index] = {}
_merge_recursive(data[index], key_tail, value)
if key == "+":
data.append(type(data[0])())
_merge_recursive(data[-1], key_tail, value)
else:
_merge_recursive(data[index], key_tail, value)
index = int(key)
if index >= len(data):
index = len(data)
if data:
data.append(type(data[0])())
_merge_recursive(data[index], key_tail, value)
else:
# Try to guess type, backtrack if fails
data.append([])
try:
_merge_recursive(data[index], key_tail, value)
except Exception:
data[index] = {}
_merge_recursive(data[index], key_tail, value)
else:
_merge_recursive(data[index], key_tail, value)
else:
if not isinstance(data, dict):
raise ValueError(
Expand Down
8 changes: 6 additions & 2 deletions tests/test_utils/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_config_simple_override(config_file: str):
)


def test_config_list_override(config_file: str):
def test_config_override_as_list(config_file: str):
overrides = ["sub_config.str_sub_param", "sub_param_override"]
cfg = Config.get_config(config_file, overrides)
assert cfg.sub_config.str_sub_param == overrides[1]
Expand Down Expand Up @@ -154,20 +154,24 @@ def test_config_override_list(config_file: str):
"list_config.0.float_list_param": 2.5,
"list_config.0.str_list_param": "test",
"list_config.1.int_list_param": 20,
"list_config.+.int_list_param": 30,
"nested_list_param.0": [30],
"nested_list_param.0.1": 40,
"nested_list_param.0.+": 50,
}
cfg = Config.get_config(config_file, overrides)
# Testing list configurations
assert len(cfg.list_config) == 2
assert len(cfg.list_config) == 3
assert cfg.list_config[0].int_list_param == 10
assert cfg.list_config[0].float_list_param == 2.5
assert cfg.list_config[0].str_list_param == "test"
assert cfg.list_config[1].int_list_param == 20
assert cfg.list_config[1].float_list_param == 1.0
assert cfg.list_config[1].str_list_param is None
assert cfg.list_config[2].int_list_param == 30
assert cfg.nested_list_param[0][0] == 30
assert cfg.nested_list_param[0][1] == 40
assert cfg.nested_list_param[0][2] == 50


def test_config_list_override_json(config_file: str):
Expand Down
Loading