Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -3,7 +3,7 @@ import json
|
|
| 3 |
from typing import Any, Dict
|
| 4 |
|
| 5 |
def to_jsonable(obj: Any) -> Any:
|
| 6 |
-
"""Safely convert pandas/numpy objects to JSON-able python types.
|
| 7 |
try:
|
| 8 |
import numpy as np
|
| 9 |
import pandas as pd
|
|
@@ -19,15 +19,14 @@ def to_jsonable(obj: Any) -> Any:
|
|
| 19 |
return int(obj)
|
| 20 |
if np is not None and isinstance(obj, (np.floating,)):
|
| 21 |
f = float(obj)
|
| 22 |
-
#
|
| 23 |
-
if f !=f:
|
| 24 |
return None
|
| 25 |
return f
|
| 26 |
if isinstance(obj, (set,)):
|
| 27 |
return list(obj)
|
| 28 |
-
if isinstance(obj,(bytes, bytearray)):
|
| 29 |
return obj.decode("utf-8", errors="ignore")
|
| 30 |
return obj
|
| 31 |
|
| 32 |
def dumps(d: Dict[str, Any]) -> str:
|
| 33 |
-
return json.dumps(d, ensure_ascii=False, default=to_jsonable)
|
|
|
|
| 3 |
from typing import Any, Dict
|
| 4 |
|
| 5 |
def to_jsonable(obj: Any) -> Any:
|
| 6 |
+
"""Safely convert pandas/numpy objects to JSON-able python types."""
|
| 7 |
try:
|
| 8 |
import numpy as np
|
| 9 |
import pandas as pd
|
|
|
|
| 19 |
return int(obj)
|
| 20 |
if np is not None and isinstance(obj, (np.floating,)):
|
| 21 |
f = float(obj)
|
| 22 |
+
if f != f: # NaN
|
|
|
|
| 23 |
return None
|
| 24 |
return f
|
| 25 |
if isinstance(obj, (set,)):
|
| 26 |
return list(obj)
|
| 27 |
+
if isinstance(obj, (bytes, bytearray)):
|
| 28 |
return obj.decode("utf-8", errors="ignore")
|
| 29 |
return obj
|
| 30 |
|
| 31 |
def dumps(d: Dict[str, Any]) -> str:
|
| 32 |
+
return json.dumps(d, ensure_ascii=False, default=to_jsonable)
|