From 1a95189c5a6fb900eac9f824ecaa373eebe26685 Mon Sep 17 00:00:00 2001 From: 2ei Date: Sun, 21 Jul 2024 16:47:32 +0300 Subject: [PATCH 1/5] Update types.py --- telebot/types.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index 6913452be..ce074881e 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -98,11 +98,21 @@ def check_json(json_type, dict_copy = True): raise ValueError("json_type should be a json dict or string.") def __str__(self): - d = { - x: y.__dict__ if hasattr(y, '__dict__') else y - for x, y in self.__dict__.items() - } - return str(d) + return json.dumps( + self, + default=lambda obj: ( + repr(obj) if not isinstance(obj, JsonDeserializable) + else { + attr: getattr(obj, attr) + for attr in filter( + lambda x: not x.startswith("_") and getattr(obj, x) is not None, + obj.__dict__ + ) + } + ), + indent=2, + ensure_ascii=False + ) class Update(JsonDeserializable): @@ -10726,4 +10736,4 @@ def de_json(cls, json_string): obj = cls.check_json(json_string) return cls(**obj) - \ No newline at end of file + From ee725a5bd1b3a427d63a210995fb0f3cce574722 Mon Sep 17 00:00:00 2001 From: Zaid Date: Sun, 21 Jul 2024 21:08:46 +0300 Subject: [PATCH 2/5] update --- telebot/types.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index ce074881e..067113dfd 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -19,6 +19,8 @@ DISABLE_KEYLEN_ERROR = False +JSONDESERIALIZABLE_PARSE_OUTPUT = False +JSONDESERIALIZABLE_SKIP_NONE = True logger = logging.getLogger('TeleBot') @@ -97,21 +99,34 @@ def check_json(json_type, dict_copy = True): else: raise ValueError("json_type should be a json dict or string.") - def __str__(self): - return json.dumps( - self, - default=lambda obj: ( - repr(obj) if not isinstance(obj, JsonDeserializable) - else { - attr: getattr(obj, attr) - for attr in filter( - lambda x: not x.startswith("_") and getattr(obj, x) is not None, - obj.__dict__ - ) + def __str__(self) -> str: + default = ( + lambda obj: (repr(obj) if JSONDESERIALIZABLE_PARSE_OUTPUT else obj) + if not isinstance(obj, JsonDeserializable) + else { + attr: getattr(obj, attr) + for attr in filter( + (lambda x: not x.startswith("_") and getattr(obj, x) is not None) + if JSONDESERIALIZABLE_SKIP_NONE + else (lambda x: not x.startswith("_")), + obj.__dict__, + ) + } + ) + return ( + json.dumps( + self, default=default, indent=2, ensure_ascii=False + ) + if JSONDESERIALIZABLE_PARSE_OUTPUT + else str( + { + x: default(y) + if isinstance(y, JsonDeserializable) + else y + for x, y in self.__dict__.items() + if y is not None or not JSONDESERIALIZABLE_SKIP_NONE } - ), - indent=2, - ensure_ascii=False + ) ) From 009d22b595671fe78d395e850f9b31067bf17099 Mon Sep 17 00:00:00 2001 From: Zaid Date: Sun, 21 Jul 2024 21:11:53 +0300 Subject: [PATCH 3/5] short --- telebot/types.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index 067113dfd..6e02a0a59 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -106,23 +106,20 @@ def __str__(self) -> str: else { attr: getattr(obj, attr) for attr in filter( - (lambda x: not x.startswith("_") and getattr(obj, x) is not None) - if JSONDESERIALIZABLE_SKIP_NONE - else (lambda x: not x.startswith("_")), + lambda x: not x.startswith("_") + and ( + getattr(obj, x) is not None or not JSONDESERIALIZABLE_SKIP_NONE + ), obj.__dict__, ) } ) return ( - json.dumps( - self, default=default, indent=2, ensure_ascii=False - ) + json.dumps(self, default=default, indent=2, ensure_ascii=False) if JSONDESERIALIZABLE_PARSE_OUTPUT else str( { - x: default(y) - if isinstance(y, JsonDeserializable) - else y + x: default(y) if isinstance(y, JsonDeserializable) else y for x, y in self.__dict__.items() if y is not None or not JSONDESERIALIZABLE_SKIP_NONE } From 866ccb8d40545c4aac3e5445239f66e62229970e Mon Sep 17 00:00:00 2001 From: 2ei Date: Thu, 25 Jul 2024 16:12:36 +0300 Subject: [PATCH 4/5] INDENT --- telebot/types.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telebot/types.py b/telebot/types.py index 6e02a0a59..769faf3b8 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -19,6 +19,7 @@ DISABLE_KEYLEN_ERROR = False +JSONDESERIALIZABLE_INDENT = 2 JSONDESERIALIZABLE_PARSE_OUTPUT = False JSONDESERIALIZABLE_SKIP_NONE = True @@ -115,7 +116,7 @@ def __str__(self) -> str: } ) return ( - json.dumps(self, default=default, indent=2, ensure_ascii=False) + json.dumps(self, default=default, indent=JSONDESERIALIZABLE_INDENT, ensure_ascii=False) if JSONDESERIALIZABLE_PARSE_OUTPUT else str( { From 7cfc9549002daa4c8eb8e541d8cc6db5ccbf99b1 Mon Sep 17 00:00:00 2001 From: Zaid Date: Sun, 28 Jul 2024 18:30:55 +0300 Subject: [PATCH 5/5] make it more clear --- telebot/types.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index 769faf3b8..96d970d65 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -100,32 +100,34 @@ def check_json(json_type, dict_copy = True): else: raise ValueError("json_type should be a json dict or string.") - def __str__(self) -> str: - default = ( - lambda obj: (repr(obj) if JSONDESERIALIZABLE_PARSE_OUTPUT else obj) - if not isinstance(obj, JsonDeserializable) - else { + @staticmethod + def _default(obj) -> Union[dict, str]: + if not isinstance(obj, JsonDeserializable): + return repr(obj) if JSONDESERIALIZABLE_PARSE_OUTPUT else obj + else: + return { attr: getattr(obj, attr) for attr in filter( - lambda x: not x.startswith("_") - and ( - getattr(obj, x) is not None or not JSONDESERIALIZABLE_SKIP_NONE - ), + lambda x: not x.startswith("_"), obj.__dict__, ) + if getattr(obj, attr) is not None or not JSONDESERIALIZABLE_SKIP_NONE } - ) - return ( - json.dumps(self, default=default, indent=JSONDESERIALIZABLE_INDENT, ensure_ascii=False) - if JSONDESERIALIZABLE_PARSE_OUTPUT - else str( + + def __str__(self) -> str: + if JSONDESERIALIZABLE_PARSE_OUTPUT: + return json.dumps( + self, + default=JsonDeserializable._default, + indent=JSONDESERIALIZABLE_INDENT, ensure_ascii=False) + else: + return str( { - x: default(y) if isinstance(y, JsonDeserializable) else y + x: JsonDeserializable._default(y) if isinstance(y, JsonDeserializable) else y for x, y in self.__dict__.items() if y is not None or not JSONDESERIALIZABLE_SKIP_NONE } ) - ) class Update(JsonDeserializable):