2019-06-04 14:10:32 +00:00
|
|
|
Debugging
|
|
|
|
=========
|
|
|
|
|
|
|
|
When working with the API, chances are you'll stumble upon bugs, get stuck and start wondering how to continue. Nothing
|
2022-01-07 09:18:51 +00:00
|
|
|
to actually worry about since Pyrogram provides some commodities to help you in this.
|
2019-06-04 14:10:32 +00:00
|
|
|
|
2020-04-01 18:08:46 +00:00
|
|
|
.. contents:: Contents
|
|
|
|
:backlinks: none
|
2020-08-22 06:05:05 +00:00
|
|
|
:depth: 1
|
2020-04-01 18:08:46 +00:00
|
|
|
:local:
|
|
|
|
|
|
|
|
-----
|
|
|
|
|
2019-06-04 14:10:32 +00:00
|
|
|
Caveman Debugging
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
*The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.*
|
|
|
|
|
|
|
|
-- Brian Kernighan, "Unix for Beginners" (1979)
|
|
|
|
|
|
|
|
Adding ``print()`` statements in crucial parts of your code is by far the most ancient, yet efficient technique for
|
|
|
|
debugging programs, especially considering the concurrent nature of the framework itself. Pyrogram goodness in this
|
|
|
|
respect comes with the fact that any object can be nicely printed just by calling ``print(obj)``, thus giving to you
|
|
|
|
an insight of all its inner details.
|
|
|
|
|
|
|
|
Consider the following code:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
me = app.get_users("me")
|
|
|
|
print(me) # User
|
2019-06-04 14:10:32 +00:00
|
|
|
|
|
|
|
This will show a JSON representation of the object returned by :meth:`~pyrogram.Client.get_users`, which is a
|
2020-08-24 13:24:06 +00:00
|
|
|
:class:`~pyrogram.types.User` instance, in this case. The output on your terminal will be something similar to this:
|
2019-06-04 14:10:32 +00:00
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
|
|
|
|
{
|
2022-01-07 09:18:51 +00:00
|
|
|
"_": "User",
|
|
|
|
"id": 123456789,
|
|
|
|
"is_self": true,
|
2019-06-04 14:10:32 +00:00
|
|
|
"is_contact": false,
|
|
|
|
"is_mutual_contact": false,
|
|
|
|
"is_deleted": false,
|
|
|
|
"is_bot": false,
|
|
|
|
"is_verified": false,
|
|
|
|
"is_restricted": false,
|
|
|
|
"is_support": false,
|
2022-01-07 09:18:51 +00:00
|
|
|
"first_name": "Pyrogram",
|
2019-06-04 14:10:32 +00:00
|
|
|
"photo": {
|
2022-01-07 09:18:51 +00:00
|
|
|
"_": "ChatPhoto",
|
|
|
|
"small_file_id": "AbCdE...EdCbA",
|
|
|
|
"small_photo_unique_id": "VwXyZ...ZyXwV",
|
|
|
|
"big_file_id": "AbCdE...EdCbA",
|
|
|
|
"big_photo_unique_id": "VwXyZ...ZyXwV"
|
2019-06-04 14:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
As you've probably guessed already, Pyrogram objects can be nested. That's how compound data are built, and nesting
|
|
|
|
keeps going until we are left with base data types only, such as ``str``, ``int``, ``bool``, etc.
|
|
|
|
|
|
|
|
Accessing Attributes
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
Even though you see a JSON output, it doesn't mean we are dealing with dictionaries; in fact, all Pyrogram types are
|
2022-01-07 09:18:51 +00:00
|
|
|
fully-fledged Python objects and the correct way to access any attribute of them is by using the dot notation ``.``:
|
2019-06-04 14:10:32 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
photo = me.photo
|
|
|
|
print(photo) # ChatPhoto
|
2019-06-04 14:10:32 +00:00
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
|
|
|
|
{
|
2022-01-07 09:18:51 +00:00
|
|
|
"_": "ChatPhoto",
|
|
|
|
"small_file_id": "AbCdE...EdCbA",
|
|
|
|
"small_photo_unique_id": "VwXyZ...ZyXwV",
|
|
|
|
"big_file_id": "AbCdE...EdCbA",
|
|
|
|
"big_photo_unique_id": "VwXyZ...ZyXwV"
|
2019-06-04 14:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Checking an Object's Type
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
Another thing worth talking about is how to tell and check for an object's type.
|
|
|
|
|
|
|
|
As you noticed already, when printing an object you'll see the special attribute ``"_"``. This is just a visual thing
|
|
|
|
useful to show humans the object type, but doesn't really exist anywhere; any attempt in accessing it will lead to an
|
|
|
|
error. The correct way to get the object type is by using the built-in function ``type()``:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
status = me.status
|
|
|
|
print(type(status))
|
2019-06-04 14:10:32 +00:00
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
2020-08-24 13:24:06 +00:00
|
|
|
<class 'pyrogram.types.UserStatus'>
|
2019-06-04 14:10:32 +00:00
|
|
|
|
|
|
|
And to check if an object is an instance of a given class, you use the built-in function ``isinstance()``:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
:name: this-py
|
|
|
|
|
2020-08-24 13:24:06 +00:00
|
|
|
from pyrogram.types import UserStatus
|
2019-06-04 14:10:32 +00:00
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
status = me.status
|
|
|
|
print(isinstance(status, UserStatus))
|
2019-06-04 14:10:32 +00:00
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
True
|
|
|
|
|
|
|
|
.. raw:: html
|
|
|
|
|
|
|
|
<script>
|
|
|
|
var e = document.querySelector("blockquote p.attribution");
|
|
|
|
var s = e.innerHTML;
|
|
|
|
|
|
|
|
e.innerHTML = s[0] + " " + s.slice(1);
|
|
|
|
</script>
|