def test() -> None: assert hcfg("dataset.name") == "cifar10" assert hcfg("dataset.path") == "/path/to/cifar10" assert hcfg("dataset.splits") == [70, 20, 10] assert hcfg("dataset.classes") == [1, 5, 6] assert hcfg("net.name") == "efficientnet" assert hcfg("net.num_layers") == 20 assert hcfg("net.ckpt_path") == "/path/to/efficientnet" assert hcfg("run_name") == "test" assert hcfg("lr") == 5e-3 assert hcfg("optimizer") == "adam"
def test() -> None: assert hcfg("group_1.param_a") == 1 assert hcfg("group_1.param_b") == 1.2 assert hcfg("group_2.param_c") is True assert hcfg("group_2.param_d") == "param_d" assert hcfg("group_3.param_e.param_f") == "param_f" assert hcfg("group_3.param_e.param_g") == 2 assert hcfg("group_3.param_e.param_h") == 4.56
def func2(): print("Let's get some values from the config:\n") keys = [ "optimizer", "dataset.name", "net.ckpt_path", "params.p3", "params.p3.asfloat" ] for key in keys: print("Key:", key) pprint(hcfg(key)) print("") print( "If you know the type of a config, you can use it to help your linter." ) value = hcfg("params.p2", int) print( f'For instance, if I run hcfg("params.p2", int), I get {value}, of type {type(value)}.' )
def test() -> None: set_cfg("new_cfg", "this is a test") assert hcfg("new_cfg") == "this is a test" set_cfg("group_1.param_a", 5) assert hcfg("group_1.param_a") == 5 assert hcfg("group_1.param_b") == 1.2 set_cfg("group_2.param_d.param_x.param_y", 1e-5) assert hcfg("group_2.param_c") is True assert hcfg("group_2.param_d.param_x.param_y") == 1e-5 set_cfg("group_3", "hello") assert hcfg("group_3") == "hello" set_cfg("group_5", [0.1, 0.5, 0.1]) assert hcfg("group_5") == [0.1, 0.5, 0.1]
def test() -> None: g1pa = hcfg("group_1.param_a", int) assert g1pa == 1 and isinstance(g1pa, int) g1pb = hcfg("group_1.param_b", float) assert g1pb == 1.2 and isinstance(g1pb, float) g2pc = hcfg("group_2.param_c", bool) assert g2pc is True and isinstance(g2pc, bool) g2pd = hcfg("group_2.param_d", str) assert g2pd == "param_d" and isinstance(g2pd, str) g3 = hcfg("group_3", Dict[str, Any]) assert isinstance(g3, dict) g4 = hcfg("group_4", Tuple[int, bool, str]) # type: ignore assert g4 == (1, True, "test") and isinstance(g4, tuple) g5 = hcfg("group_5", List[float]) assert g5 == [0.1, 0.1, 0.1] and isinstance(g5, list) with pytest.raises(TypeError): hcfg("group_1.param_a", str) with pytest.raises(TypeError): hcfg("group_1.param_b", int) with pytest.raises(TypeError): hcfg("group_2.param_c", str) with pytest.raises(TypeError): hcfg("group_2.param_d", bool) with pytest.raises(TypeError): hcfg("group_3", Dict[str, int]) with pytest.raises(TypeError): hcfg("group_4", Tuple[int, float, int]) # type: ignore with pytest.raises(TypeError): hcfg("group_5", List[str])
def check_cfg() -> None: assert hcfg("dataset.name") == "cifar10" assert hcfg("dataset.path") == "/path/to/cifar10" assert hcfg("dataset.splits") == [70, 20, 10] assert hcfg("dataset.classes") == [4, 7, 8] assert hcfg("net.name") == "efficientnet" assert hcfg("net.num_layers") == 20 assert hcfg("net.ckpt_path") == "/path/to/efficientnet" assert hcfg("run_name") == "test" assert hcfg("lr") == 1e-10 assert hcfg("optimizer") == "adam" assert hcfg("new_group.new_sub_group.new_sub_param") == { "t", "e", "s", "t" }
def test() -> None: args = [ "group_1.param_a=5", "group_1.param_c=1.2345", "group_1.param_d=1e-4", "group_1.param_e=False", "-group_3.param_e.param_i:this is a test", "--group_5=[1, 2, 3]", '---group_6.subgroup.subsubgroup.subsubsubgroup:(1.2, "test", True)', '----param_7=\\|!"£$%&/()=?^€[]*@#°§<>,;.:-_+=abcABC123àèìòùç', "param_8:{7, 8, 9}", "param_9:=value", "param_10=:value", "param_11==value", "param_12::value", "#param_13:value", "!param_14=value", ] _parse_args(args) assert hcfg("group_1.param_a") == 5 assert hcfg("group_1.param_b") == 1.2 assert hcfg("group_1.param_c") == 1.2345 assert hcfg("group_1.param_d") == 1e-4 assert hcfg("group_1.param_e") is False assert hcfg("group_2.param_c") is True assert hcfg("group_2.param_d") == "param_d" assert hcfg("group_3.param_e.param_f") == "param_f" assert hcfg("group_3.param_e.param_g") == 2 assert hcfg("group_3.param_e.param_h") == 4.56 assert hcfg("group_3.param_e.param_i") == "this is a test" assert hcfg("group_4") == (1, True, "test") assert hcfg("group_5") == [1, 2, 3] assert hcfg("group_6.subgroup.subsubgroup.subsubsubgroup") == (1.2, "test", True) assert hcfg( "param_7") == '\\|!"£$%&/()=?^€[]*@#°§<>,;.:-_+=abcABC123àèìòùç' assert hcfg("param_8") == {7, 8, 9} assert hcfg("param_9") == "=value" assert hcfg("param_10") == ":value" assert hcfg("param_11") == "=value" assert hcfg("param_12") == ":value" assert hcfg("#param_13") == "value" assert hcfg("!param_14") == "value" wrong_args = [ "key value", "keyvalue", "key-value", "key_value", "=", ":", "-=", "-:", ] for arg in wrong_args: with pytest.raises(ValueError): _parse_args([arg])