(self, obj: Any)
| 51 | |
| 52 | class SafeJSONEncoder(json.JSONEncoder): |
| 53 | def default(self, obj: Any): |
| 54 | try: |
| 55 | import numpy as np |
| 56 | |
| 57 | numpy_imported = True |
| 58 | except ImportError: |
| 59 | numpy_imported = False |
| 60 | |
| 61 | try: |
| 62 | import pandas as pd |
| 63 | |
| 64 | pandas_imported = True |
| 65 | except ImportError: |
| 66 | pandas_imported = False |
| 67 | |
| 68 | if numpy_imported and isinstance(obj, np.ndarray): |
| 69 | return obj.tolist() |
| 70 | elif pandas_imported and isinstance(obj, pd.DataFrame): |
| 71 | return obj.to_dict(orient="records") |
| 72 | elif pandas_imported and isinstance(obj, pd.Series): |
| 73 | return obj.tolist() |
| 74 | |
| 75 | try: |
| 76 | return super().default(obj) |
| 77 | except TypeError: |
| 78 | return repr(obj) |
| 79 | |
| 80 | |
| 81 | def _should_chown(agent_home: pathlib.Path, path: pathlib.Path): |
no test coverage detected