Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_conf_scope_contains_presets():
conf_dict = ConfigDict({"answer": 42})
cfg = conf_dict(preset={"a": 21, "unrelated": True})
assert set(cfg.keys()) == {"a", "answer", "unrelated"}
assert cfg["a"] == 21
assert cfg["answer"] == 42
assert cfg["unrelated"] is True
def test_config_dict_raises_on_invalid_keys(key):
with pytest.raises(KeyError):
ConfigDict({key: True})
def test_fixed_subentry_of_preset():
config_dict = ConfigDict({})
cfg = config_dict(preset={"d": {"a": 1, "b": 2}}, fixed={"d": {"a": 10}})
assert set(cfg.keys()) == {"d"}
assert set(cfg["d"].keys()) == {"a", "b"}
assert cfg["d"]["a"] == 10
assert cfg["d"]["b"] == 2
def conf_dict():
cfg = ConfigDict(
{
"a": 1,
"b": 2.0,
"c": True,
"d": "string",
"e": [1, 2, 3],
"f": {"a": "b", "c": "d"},
}
)
return cfg
def test_add_config_dict_sequential():
# https://github.com/IDSIA/sacred/issues/409
adict = ConfigDict(dict(dictnest2={"key_1": "value_1", "key_2": "value_2"}))
bdict = ConfigDict(
dict(
dictnest2={"key_2": "update_value_2", "key_3": "value3", "key_4": "value4"}
)
)
final_config = bdict(preset=adict())
assert final_config == {
"dictnest2": {
"key_1": "value_1",
"key_2": "update_value_2",
"key_3": "value3",
"key_4": "value4",
}
def _create_config_dict(cfg_or_file, kw_conf):
if cfg_or_file is not None and kw_conf:
raise ValueError(
"cannot combine keyword config with " "positional argument"
)
if cfg_or_file is None:
if not kw_conf:
raise ValueError("attempted to add empty config")
return ConfigDict(kw_conf)
elif isinstance(cfg_or_file, dict):
return ConfigDict(cfg_or_file)
elif isinstance(cfg_or_file, str):
if not os.path.exists(cfg_or_file):
raise OSError("File not found {}".format(cfg_or_file))
abspath = os.path.abspath(cfg_or_file)
return ConfigDict(load_config_file(abspath))
else:
raise TypeError("Invalid argument type {}".format(type(cfg_or_file)))
def _create_config_dict(cfg_or_file, kw_conf):
if cfg_or_file is not None and kw_conf:
raise ValueError(
"cannot combine keyword config with " "positional argument"
)
if cfg_or_file is None:
if not kw_conf:
raise ValueError("attempted to add empty config")
return ConfigDict(kw_conf)
elif isinstance(cfg_or_file, dict):
return ConfigDict(cfg_or_file)
elif isinstance(cfg_or_file, str):
if not os.path.exists(cfg_or_file):
raise OSError("File not found {}".format(cfg_or_file))
abspath = os.path.abspath(cfg_or_file)
return ConfigDict(load_config_file(abspath))
else:
raise TypeError("Invalid argument type {}".format(type(cfg_or_file)))
def _create_config_dict(cfg_or_file, kw_conf):
if cfg_or_file is not None and kw_conf:
raise ValueError(
"cannot combine keyword config with " "positional argument"
)
if cfg_or_file is None:
if not kw_conf:
raise ValueError("attempted to add empty config")
return ConfigDict(kw_conf)
elif isinstance(cfg_or_file, dict):
return ConfigDict(cfg_or_file)
elif isinstance(cfg_or_file, str):
if not os.path.exists(cfg_or_file):
raise OSError("File not found {}".format(cfg_or_file))
abspath = os.path.abspath(cfg_or_file)
return ConfigDict(load_config_file(abspath))
else:
raise TypeError("Invalid argument type {}".format(type(cfg_or_file)))