twpasolver.file_utils module#

Functions for saving to json and hdf5 files.

This module provides functions to read from and write to JSON and HDF5 files. It includes utility functions to ensure file extensions and directories are correctly handled, as well as recursive functions to handle nested dictionaries when working with HDF5 files.

twpasolver.file_utils.read_file(savename: str, writer: str = 'json') Dict[str, Any][source]#

Read data from a file and return it as a dictionary.

Parameters:
  • savename (str) – The name of the file to read.

  • writer (str) – The file format to use (default is “json”).

Returns:

Dictionary containing the read data.

Return type:

Dict[str, Any]

twpasolver.file_utils.save_to_file(savename: str, d: Dict[str, Any], writer: str = 'json') None[source]#

Save data to a file in the specified format.

Parameters:
  • savename (str) – The name of the file to save.

  • d (Dict[str, Any]) – Dictionary containing data to be saved.

  • writer (str) – The file format to use (default is “json”).

twpasolver.file_utils.add_extension(filename: str, extension: str) str[source]#

Add the specified extension to the filename if not already present.

Parameters:
  • filename (str) – The name of the file.

  • extension (str) – The extension to add.

Returns:

Filename with the specified extension.

Return type:

str

twpasolver.file_utils.ensure_directory_exists(filename: str) None[source]#

Ensure that the directory for the given filename exists; create it if necessary.

Parameters:

filename (str) – The name of the file.

twpasolver.file_utils.read_hdf5(savename: str) Dict[str, Any][source]#

Read data from an HDF5 file and return it as a dictionary.

Parameters:

savename (str) – The name of the HDF5 file to read.

Returns:

Dictionary containing the read data.

Return type:

Dict[str, Any]

twpasolver.file_utils.read_json(savename: str) Dict[str, Any][source]#

Read data from a JSON file and return it as a dictionary.

Parameters:

savename (str) – The name of the JSON file to read.

Returns:

Dictionary containing the read data.

Return type:

Dict[str, Any]

twpasolver.file_utils.save_to_hdf5(savename: str, d: Dict[str, Any]) None[source]#

Save data to an HDF5 file.

Parameters:
  • savename (str) – The name of the HDF5 file to save.

  • d (Dict[str, Any]) – Dictionary containing data to be saved.

twpasolver.file_utils.save_to_json(savename: str, d: Dict[str, Any]) None[source]#

Save data to a JSON file.

Parameters:
  • savename (str) – The name of the JSON file to save.

  • d (Dict[str, Any]) – Dictionary containing data to be saved.

class twpasolver.file_utils.NpEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#

Bases: JSONEncoder

JSON encoder for handling NumPy types.

default(o: Any) int | float | List[Any] | str[source]#

Return default encoding for NumPy types.

Parameters:

o (Any) – The object to encode.

Returns:

The encoded object.

Return type:

int | float | List[Any] | str