sanitize_dict#
- cherab.lhd.tools.utility.sanitize_dict(attrs: dict[str, object]) dict[str, object]Source#
Convert dictionary to a form that can be saved in NetCDF or JSON.
This function converts all values in the input dictionary to basic Python types, such as str, int, float, bool, or list. This is useful when saving dictionaries to NetCDF or JSON files, as these formats do not support all Python types, such as NumPy types or custom objects.
- Parameters:
- attrs
dict Dictionary of attributes to sanitize.
- attrs
- Returns:
Examples
>>> x = { ... "str": "string", ... "int": 1, ... "float": 1.0, ... "bool": True, ... "np_int": np.int64(1), ... "np_float": np.float64(1.0), ... "np_bool": np.bool_(True), ... "list": [1, 2, 3], ... "dict": {"key": "value"}, ... "none": None, ... "object": object(), ... } >>> sanitize_dict(x) { "str": "string", "int": 1, "float": 1.0, "bool": "True", "np_int": 1, "np_float": 1.0, "np_bool": "True", "list": [1, 2, 3], "dict": '{"key": "value"}', "none": "None", "object": "<object object at 0x...>", }