Skip to content

Commit

Permalink
Merge branch 'hhell/stuff' into 'master'
Browse files Browse the repository at this point in the history
feat(transforms): load_from_key_value

See merge request utilities/ntc!23
  • Loading branch information
Artem Vasenin committed Nov 15, 2021
2 parents 5ef5031 + 05531e7 commit 593486c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
24 changes: 23 additions & 1 deletion ntc/transforms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Union
from typing import Any, Dict, Union

import yaml

Expand All @@ -17,3 +17,25 @@ def _merge(cfg: CN) -> None:
cfg.update(yaml.load(f, Loader=yaml.Loader))

return _merge


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}}
"""
structured = {}
for key, value in kv.items():
key_pieces = key.split(sep)
here = structured
for piece in key_pieces[:-1]:
here = here.setdefault(piece, {})
here[key_pieces[-1]] = value
return structured


def load_from_key_value(kv: Dict[str, str]):
def _merge(cfg: CN) -> None:
cfg.update(_flat_to_structured(kv))

return _merge
4 changes: 3 additions & 1 deletion tests/data/base_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
cfg.NAME = CL(None, str)
cfg.DICT = CN()
cfg.DICT.INT = 1
cfg.DICT.FOO = "foo"
cfg.DICT.FOO = "Default foo value"
cfg.DICT.FOO2 = "Default foo2 value"
cfg.DICT.X = "X"
cfg.LIST = [1, 2, 3, 4]
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: "bar"
FOO: "Foo value from yaml"
3 changes: 2 additions & 1 deletion tests/data/transforms/load_from_file_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from tests.data.transforms.load_from_file import cfg

cfg = CN(cfg)
cfg.DICT.FOO = "zoo"
cfg.DICT.FOO = "Foo value from changes"
cfg.DICT.FOO2 = "Foo2 value from changes"
19 changes: 18 additions & 1 deletion tests/test_transforms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
from pathlib import Path

from ntc import CN
from ntc.transforms import load_from_key_value

DATA_DIR = Path(__file__).parent / "data" / "transforms"


def test_load_from_file():
cfg = CN.load(DATA_DIR / "load_from_file_changes.py")
assert cfg.DICT.FOO == "bar"
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",
}
cfg.add_transform(load_from_key_value(flat_data))
cfg.transform()
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 593486c

Please sign in to comment.