def to_json(self, file_path, return_path=True, allow_overwrite=True, verbose=0): """ Dumps the MinyDict to a json file. Args: file_path (str or pathlib.Path): path (and name) of the file to save to return_path (bool, optional): Whether to return the resulting file's path (as str). Defaults to True. allow_overwrite (bool, optional): Whether to allow overwrites, or raise a FileError if the file already exits. Defaults to True. verbose (int, optional): >0 => print the path. Defaults to 0. Returns: Optional[str]: Path to the dumped file """ p = resolve_path(file_path) if not allow_overwrite and p.exists(): raise FileExistsError( f"Error dumping the MinyDict: file {p} already exists." ) with p.open("w") as f: json.dump(self.to_dict(), f) if verbose > 0: print("Json dumped to:", str(p)) if return_path: return str(p)
def from_json(self, file_path): """ Reads a MinyDict from a json file. Args: file_path (str): Path to the file to load from. """ p = resolve_path(file_path) with p.open("r") as f: return MinyDict(json.load(f))
def from_pickle(self, file_path): """ Reads a MinyDict from a pickle file. Args: file_path (str): Path to the file to load from. """ p = resolve_path(file_path) with p.open("rb") as f: return pkl.load(f)
def load_defaults(default: Union[str, dict, MinyDict]): """ Set the default keys from `defaults`: * str/path -> must point to a yaml/json/pickle file then MinyDict.from_X will be used * dict -> Convert that dictionnary to a resolved MinyDict * list -> Recursively calls load_defaults on each element, then sequentially update an (initially empty) MinyDict to allow for hierarchical defaults. Args: allow (Union[str, dict, MinyDict]): The set of allowed keys as a (Miny)dict or a path to a file that `minydra.MinyDict` will be able to load (as `json`, `pickle` or `yaml`) """ # `defaults` is a path: load it with MinyDict.from_X if isinstance(default, (str, pathlib.Path)): # resolve path to file default = resolve_path(default) # ensure it exists assert default.exists() assert default.is_file() # check for known file formats if default.suffix not in {".json", ".yaml", ".yml", ".pickle", ".pkl"}: raise ValueError(f"{str(default)} is not a valid file extension.") # Load from YAML if default.suffix in {".yaml", ".yml"}: default = MinyDict.from_yaml(default) # Load from Pickle elif default.suffix in {".pickle", ".pkl"}: default = MinyDict.from_pickle(default) # Load from JSON else: default = MinyDict.from_json(default) # `defaults` is a dictionnary: convert it to a resolved MinyDict elif isinstance(default, dict): default = MinyDict(default).resolve() # `defaults` is a list: recursively call load_defaults on each element # then sequentially merge all dictionaries to enable hierarchical defaults elif isinstance(default, list): defaults = [Parser.load_defaults(d) for d in default] default = MinyDict() for d in defaults: default.update(d, strict=False) assert isinstance(default, MinyDict) return default
def from_yaml(self, file_path): """ Reads a MinyDict from a yaml file. Args: file_path (str): Path to the file to load from. """ if yaml is None: raise ModuleNotFoundError( "Cannot import module 'yaml'.\n" + "Install minydra with YAML: $ pip install minydra[yaml]\n" + "Or install it directly: $ pip install PyYAML" ) p = resolve_path(file_path) with p.open("r") as f: return MinyDict(yaml.safe_load(f))
def to_yaml(self, file_path, return_path=True, allow_overwrite=True, verbose=0): """ Dumps the MinyDict to a YAML file. Requires the PyYAML package: $ pip install --upgrade minydra[yaml] Or $ pip install PyYAML Args: file_path (str or pathlib.Path): path (and name) of the file to save to return_path (bool, optional): Whether to return the resulting file's path (as str). Defaults to True. allow_overwrite (bool, optional): Whether to allow overwrites, or raise a FileError if the file already exits. Defaults to True. verbose (int, optional): >0 => print the path. Defaults to 0. Returns: Optional[str]: Path to the dumped file """ if yaml is None: raise ModuleNotFoundError( "Cannot import module 'yaml'.\n" + "Install minydra with YAML: $ pip install minydra[yaml]\n" + "Or install it directly: $ pip install PyYAML" ) p = resolve_path(file_path) if not allow_overwrite and p.exists(): raise FileExistsError( f"Error dumping the MinyDict: file {p} already exists." ) with p.open("w") as f: yaml.safe_dump(self.to_dict(), f) if verbose > 0: print("YAML dumped to:", str(p)) if return_path: return str(p)