MCPcopy Create free account
hub / github.com/PaddlePaddle/FastDeploy / simple_convert

Function simple_convert

fastdeploy/usage/usage_lib.py:213–246  ·  view source on GitHub ↗
(obj)

Source from the content-addressed store, hash-verified

211
212
213def simple_convert(obj):
214 if obj is None:
215 return None
216 elif isinstance(obj, (str, int, float, bool)):
217 return obj
218 elif isinstance(obj, dict):
219 return {k: simple_convert(v) for k, v in obj.items()}
220 elif isinstance(obj, (list, tuple, set)):
221 return [simple_convert(item) for item in obj]
222
223 if isinstance(obj, str):
224 try:
225 return json.loads(obj)
226 except:
227 return obj
228
229 if hasattr(obj, "__dict__"):
230 for method in ["to_dict", "to_json", "__getstate__", "as_dict"]:
231 if hasattr(obj, method):
232 result = getattr(obj, method)()
233 if isinstance(result, dict):
234 return simple_convert(result)
235 elif isinstance(result, str):
236 try:
237 return json.loads(result)
238 except:
239 return result
240
241 try:
242 return {k: simple_convert(v) for k, v in vars(obj).items() if not k.startswith("_")}
243 except Exception:
244 return str(obj)
245
246 return str(obj)
247
248
249class UsageMessage:

Callers 2

test_simple_convertMethod · 0.90
_report_usage_onceMethod · 0.85

Calls 1

itemsMethod · 0.80

Tested by 1

test_simple_convertMethod · 0.72