forked from kate/uniborg
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
This commit is contained in:
parent
778a604d69
commit
35b6e7d1e4
|
@ -33,42 +33,42 @@ def yaml_format(obj, indent=0):
|
||||||
obj = obj.to_dict()
|
obj = obj.to_dict()
|
||||||
|
|
||||||
if isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
|
if not obj:
|
||||||
|
return 'dict:'
|
||||||
result.append(obj.get('_', 'dict') + ':')
|
result.append(obj.get('_', 'dict') + ':')
|
||||||
if obj:
|
items = obj.items()
|
||||||
items = obj.items()
|
has_multiple_items = len(items) > 2
|
||||||
has_multiple_items = len(items) > 2
|
if has_multiple_items:
|
||||||
if has_multiple_items:
|
result.append('\n')
|
||||||
result.append('\n')
|
indent += 2
|
||||||
indent += 2
|
for k, v in items:
|
||||||
for k, v in items:
|
if k == '_' or v is None:
|
||||||
if k == '_' or v is None:
|
continue
|
||||||
continue
|
formatted = yaml_format(v, indent)
|
||||||
formatted = yaml_format(v, indent)
|
if not formatted.strip():
|
||||||
if not formatted.strip():
|
continue
|
||||||
continue
|
result.append(' ' * (indent if has_multiple_items else 1))
|
||||||
result.append(' ' * (indent if has_multiple_items else 1))
|
result.append(f'{k}: {formatted}')
|
||||||
result.append(f'{k}: {formatted}')
|
result.append('\n')
|
||||||
result.append('\n')
|
result.pop()
|
||||||
result.pop()
|
indent -= 2
|
||||||
indent -= 2
|
result.append(' ' * indent)
|
||||||
result.append(' ' * indent)
|
|
||||||
elif isinstance(obj, str):
|
elif isinstance(obj, str):
|
||||||
# truncate long strings and display elipsis
|
# 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:
|
if len(obj) > STR_LEN_MAX:
|
||||||
result.append('…')
|
result += '…'
|
||||||
|
return result
|
||||||
elif isinstance(obj, bytes):
|
elif isinstance(obj, bytes):
|
||||||
# repr() bytes if it's printable, hex like "FF EE BB" otherwise
|
# repr() bytes if it's printable, hex like "FF EE BB" otherwise
|
||||||
if all(c in PRINTABLE_SET for c in obj):
|
if all(c in PRINTABLE_SET for c in obj):
|
||||||
result.append(repr(obj))
|
return repr(obj)
|
||||||
else:
|
else:
|
||||||
if len(obj) > BYTE_LEN_MAX:
|
return ('<…>' if len(obj) > BYTE_LEN_MAX else
|
||||||
result.append('<…>')
|
' '.join(f'{b:02X}' for b in obj))
|
||||||
else:
|
|
||||||
result.append(' '.join(f'{b:02X}' for b in obj))
|
|
||||||
elif isinstance(obj, datetime.datetime):
|
elif isinstance(obj, datetime.datetime):
|
||||||
# ISO-8601 without timezone offset (telethon dates are always UTC)
|
# 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__'):
|
elif hasattr(obj, '__iter__'):
|
||||||
# display iterables one after another at the base indentation level
|
# display iterables one after another at the base indentation level
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
|
@ -80,7 +80,7 @@ def yaml_format(obj, indent=0):
|
||||||
indent -= 2
|
indent -= 2
|
||||||
result.append(' ' * indent)
|
result.append(' ' * indent)
|
||||||
else:
|
else:
|
||||||
result.append(repr(obj))
|
return repr(obj)
|
||||||
|
|
||||||
return ''.join(result)
|
return ''.join(result)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue