Compare commits

...

3 Commits

Author SHA1 Message Date
udf 35b6e7d1e4
Return immediately in some cases instead of using the result list
I like it better now, but I still dislike it; We need to rewrite the entire function
2018-11-21 22:49:17 +02:00
udf 778a604d69
Prepend list items with "- " 2018-11-21 22:45:01 +02:00
udf e760062218
Minor code cleanup 2018-11-21 22:34:11 +02:00
1 changed files with 30 additions and 31 deletions

View File

@ -10,7 +10,7 @@ from telethon.tl.types import MessageEntityPre
from telethon.tl.tlobject import TLObject
import datetime
PRINTABLE_SET = set(bytes(string.printable, 'ascii'))
PRINTABLE_SET = set(string.printable.encode())
STR_LEN_MAX = 256
BYTE_LEN_MAX = 64
@ -19,7 +19,7 @@ def parse_pre(text):
text = text.strip()
return (
text,
[MessageEntityPre(offset=0, length=len(add_surrogate(text)), language='potato')]
[MessageEntityPre(offset=0, length=len(add_surrogate(text)), language='')]
)
@ -33,8 +33,9 @@ def yaml_format(obj, indent=0):
obj = obj.to_dict()
if isinstance(obj, dict):
if not obj:
return 'dict:'
result.append(obj.get('_', 'dict') + ':')
if obj:
items = obj.items()
has_multiple_items = len(items) > 2
if has_multiple_items:
@ -54,34 +55,32 @@ def yaml_format(obj, indent=0):
result.append(' ' * indent)
elif isinstance(obj, str):
# truncate long strings and display elipsis
result.append(repr(obj[:STR_LEN_MAX]))
result = repr(obj[:STR_LEN_MAX])
if len(obj) > STR_LEN_MAX:
result.append('')
result += ''
return result
elif isinstance(obj, bytes):
# repr() bytes if it's printable, hex like "FF EE BB" otherwise
if all(c in PRINTABLE_SET for c in obj):
result.append(repr(obj))
return repr(obj)
else:
if len(obj) > BYTE_LEN_MAX:
result.append('<…>')
else:
result.append(' '.join(f'{b:02X}' for b in obj))
return ('<…>' if len(obj) > BYTE_LEN_MAX else
' '.join(f'{b:02X}' for b in obj))
elif isinstance(obj, datetime.datetime):
# ISO-8601 without timezone offset (telethon dates are always UTC)
result.append(obj.strftime('%Y-%m-%d %H:%M:%S'))
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif hasattr(obj, '__iter__'):
# display iterables one after another at the base indentation level
result.append('\n')
indent += 2
for x in obj:
result.append(' ' * indent)
result.append(yaml_format(x, indent))
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
result.append('\n')
result.pop()
indent -= 2
result.append(' ' * indent)
else:
result.append(repr(obj))
return repr(obj)
return ''.join(result)