Telethon/readthedocs/concepts/strings.rst

89 lines
2.7 KiB
ReStructuredText
Raw Normal View History

2019-05-09 13:24:37 +03:00
======================
String-based Debugging
======================
Debugging is *really* important. Telegram's API is really big and there
2020-09-10 15:51:31 +03:00
are a lot of things that you should know. Such as, what attributes or fields
2019-05-09 13:24:37 +03:00
does a result have? Well, the easiest thing to do is printing it:
.. code-block:: python
entity = await client.get_entity('username')
print(entity)
2019-05-09 13:24:37 +03:00
That will show a huge **string** similar to the following:
.. code-block:: python
Channel(id=1066197625, title='Telegram Usernames', photo=ChatPhotoEmpty(), date=datetime.datetime(2016, 12, 16, 15, 15, 43, tzinfo=datetime.timezone.utc), version=0, creator=False, left=True, broadcast=True, verified=True, megagroup=False, restricted=False, signatures=False, min=False, scam=False, has_link=False, has_geo=False, slowmode_enabled=False, access_hash=-6309373984955162244, username='username', restriction_reason=[], admin_rights=None, banned_rights=None, default_banned_rights=None, participants_count=None)
2019-05-09 13:24:37 +03:00
That's a lot of text. But as you can see, all the properties are there.
So if you want the title you **don't use regex** or anything like
splitting ``str(entity)`` to get what you want. You just access the
2019-05-09 13:24:37 +03:00
attribute you need:
.. code-block:: python
title = entity.title
2019-05-09 13:24:37 +03:00
Can we get better than the shown string, though? Yes!
.. code-block:: python
print(entity.stringify())
2019-05-09 13:24:37 +03:00
2020-09-10 15:51:31 +03:00
Will show a much better representation:
2019-05-09 13:24:37 +03:00
.. code-block:: python
Channel(
id=1066197625,
title='Telegram Usernames',
photo=ChatPhotoEmpty(
),
date=datetime.datetime(2016, 12, 16, 15, 15, 43, tzinfo=datetime.timezone.utc),
version=0,
creator=False,
left=True,
broadcast=True,
verified=True,
megagroup=False,
2019-05-09 13:24:37 +03:00
restricted=False,
signatures=False,
2019-05-09 13:24:37 +03:00
min=False,
scam=False,
has_link=False,
has_geo=False,
slowmode_enabled=False,
access_hash=-6309373984955162244,
username='username',
restriction_reason=[
],
admin_rights=None,
banned_rights=None,
default_banned_rights=None,
participants_count=None
2019-05-09 13:24:37 +03:00
)
2019-05-09 13:24:37 +03:00
Now it's easy to see how we could get, for example,
the ``year`` value. It's inside ``date``:
2019-05-09 13:24:37 +03:00
.. code-block:: python
channel_year = entity.date.year
2019-05-09 13:24:37 +03:00
You don't need to print everything to see what all the possible values
2019-05-22 12:29:46 +03:00
can be. You can just search in http://tl.telethon.dev/.
2019-05-09 13:24:37 +03:00
Remember that you can use Python's `isinstance
<https://docs.python.org/3/library/functions.html#isinstance>`_
to check the type of something. For example:
.. code-block:: python
from telethon import types
if isinstance(entity.photo, types.ChatPhotoEmpty):
print('Channel has no photo')