Skip to content

Commit 8f56610

Browse files
committed
Add examples to utility methods
1 parent 72b7a53 commit 8f56610

File tree

1 file changed

+166
-40
lines changed

1 file changed

+166
-40
lines changed

pyrogram/client/client.py

Lines changed: 166 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ class Client(Methods, BaseClient):
174174
download_media, ...) are less prone to throw FloodWait exceptions.
175175
Only available for users, bots will ignore this parameter.
176176
Defaults to False (normal session).
177+
178+
Example:
179+
.. code-block:: python
180+
181+
from pyrogram import Client
182+
183+
app = Client("my_account")
184+
185+
with app:
186+
app.send_message("me", "Hi!")
187+
177188
"""
178189

179190
terms_of_service_displayed = False
@@ -269,11 +280,28 @@ def proxy(self, value):
269280
self._proxy.update(value)
270281

271282
def start(self):
272-
"""Start the Client.
283+
"""Start the client.
284+
285+
This method connects the client to Telegram and, in case of new sessions, automatically manages the full login
286+
process using an interactive prompt (by default).
287+
288+
Has no parameters.
273289
274290
Raises:
275-
RPCError: In case of a Telegram RPC error.
276-
ConnectionError: In case you try to start an already started Client.
291+
ConnectionError: In case you try to start an already started client.
292+
293+
Example:
294+
.. code-block:: python
295+
:emphasize-lines: 4
296+
297+
from pyrogram import Client
298+
299+
app = Client("my_account")
300+
app.start()
301+
302+
... # Call API methods
303+
304+
app.stop()
277305
"""
278306
if self.is_started:
279307
raise ConnectionError("Client has already been started")
@@ -346,8 +374,25 @@ def start(self):
346374
def stop(self):
347375
"""Stop the Client.
348376
377+
This method disconnects the client from Telegram and stops the underlying tasks.
378+
379+
Has no parameters.
380+
349381
Raises:
350-
ConnectionError: In case you try to stop an already stopped Client.
382+
ConnectionError: In case you try to stop an already stopped client.
383+
384+
Example:
385+
.. code-block:: python
386+
:emphasize-lines: 8
387+
388+
from pyrogram import Client
389+
390+
app = Client("my_account")
391+
app.start()
392+
393+
... # Call API methods
394+
395+
app.stop()
351396
"""
352397
if not self.is_started:
353398
raise ConnectionError("Client is already stopped")
@@ -388,8 +433,30 @@ def stop(self):
388433
def restart(self):
389434
"""Restart the Client.
390435
436+
This method will first call :meth:`~Client.stop` and then :meth:`~Client.start` in a row in order to restart
437+
a client using a single method.
438+
439+
Has no parameters.
440+
391441
Raises:
392442
ConnectionError: In case you try to restart a stopped Client.
443+
444+
Example:
445+
.. code-block:: python
446+
:emphasize-lines: 8
447+
448+
from pyrogram import Client
449+
450+
app = Client("my_account")
451+
app.start()
452+
453+
... # Call API methods
454+
455+
app.restart()
456+
457+
... # Call other API methods
458+
459+
app.stop()
393460
"""
394461
self.stop()
395462
self.start()
@@ -451,25 +518,40 @@ def signal_handler(*args):
451518
time.sleep(1)
452519

453520
def run(self):
454-
"""Start the Client and automatically idle the main script.
521+
"""Start the client, idle the main script and finally stop the client.
522+
523+
This is a convenience method that calls :meth:`~Client.start`, :meth:`~Client.idle` and :meth:`~Client.stop` in
524+
sequence. It makes running a client less verbose, but is not suitable in case you want to run more than one
525+
client in a single main script, since idle() will block after starting the own client.
455526
456-
This is a convenience method that literally just calls :meth:`~Client.start` and :meth:`~Client.idle`. It makes
457-
running a client less verbose, but is not suitable in case you want to run more than one client in a single main
458-
script, since :meth:`~Client.idle` will block.
527+
Has no parameters.
459528
460529
Raises:
461-
RPCError: In case of a Telegram RPC error.
530+
ConnectionError: In case you try to run an already started client.
531+
532+
Example:
533+
.. code-block:: python
534+
:emphasize-lines: 7
535+
536+
from pyrogram import Client
537+
538+
app = Client("my_account")
539+
540+
... # Set handlers up
541+
542+
app.run()
462543
"""
463544
self.start()
464-
self.idle()
545+
Client.idle()
465546
self.stop()
466547

467548
def add_handler(self, handler: Handler, group: int = 0):
468549
"""Register an update handler.
469550
470-
You can register multiple handlers, but at most one handler within a group
471-
will be used for a single update. To handle the same update more than once, register
472-
your handler using a different group id (lower group id == higher priority).
551+
You can register multiple handlers, but at most one handler within a group will be used for a single update.
552+
To handle the same update more than once, register your handler using a different group id (lower group id
553+
== higher priority). This mechanism is explained in greater details at
554+
:doc:`More on Updates <../../topics/more-on-updates>`.
473555
474556
Parameters:
475557
handler (``Handler``):
@@ -479,7 +561,22 @@ def add_handler(self, handler: Handler, group: int = 0):
479561
The group identifier, defaults to 0.
480562
481563
Returns:
482-
``tuple``: A tuple consisting of (handler, group).
564+
``tuple``: A tuple consisting of *(handler, group)*.
565+
566+
Example:
567+
.. code-block:: python
568+
:emphasize-lines: 8
569+
570+
from pyrogram import Client, MessageHandler
571+
572+
def dump(client, message):
573+
print(message)
574+
575+
app = Client("my_account")
576+
577+
app.add_handler(MessageHandler(dump))
578+
579+
app.run()
483580
"""
484581
if isinstance(handler, DisconnectHandler):
485582
self.disconnect_handler = handler.callback
@@ -491,16 +588,33 @@ def add_handler(self, handler: Handler, group: int = 0):
491588
def remove_handler(self, handler: Handler, group: int = 0):
492589
"""Remove a previously-registered update handler.
493590
494-
Make sure to provide the right group that the handler was added in. You can use
495-
the return value of the :meth:`~Client.add_handler` method, a tuple of (handler, group), and
496-
pass it directly.
591+
Make sure to provide the right group where the handler was added in. You can use the return value of the
592+
:meth:`~Client.add_handler` method, a tuple of *(handler, group)*, and pass it directly.
497593
498594
Parameters:
499595
handler (``Handler``):
500596
The handler to be removed.
501597
502598
group (``int``, *optional*):
503599
The group identifier, defaults to 0.
600+
601+
Example:
602+
.. code-block:: python
603+
:emphasize-lines: 11
604+
605+
from pyrogram import Client, MessageHandler
606+
607+
def dump(client, message):
608+
print(message)
609+
610+
app = Client("my_account")
611+
612+
handler = app.add_handler(MessageHandler(dump))
613+
614+
# Starred expression to unpack (handler, group)
615+
app.remove_handler(*handler)
616+
617+
app.run()
504618
"""
505619
if isinstance(handler, DisconnectHandler):
506620
self.disconnect_handler = None
@@ -509,7 +623,28 @@ def remove_handler(self, handler: Handler, group: int = 0):
509623

510624
def stop_transmission(self):
511625
"""Stop downloading or uploading a file.
512-
Must be called inside a progress callback function.
626+
627+
This method must be called inside a progress callback function in order to stop the transmission at the
628+
desired time. The progress callback is called every time a file chunk is uploaded/downloaded.
629+
630+
Has no parameters.
631+
632+
Example:
633+
.. code-block:: python
634+
:emphasize-lines: 9
635+
636+
from pyrogram import Client
637+
638+
app = Client("my_account")
639+
640+
# Example to stop transmission once the upload progress reaches 50%
641+
# Useless in practice, but shows how to stop on command
642+
def progress(client, current, total):
643+
if (current * 100 / total) > 50:
644+
client.stop_transmission()
645+
646+
with app:
647+
app.send_document("me", "files.zip", progress=progress)
513648
"""
514649
raise Client.StopTransmission
515650

@@ -541,9 +676,9 @@ def export_session_string(self):
541676
def set_parse_mode(self, parse_mode: Union[str, None] = "combined"):
542677
"""Set the parse mode to be used globally by the client.
543678
544-
When setting the parse mode with this method, all methods having a *parse_mode* parameter will follow the global
545-
value by default. The default value *"combined"* enables both Markdown and HTML styles to be used and combined
546-
together.
679+
When setting the parse mode with this method, all other methods having a *parse_mode* parameter will follow the
680+
global value by default. The default value *"combined"* enables both Markdown and HTML styles to be used and
681+
combined together.
547682
548683
Parameters:
549684
parse_mode (``str``):
@@ -1172,7 +1307,7 @@ def load_session(self):
11721307
])
11731308

11741309
if session_empty:
1175-
self.storage.dc_id = 1
1310+
self.storage.dc_id = 4
11761311
self.storage.date = 0
11771312

11781313
self.storage.test_mode = self.test_mode
@@ -1445,23 +1580,22 @@ def save_file(
14451580
In case a file part expired, pass the file_id and the file_part to retry uploading that specific chunk.
14461581
14471582
progress (``callable``, *optional*):
1448-
Pass a callback function to view the upload progress.
1449-
The function must take *(client, current, total, \*args)* as positional arguments (look at the section
1450-
below for a detailed description).
1583+
Pass a callback function to view the file transmission progress.
1584+
The function must take *(current, total)* as positional arguments (look at Other Parameters below for a
1585+
detailed description) and will be called back each time a new file chunk has been successfully
1586+
transmitted.
14511587
14521588
progress_args (``tuple``, *optional*):
1453-
Extra custom arguments for the progress callback function. Useful, for example, if you want to pass
1454-
a chat_id and a message_id in order to edit a message with the updated progress.
1589+
Extra custom arguments for the progress callback function.
1590+
You can pass anything you need to be available in the progress callback scope; for example, a Message
1591+
object or a Client instance in order to edit the message with the updated progress status.
14551592
14561593
Other Parameters:
1457-
client (:obj:`Client`):
1458-
The Client itself, useful when you want to call other API methods inside the callback function.
1459-
14601594
current (``int``):
1461-
The amount of bytes uploaded so far.
1595+
The amount of bytes transmitted so far.
14621596
14631597
total (``int``):
1464-
The size of the file.
1598+
The total size of the file.
14651599
14661600
*args (``tuple``, *optional*):
14671601
Extra custom arguments as defined in the *progress_args* parameter.
@@ -1775,11 +1909,3 @@ def guess_extension(self, mime_type: str):
17751909

17761910
if extensions:
17771911
return extensions.split(" ")[0]
1778-
1779-
def export_session_string(self):
1780-
"""Export the current session as serialized string.
1781-
1782-
Returns:
1783-
``str``: The session serialized into a printable, url-safe string.
1784-
"""
1785-
return self.storage.export_session_string()

0 commit comments

Comments
 (0)