def test_merge(caplog): """Unit test of merge.""" caplog.set_level(logging.INFO) assert merge({1: 1}, {2: 2}) == {1: 1, 2: 2} assert merge({1: 1}, None) == {1: 1} assert merge(None, {2: 2}) == {2: 2} assert merge({1: 1, 3: {4: 4}}, {2: 2}) == {1: 1, 2: 2, 3: {4: 4}}
def update_config( config: Optional[Dict[Any, Any]] = {}, path: Optional[str] = None, filename: Optional[str] = "emmental-config.yaml", update_random_seed: Optional[bool] = True, ) -> None: """Update the config with the configs in root of project and its parents. Note: There are two ways to update the config: (1) uses a config dict to update to config (2) uses path and filename to load yaml file to update config Args: config: The new configuration, defaults to {}. path: The path to the config file, defaults to os.getcwd(). filename: The config file name, defaults to "emmental-config.yaml". update_random_seed: Whether update the random seed or not. """ if config != {}: Meta.config = merge(Meta.config, config, specical_keys="checkpoint_metric") logger.info("Updating Emmental config from user provided config.") if path is not None: tries = 0 current_dir = path while current_dir and tries < MAX_CONFIG_SEARCH_DEPTH: potential_path = os.path.join(current_dir, filename) if os.path.exists(potential_path): with open(potential_path, "r") as f: Meta.config = merge( Meta.config, yaml.load(f, Loader=yaml.FullLoader), specical_keys="checkpoint_metric", ) logger.info( f"Updating Emmental config from {potential_path}.") break new_dir = os.path.split(current_dir)[0] if current_dir == new_dir: logger.info("Unable to find config file. Using defaults.") break current_dir = new_dir tries += 1 if update_random_seed: set_random_seed(Meta.config["meta_config"]["seed"]) Meta.check_config()
def update_config( config: Optional[Dict[Any, Any]] = {}, path: Optional[str] = None, filename: Optional[str] = "emmental-config.yaml", ) -> None: r"""Update the configuration with the configs in root of project and its parents. Note: There are two ways to update the config: (1) uses a config dict to update to config (2) uses path and filename to load yaml file to update config Args: config(dict, optional): The new configuration, defaults to {}. path(str, optional): The path to the config file, defaults to os.getcwd(). filename(str, optional): The config file name, defaults to "emmental-config.yaml". """ if config != {}: Meta.config = merge(Meta.config, config) logger.info("Updating Emmental config from user provided config.") if path is not None: tries = 0 current_dir = path while current_dir and tries < MAX_CONFIG_SEARCH_DEPTH: potential_path = os.path.join(current_dir, filename) if os.path.exists(potential_path): with open(potential_path, "r") as f: Meta.config = merge(Meta.config, yaml.safe_load(f)) logger.info( f"Updating Emmental config from {potential_path}.") break new_dir = os.path.split(current_dir)[0] if current_dir == new_dir: logger.info("Unable to find config file. Using defaults.") break current_dir = new_dir tries += 1