From 5014fbd9e238f19c68c9ce6f94f29a048d79a70e Mon Sep 17 00:00:00 2001 From: StanChan03 Date: Sat, 18 Jan 2025 16:48:47 -0800 Subject: [PATCH 1/2] custom object serialization --- lotus/cache.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lotus/cache.py b/lotus/cache.py index b5b85c7e..cd306876 100644 --- a/lotus/cache.py +++ b/lotus/cache.py @@ -53,7 +53,10 @@ def serialize(value: Any) -> Any: elif hasattr(value, "dict"): return value.dict() elif hasattr(value, "__dict__"): - return {key: serialize(val) for key, val in vars(value).items() if not key.startswith("_")} + return { + "class_name": type(value).__name__, + "attributes": {k: serialize(v) for k, v in vars(value).items()}, + } else: # For unsupported types, convert to string (last resort) lotus.logger.warning(f"Unsupported type {type(value)} for serialization. Converting to string.") From a9a0702a5e311f393e4df2d33e0dc2af2c1b54ff Mon Sep 17 00:00:00 2001 From: StanChan03 Date: Sat, 18 Jan 2025 20:14:36 -0800 Subject: [PATCH 2/2] add right object --- lotus/cache.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lotus/cache.py b/lotus/cache.py index cd306876..609c6f0f 100644 --- a/lotus/cache.py +++ b/lotus/cache.py @@ -53,16 +53,13 @@ def serialize(value: Any) -> Any: elif hasattr(value, "dict"): return value.dict() elif hasattr(value, "__dict__"): - return { - "class_name": type(value).__name__, - "attributes": {k: serialize(v) for k, v in vars(value).items()}, - } + return {key: serialize(val) for key, val in vars(value).items() if not key.startswith("_")} else: # For unsupported types, convert to string (last resort) lotus.logger.warning(f"Unsupported type {type(value)} for serialization. Converting to string.") return str(value) - serialize_self = serialize(self) + serialize_self = serialize(self._obj) serialized_kwargs = {key: serialize(value) for key, value in kwargs.items()} serialized_args = [serialize(arg) for arg in args] cache_key = hashlib.sha256(