Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make json viewer more clear (reopen) #2350

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions telebot/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@


DISABLE_KEYLEN_ERROR = False
JSONDESERIALIZABLE_INDENT = 2
JSONDESERIALIZABLE_PARSE_OUTPUT = False
JSONDESERIALIZABLE_SKIP_NONE = True

logger = logging.getLogger('TeleBot')

Expand Down Expand Up @@ -97,12 +100,32 @@ def check_json(json_type, dict_copy = True):
else:
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)
def __str__(self) -> str:
default = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frankly speaking, I do not understand anything in this pigeon talk. It takes too much time to decode.

Can this code be rewriten in adequate if / then / else mode? So ANYBODY who see it can understand what will be returned in what case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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 or not JSONDESERIALIZABLE_SKIP_NONE
),
obj.__dict__,
)
}
)
return (
json.dumps(self, default=default, indent=JSONDESERIALIZABLE_INDENT, 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
}
)
)


class Update(JsonDeserializable):
Expand Down Expand Up @@ -10726,4 +10749,4 @@ def de_json(cls, json_string):
obj = cls.check_json(json_string)
return cls(**obj)



Loading