Source code for matador.utils.print_utils

# coding: utf-8
# Distributed under the terms of the MIT License.

""" This file implements some useful wrappers to the print function for
writing errors and warnings to stderr.

"""


import sys
import json
import numpy as np














[docs]def dumps(obj, **kwargs): """Mirrors `json.dumps` whilst handling numpy arrays.""" return json.dumps(obj, cls=NumpyEncoder, **kwargs)
[docs]class NumpyEncoder(json.JSONEncoder): """This encoder handles NumPy arrays in JSON, and was taken from StackOverflow (where else) (https://stackoverflow.com/a/47626762). """
[docs] def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj)