Skip to content

Commit

Permalink
refactor(transforms): Minor value adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vasenin committed Nov 15, 2021
1 parent e7eed25 commit 05531e7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
6 changes: 2 additions & 4 deletions ntc/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _merge(cfg: CN) -> None:
return _merge


def _flat_to_structured(kv: Dict[str, Any], sep: str = ".") -> Dict[str, Any]:
def _flat_to_structured(kv: Dict[str, Any], sep=".") -> Dict[str, Any]:
"""
>>> _flat_to_structured({"a.b.c": 1, "a.b2": 2})
{"a": {"b": {"c": 1}, "b2": 2}}
Expand All @@ -35,9 +35,7 @@ def _flat_to_structured(kv: Dict[str, Any], sep: str = ".") -> Dict[str, Any]:


def load_from_key_value(kv: Dict[str, str]):
structured = _flat_to_structured(kv)

def _merge(cfg: CN) -> None:
cfg.update(structured)
cfg.update(_flat_to_structured(kv))

return _merge
6 changes: 3 additions & 3 deletions tests/data/base_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
cfg.NAME = CL(None, str)
cfg.DICT = CN()
cfg.DICT.INT = 1
cfg.DICT.FOO = "foo_value_default"
cfg.DICT.FOO2 = "foo2_value_default"
cfg.DICT.FOO = "Default foo value"
cfg.DICT.FOO2 = "Default foo2 value"
cfg.DICT.X = "X"
cfg.LIST = [1, 2, 3, 4]
cfg.STR = "str_value_default"
cfg.STR = "Default str value"
cfg.CLASS = BaseClass()
cfg.CLASSES = CN(BaseClass)
cfg.SUBCLASS = BaseClass
Expand Down
2 changes: 1 addition & 1 deletion tests/data/transforms/extra.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DICT:
FOO: "foo_value_from_yaml_file"
FOO: "Foo value from yaml"
4 changes: 2 additions & 2 deletions tests/data/transforms/load_from_file_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from tests.data.transforms.load_from_file import cfg

cfg = CN(cfg)
cfg.DICT.FOO = "foo_value_from_changes"
cfg.DICT.FOO2 = "foo2_value_from_changes"
cfg.DICT.FOO = "Foo value from changes"
cfg.DICT.FOO2 = "Foo2 value from changes"
14 changes: 7 additions & 7 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

def test_load_from_file():
cfg = CN.load(DATA_DIR / "load_from_file_changes.py")
assert cfg.DICT.FOO == "foo_value_from_yaml_file"
assert cfg.DICT.FOO2 == "foo2_value_from_changes"
assert cfg.DICT.FOO == "Foo value from yaml"
assert cfg.DICT.FOO2 == "Foo2 value from changes"


def test_load_from_key_value():
from .data.base_cfg import cfg as cfg_base

cfg = cfg_base.inherit()
flat_data = {
"DICT.FOO": "foo_value_from_flat_data",
"STR": "str_value_from_flat_data",
"DICT.FOO": "Foo value from flat data",
"STR": "Str value from flat data",
}
cfg.add_transform(load_from_key_value(flat_data))
cfg.transform()
assert cfg.DICT.FOO == "foo_value_from_flat_data"
assert cfg.DICT.FOO2 == "foo2_value_default"
assert cfg.STR == "str_value_from_flat_data"
assert cfg.DICT.FOO == flat_data["DICT.FOO"]
assert cfg.DICT.FOO2 == "Default foo2 value"
assert cfg.STR == flat_data["STR"]

0 comments on commit 05531e7

Please sign in to comment.