Skip to content

hdx.utilities.loader

module hdx.utilities.loader

Loading utilities for YAML, JSON etc.

Classes

Functions

  • load_text Load file into a string removing newlines.

  • load_yaml Load YAML file into an ordered dictionary.

  • load_json Load JSON file into an ordered dictionary (dict for Python 3.7+)

  • load_and_merge_yaml Load multiple YAML files that are in dictionary form and merge into one dictionary.

  • load_and_merge_json Load multiple JSON files that are in dictionary form and merge into one dictionary.

  • load_yaml_into_existing_dict Merge YAML file that is in dictionary form into existing dictionary.

  • load_json_into_existing_dict Merge JSON file that is in dictionary form into existing dictionary.

class LoadError()

Bases : Exception

load_text(path: Path | str, encoding: str = 'utf-8', strip: bool = False, replace_newlines: str | None = None, replace_line_separators: str | None = None, loaderror_if_empty: bool = True, default_line_separator: str = '\n')str

Load file into a string removing newlines.

Parameters

  • path : Path | str Path to file

  • encoding : str Encoding of file. Defaults to utf-8.

  • strip : bool Whether to strip whitespace from start and end. Defaults to False.

  • replace_newlines : str | None String with which to replace newlines. Defaults to None (don't replace). (deprecated 2024-02-07)

  • replace_line_separators : str | None String with which to replace newlines. Defaults to None (don't replace).

  • loaderror_if_empty : bool Whether to raise LoadError if file is empty. Default to True.

  • default_line_separator : str line separator to be replaced if replace_line_separators is not None

Returns

  • str String contents of file

Raises

load_yaml(path: Path | str, encoding: str = 'utf-8', loaderror_if_empty: bool = True)Any

Load YAML file into an ordered dictionary.

Parameters

  • path : Path | str Path to YAML file

  • encoding : str Encoding of file. Defaults to utf-8.

  • loaderror_if_empty : bool Whether to raise LoadError if file is empty. Default to True.

Returns

  • Any The data from the YAML file

Raises

load_json(path: Path | str, encoding: str = 'utf-8', loaderror_if_empty: bool = True)Any

Load JSON file into an ordered dictionary (dict for Python 3.7+)

Parameters

  • path : Path | str Path to JSON file

  • encoding : str Encoding of file. Defaults to utf-8.

  • loaderror_if_empty : bool Whether to raise LoadError if file is empty. Default to True.

Returns

  • Any The data from the JSON file

Raises

load_and_merge_yaml(paths: Sequence[str], encoding: str = 'utf-8', loaderror_if_empty: bool = True)Mapping

Load multiple YAML files that are in dictionary form and merge into one dictionary.

Parameters

  • paths : Sequence[str] Paths to YAML files

  • encoding : str Encoding of file. Defaults to utf-8.

  • loaderror_if_empty : bool Whether to raise LoadError if any file is empty. Default to True.

Returns

  • Mapping Dictionary of merged YAML files

load_and_merge_json(paths: Sequence[str], encoding: str = 'utf-8', loaderror_if_empty: bool = True)Mapping

Load multiple JSON files that are in dictionary form and merge into one dictionary.

Parameters

  • paths : Sequence[str] Paths to JSON files

  • encoding : str Encoding of file. Defaults to utf-8.

  • loaderror_if_empty : bool Whether to raise LoadError if any file is empty. Default to True.

Returns

  • Mapping Dictionary of merged JSON files

load_yaml_into_existing_dict(data: MutableMapping, path: Path | str, encoding: str = 'utf-8', loaderror_if_empty: bool = True)MutableMapping

Merge YAML file that is in dictionary form into existing dictionary.

Parameters

  • data : MutableMapping Dictionary to merge into

  • path : Path | str YAML file to load and merge

  • encoding : str Encoding of file. Defaults to utf-8.

  • loaderror_if_empty : bool Whether to raise LoadError if file is empty. Default to True.

Returns

  • MutableMapping YAML file merged into dictionary

load_json_into_existing_dict(data: MutableMapping, path: Path | str, encoding: str = 'utf-8', loaderror_if_empty: bool = True)MutableMapping

Merge JSON file that is in dictionary form into existing dictionary.

Parameters

  • data : MutableMapping Dictionary to merge into

  • path : Path | str JSON file to load and merge

  • encoding : str Encoding of file. Defaults to utf-8.

  • loaderror_if_empty : bool Whether to raise LoadError if file is empty. Default to True.

Returns

  • MutableMapping JSON file merged into dictionary