pyrestoolbox-mcp 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyrestoolbox_mcp/__init__.py +3 -0
- pyrestoolbox_mcp/serialize.py +50 -0
- pyrestoolbox_mcp/server.py +681 -0
- pyrestoolbox_mcp-0.1.0.dist-info/METADATA +123 -0
- pyrestoolbox_mcp-0.1.0.dist-info/RECORD +9 -0
- pyrestoolbox_mcp-0.1.0.dist-info/WHEEL +5 -0
- pyrestoolbox_mcp-0.1.0.dist-info/entry_points.txt +2 -0
- pyrestoolbox_mcp-0.1.0.dist-info/licenses/LICENSE +674 -0
- pyrestoolbox_mcp-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Convert pyrestoolbox return values to JSON-serializable structures."""
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
import math
|
|
5
|
+
from enum import Enum
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def to_jsonable(obj):
|
|
9
|
+
"""Recursively convert obj to JSON-serializable Python primitives.
|
|
10
|
+
|
|
11
|
+
numpy scalars/arrays, pandas DataFrames/Series, dataclasses, Enums and
|
|
12
|
+
nested containers are handled. Non-finite floats become None (JSON has
|
|
13
|
+
no NaN/inf).
|
|
14
|
+
"""
|
|
15
|
+
if obj is None or isinstance(obj, (bool, str)):
|
|
16
|
+
return obj
|
|
17
|
+
if isinstance(obj, int):
|
|
18
|
+
return int(obj)
|
|
19
|
+
if isinstance(obj, float): # np.float64 subclasses float - cast to plain float
|
|
20
|
+
return float(obj) if math.isfinite(obj) else None
|
|
21
|
+
if isinstance(obj, Enum):
|
|
22
|
+
return obj.name
|
|
23
|
+
if dataclasses.is_dataclass(obj) and not isinstance(obj, type):
|
|
24
|
+
return {f.name: to_jsonable(getattr(obj, f.name)) for f in dataclasses.fields(obj)}
|
|
25
|
+
# numpy
|
|
26
|
+
try:
|
|
27
|
+
import numpy as np
|
|
28
|
+
if isinstance(obj, np.generic):
|
|
29
|
+
return to_jsonable(obj.item())
|
|
30
|
+
if isinstance(obj, np.ndarray):
|
|
31
|
+
return [to_jsonable(v) for v in obj.tolist()]
|
|
32
|
+
except ImportError:
|
|
33
|
+
pass
|
|
34
|
+
# pandas
|
|
35
|
+
try:
|
|
36
|
+
import pandas as pd
|
|
37
|
+
if isinstance(obj, pd.DataFrame):
|
|
38
|
+
return {
|
|
39
|
+
"columns": [str(c) for c in obj.columns],
|
|
40
|
+
"records": to_jsonable(obj.to_dict(orient="records")),
|
|
41
|
+
}
|
|
42
|
+
if isinstance(obj, pd.Series):
|
|
43
|
+
return to_jsonable(obj.tolist())
|
|
44
|
+
except ImportError:
|
|
45
|
+
pass
|
|
46
|
+
if isinstance(obj, dict):
|
|
47
|
+
return {str(k): to_jsonable(v) for k, v in obj.items()}
|
|
48
|
+
if isinstance(obj, (list, tuple, set)):
|
|
49
|
+
return [to_jsonable(v) for v in obj]
|
|
50
|
+
return str(obj)
|