mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-24 08:14:14 +03:00
Make str(TLObject) return valid code to generate it back
This commit is contained in:
parent
48c8837f19
commit
1f54cbfb5a
|
@ -1,3 +1,4 @@
|
||||||
|
from datetime import datetime
|
||||||
from threading import Event
|
from threading import Event
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,18 +20,14 @@ class TLObject:
|
||||||
"""
|
"""
|
||||||
if indent is None:
|
if indent is None:
|
||||||
if isinstance(obj, TLObject):
|
if isinstance(obj, TLObject):
|
||||||
children = obj.to_dict(recursive=False)
|
return '{}({})'.format(type(obj).__name__, ', '.join(
|
||||||
if children:
|
'{}={}'.format(k, TLObject.pretty_format(v))
|
||||||
return '{}: {}'.format(
|
for k, v in obj.to_dict(recursive=False).items()
|
||||||
type(obj).__name__, TLObject.pretty_format(children)
|
))
|
||||||
)
|
|
||||||
else:
|
|
||||||
return type(obj).__name__
|
|
||||||
if isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
return '{{{}}}'.format(', '.join(
|
return '{{{}}}'.format(', '.join(
|
||||||
'{}: {}'.format(
|
'{}: {}'.format(k, TLObject.pretty_format(v))
|
||||||
k, TLObject.pretty_format(v)
|
for k, v in obj.items()
|
||||||
) for k, v in obj.items()
|
|
||||||
))
|
))
|
||||||
elif isinstance(obj, str) or isinstance(obj, bytes):
|
elif isinstance(obj, str) or isinstance(obj, bytes):
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
|
@ -38,31 +35,36 @@ class TLObject:
|
||||||
return '[{}]'.format(
|
return '[{}]'.format(
|
||||||
', '.join(TLObject.pretty_format(x) for x in obj)
|
', '.join(TLObject.pretty_format(x) for x in obj)
|
||||||
)
|
)
|
||||||
|
elif isinstance(obj, datetime):
|
||||||
|
return 'datetime.fromtimestamp({})'.format(obj.timestamp())
|
||||||
else:
|
else:
|
||||||
return str(obj)
|
return repr(obj)
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
if isinstance(obj, TLObject):
|
if isinstance(obj, TLObject) or isinstance(obj, dict):
|
||||||
result.append(type(obj).__name__)
|
if isinstance(obj, dict):
|
||||||
children = obj.to_dict(recursive=False)
|
d = obj
|
||||||
if children:
|
start, end, sep = '{', '}', ': '
|
||||||
result.append(': ')
|
else:
|
||||||
result.append(TLObject.pretty_format(
|
d = obj.to_dict(recursive=False)
|
||||||
obj.to_dict(recursive=False), indent
|
start, end, sep = '(', ')', '='
|
||||||
))
|
result.append(type(obj).__name__)
|
||||||
|
|
||||||
elif isinstance(obj, dict):
|
result.append(start)
|
||||||
result.append('{\n')
|
if d:
|
||||||
indent += 1
|
result.append('\n')
|
||||||
for k, v in obj.items():
|
indent += 1
|
||||||
|
for k, v in d.items():
|
||||||
|
result.append('\t' * indent)
|
||||||
|
result.append(k)
|
||||||
|
result.append(sep)
|
||||||
|
result.append(TLObject.pretty_format(v, indent))
|
||||||
|
result.append(',\n')
|
||||||
|
result.pop() # last ',\n'
|
||||||
|
indent -= 1
|
||||||
|
result.append('\n')
|
||||||
result.append('\t' * indent)
|
result.append('\t' * indent)
|
||||||
result.append(k)
|
result.append(end)
|
||||||
result.append(': ')
|
|
||||||
result.append(TLObject.pretty_format(v, indent))
|
|
||||||
result.append(',\n')
|
|
||||||
indent -= 1
|
|
||||||
result.append('\t' * indent)
|
|
||||||
result.append('}')
|
|
||||||
|
|
||||||
elif isinstance(obj, str) or isinstance(obj, bytes):
|
elif isinstance(obj, str) or isinstance(obj, bytes):
|
||||||
result.append(repr(obj))
|
result.append(repr(obj))
|
||||||
|
@ -78,8 +80,13 @@ class TLObject:
|
||||||
result.append('\t' * indent)
|
result.append('\t' * indent)
|
||||||
result.append(']')
|
result.append(']')
|
||||||
|
|
||||||
|
elif isinstance(obj, datetime):
|
||||||
|
result.append('datetime.fromtimestamp(')
|
||||||
|
result.append(repr(obj.timestamp()))
|
||||||
|
result.append(')')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
result.append(str(obj))
|
result.append(repr(obj))
|
||||||
|
|
||||||
return ''.join(result)
|
return ''.join(result)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user