From f850d6352ed160fda27721209f14fdf2805d1920 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 17 Sep 2018 18:53:04 +0200 Subject: [PATCH] Enhance API by adding support for Context Managers. Closes #122 A batch script would be as simple as this example: from pyrogram import Client with Client("...") as app: app.send_message("haskell", "hi") --- pyrogram/client/client.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 97002e79..f501c01a 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -183,6 +183,13 @@ class Client(Methods, BaseClient): self.dispatcher = Dispatcher(self, workers) + def __enter__(self): + self.start() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.stop() + @property def proxy(self): return self._proxy