hdx.utilities.loader

Loading utilities for YAML, JSON etc.

load_text

def load_text(path: str,
              encoding: str = "utf-8",
              strip: bool = False,
              replace_newlines: Optional[str] = None,
              replace_line_separators: Optional[str] = None,
              loaderror_if_empty: bool = True,
              default_line_separator: str = "\n") -> str

[view_source]

Load file into a string removing newlines.

Arguments:

  • 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 Optional[str] - String with which to replace newlines. Defaults to None (don't replace). (deprecated 2024-02-07)
  • replace_line_separators Optional[str] - 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

load_yaml

def load_yaml(path: str,
              encoding: str = "utf-8",
              loaderror_if_empty: bool = True) -> Any

[view_source]

Load YAML file into an ordered dictionary.

Arguments:

  • 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

load_json

def load_json(path: str,
              encoding: str = "utf-8",
              loaderror_if_empty: bool = True) -> Any

[view_source]

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

Arguments:

  • 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

load_and_merge_yaml

def load_and_merge_yaml(paths: ListTuple[str],
                        encoding: str = "utf-8",
                        loaderror_if_empty: bool = True) -> Dict

[view_source]

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

Arguments:

  • paths ListTuple[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:

  • Dict - Dictionary of merged YAML files

load_and_merge_json

def load_and_merge_json(paths: ListTuple[str],
                        encoding: str = "utf-8",
                        loaderror_if_empty: bool = True) -> Dict

[view_source]

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

Arguments:

  • paths ListTuple[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:

  • Dict - Dictionary of merged JSON files

load_yaml_into_existing_dict

def load_yaml_into_existing_dict(data: dict,
                                 path: str,
                                 encoding: str = "utf-8",
                                 loaderror_if_empty: bool = True) -> Dict

[view_source]

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

Arguments:

  • data dict - Dictionary to merge into
  • 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:

  • Dict - YAML file merged into dictionary

load_json_into_existing_dict

def load_json_into_existing_dict(data: dict,
                                 path: str,
                                 encoding: str = "utf-8",
                                 loaderror_if_empty: bool = True) -> Dict

[view_source]

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

Arguments:

  • data dict - Dictionary to merge into
  • 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:

  • dict - JSON file merged into dictionary

hdx.utilities.saver

Saving utilities for YAML, JSON etc.

save_text

def save_text(string: str, path: str, encoding: str = "utf-8") -> None

[view_source]

Save text string to file.

Arguments:

  • string str - String to save
  • path str - Path to file
  • encoding str - Encoding of file. Defaults to utf-8.

Returns:

None

save_yaml

def save_yaml(object: Any,
              path: str,
              encoding: str = "utf-8",
              pretty: bool = False,
              sortkeys: bool = False) -> None

[view_source]

Save dictionary to YAML file preserving order if it is an OrderedDict.

Arguments:

  • object Any - Python object to save
  • path str - Path to YAML file
  • encoding str - Encoding of file. Defaults to utf-8.
  • pretty bool - Whether to pretty print. Defaults to False.
  • sortkeys bool - Whether to sort dictionary keys. Defaults to False.

Returns:

None

save_json

def save_json(object: Any,
              path: str,
              encoding: str = "utf-8",
              pretty: bool = False,
              sortkeys: bool = False) -> None

[view_source]

Save dictionary to JSON file preserving order if it is an OrderedDict.

Arguments:

  • object Any - Python object to save
  • path str - Path to JSON file
  • encoding str - Encoding of file. Defaults to utf-8.
  • pretty bool - Whether to pretty print. Defaults to False.
  • sortkeys bool - Whether to sort dictionary keys. Defaults to False.

Returns:

None

save_hxlated_output

def save_hxlated_output(configuration: Dict,
                        rows: ListTuple[ListTupleDict],
                        includes_header: bool = True,
                        includes_hxltags: bool = False,
                        output_dir: str = "",
                        **kwargs: Any) -> None

[view_source]

Save rows with header and HXL hashtags. Currently, JSON and/or csv outputs are supported. An output directory (which defaults to "") can be given. The utility expects a configuration containing definitions of input headers and HXLtags if these are not included in the rows, and output files with desired HXL hashtags. Keyword arguments are used to pass in any variables needed by the metadata defined in the configuration.

Arguments:

  • configuration Dict - Configuration for input and output
  • rows ListTuple[ListTupleDict] - Rows of data
  • includes_header bool - Whether rows includes header. Defaults to True,
  • includes_hxltags bool - Whether rows includes HXL hashtags. Defaults to False.
  • output_dir str - Output directory. Defaults to "".
  • **kwargs - Variables to use when evaluating template arguments

Returns:

None