Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telegram: add delete() when leave=False #1189

Merged
merged 2 commits into from Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions tqdm/contrib/telegram.py
Expand Up @@ -62,6 +62,17 @@ def write(self, s):
else:
return future

def delete(self):
"""Deletes internal `message_id`."""
try:
future = self.submit(
self.session.post, self.API + '%s/deleteMessage' % self.token,
data={'chat_id': self.chat_id, 'message_id': self.message_id})
except Exception as e:
tqdm_auto.write(str(e))
else:
return future


class tqdm_telegram(tqdm_auto):
"""
Expand Down Expand Up @@ -112,6 +123,13 @@ def clear(self, *args, **kwargs):
if not self.disable:
self.tgio.write("")

def close(self):
if self.disable:
return
super(tqdm_telegram, self).close()
if not (self.leave or (self.leave is None and self.pos == 0)):
Copy link
Sponsor Member

@casperdcl casperdcl Jun 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

borrowing usual leave=None|False|True logic here.

Potential future issue is if people want different behaviour for terminal & telegram outputs (leave one but not the other). They'd have to override the close() method. Don't think we need to explicitly support/worry about that case for now.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the leave variable is perfect, it fits our needs, and yes the default behaviour would be leave = False. In the meantime we don't worry about that case for terminal and telegram outputs. When the close() method is called just delete the message from telegram and stop the bar from terminal.

self.tgio.delete()


def ttgrange(*args, **kwargs):
"""
Expand Down
4 changes: 2 additions & 2 deletions tqdm/rich.py
Expand Up @@ -113,10 +113,10 @@ def __init__(self, *args, **kwargs):
self._prog.__enter__()
self._task_id = self._prog.add_task(self.desc or "", **d)

def close(self, *args, **kwargs):
def close(self):
if self.disable:
return
super(tqdm_rich, self).close(*args, **kwargs)
super(tqdm_rich, self).close()
self._prog.__exit__(None, None, None)

def clear(self, *_, **__):
Expand Down