Skip to content

hdx.utilities.saver

module hdx.utilities.saver

Saving utilities for YAML, JSON etc.

Classes

Functions

  • save_text — Save text string to file.

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

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

  • save_iterable — Save an iterable of rows in dict or list form to a csv. The headers argument is either a row number (rows start counting at 1), or the actual headers defined as a list of strings, the order of which defines the column order. Columns not named in that list of strings will be dropped. If headers is not set, all rows will be treated as containing values. The columns argument defines which columns will be output. It can be used in conjunction with an integer headers with columns selecting the columns and hedders selecting the starting row.

class UnPrettyRTRepresenter(default_style: Any = None, default_flow_style: Any = None, dumper: Any = None)

Bases : RoundTripRepresenter

Methods

method UnPrettyRTRepresenter.represent_none(data: Any) → Any

class UnPrettySafeRepresenter(default_style: Any = None, default_flow_style: Any = None, dumper: Any = None)

Bases : SafeRepresenter

Methods

method UnPrettySafeRepresenter.represent_none(data: Any) → Any

class PrettySafeRepresenter(default_style: Any = None, default_flow_style: Any = None, dumper: Any = None)

Bases : SafeRepresenter

Methods

method PrettySafeRepresenter.represent_none(data: Any) → Any

save_text(string: str, path: Path | str, encoding: str = 'utf-8') → None

Save text string to file.

Parameters

  • string : str — String to save

  • path : Path | str — Path to file

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

Returns

  • None — None

save_yaml(object: Any, path: Path | str, encoding: str = 'utf-8', pretty: bool = False, sortkeys: bool = False) → None

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

Parameters

  • object : Any — Python object to save

  • path : 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 — None

save_json(object: Any, path: Path | str, encoding: str = 'utf-8', pretty: bool = False, sortkeys: bool = False) → None

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

Parameters

  • object : Any — Python object to save

  • path : 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 — None

save_iterable(filepath: Path | str, rows: Iterable[Sequence | Mapping], headers: int | Sequence[str] | None = None, columns: Sequence[int] | Sequence[str] | None = None, format: str = 'csv', encoding: str | None = None, row_function: Callable[[dict], dict | None] | None = None, no_empty: bool = True) → list

Save an iterable of rows in dict or list form to a csv. The headers argument is either a row number (rows start counting at 1), or the actual headers defined as a list of strings, the order of which defines the column order. Columns not named in that list of strings will be dropped. If headers is not set, all rows will be treated as containing values. The columns argument defines which columns will be output. It can be used in conjunction with an integer headers with columns selecting the columns and hedders selecting the starting row.

Parameters

  • filepath : Path | str — Path to write to

  • rows : Iterable[Sequence | Mapping] — List of rows in dict or list form

  • headers : int | Sequence[str] | None — Headers to write. Defaults to None.

  • columns : Sequence[int] | Sequence[str] | None — Columns to write. Defaults to all.

  • format : str — Format to write. Defaults to csv.

  • encoding : str | None — Encoding to use. Defaults to None (infer encoding).

  • row_function : Callable[[dict], dict | None] | None — Row function to call for each row. Defaults to None.

  • no_empty : bool — Don't save file if there are no data rows. Defaults to True.

Returns

  • list — List of rows written to file