From 4b10ec8e87956bf220eaf0dc180adf1b14d51392 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 20 Jun 2022 11:43:40 +0200 Subject: [PATCH] Pickle datetime objects into timestamps (#1016) * Pickle datetime objects into timestamps * Rename variable * Add length check --- pyrogram/types/object.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/pyrogram/types/object.py b/pyrogram/types/object.py index 28bee9a7..3253c8ec 100644 --- a/pyrogram/types/object.py +++ b/pyrogram/types/object.py @@ -98,7 +98,24 @@ class Object: return True + def __setstate__(self, state): + for attr in state: + obj = state[attr] + + # Maybe a better alternative would be https://docs.python.org/3/library/inspect.html#inspect.signature + if isinstance(obj, tuple) and len(obj) == 2 and obj[0] == "dt": + state[attr] = datetime.fromtimestamp(obj[1]) + + self.__dict__ = state + def __getstate__(self): - new_dict = self.__dict__.copy() - new_dict.pop("_client", None) - return new_dict + state = self.__dict__.copy() + state.pop("_client", None) + + for attr in state: + obj = state[attr] + + if isinstance(obj, datetime): + state[attr] = ("dt", obj.timestamp()) + + return state