def test_config_roundtrip_disk(): cfg = Config().from_str(OPTIMIZER_CFG) with make_tempdir() as path: cfg_path = path / "config.cfg" cfg.to_disk(cfg_path) new_cfg = Config().from_disk(cfg_path) assert new_cfg.to_str().strip() == OPTIMIZER_CFG.strip()
def test_config_to_str_creates_intermediate_blocks(): cfg = Config({"optimizer": {"foo": {"bar": 1}}}) assert ( cfg.to_str().strip() == """ [optimizer] [optimizer.foo] bar = 1 """.strip() )
def save_config(config: Config, output_file: Path, is_stdout: bool = False, silent: bool = False) -> None: no_print = is_stdout or silent msg = Printer(no_print=no_print) if is_stdout: print(config.to_str()) else: if not output_file.parent.exists(): output_file.parent.mkdir(parents=True) config.to_disk(output_file, interpolate=False) msg.good("Saved config", output_file) msg.text("You can now add your data and train your pipeline:") variables = ["--paths.train ./train.spacy", "--paths.dev ./dev.spacy"] if not no_print: print( f"{COMMAND} train {output_file.parts[-1]} {' '.join(variables)}" )
def test_config_custom_sort_preserve(): """Test that sort order is preserved when merging and copying configs, or when configs are filled and resolved.""" cfg = {"x": {}, "y": {}, "z": {}} section_order = ["y", "z", "x"] expected = "[y]\n\n[z]\n\n[x]" config = Config(cfg, section_order=section_order) assert config.to_str() == expected config2 = config.copy() assert config2.to_str() == expected config3 = config.merge({"a": {}}) assert config3.to_str() == f"{expected}\n\n[a]" config4 = Config(config) assert config4.to_str() == expected config_str = """[a]\nb = 1\n[c]\n@cats = "catsie.v1"\nevil = true\n\n[t]\n x = 2""" section_order = ["c", "a", "t"] config5 = Config(section_order=section_order).from_str(config_str) assert list(config5.keys()) == section_order filled = my_registry.fill_config(config5) assert filled.section_order == section_order
def test_config_roundtrip_bytes(): cfg = Config().from_str(OPTIMIZER_CFG) cfg_bytes = cfg.to_bytes() new_cfg = Config().from_bytes(cfg_bytes) assert new_cfg.to_str().strip() == OPTIMIZER_CFG.strip()
def test_config_to_str(): cfg = Config().from_str(OPTIMIZER_CFG) assert cfg.to_str().strip() == OPTIMIZER_CFG.strip() cfg = Config({"optimizer": {"foo": "bar"}}).from_str(OPTIMIZER_CFG) assert cfg.to_str().strip() == OPTIMIZER_CFG.strip()
def test_config_roundtrip_disk_respects_path_subclasses(pathy_fixture): cfg = Config().from_str(OPTIMIZER_CFG) cfg_path = pathy_fixture / "config.cfg" cfg.to_disk(cfg_path) new_cfg = Config().from_disk(cfg_path) assert new_cfg.to_str().strip() == OPTIMIZER_CFG.strip()