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

Add postfixes to tqdm.rich #1510

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 37 additions & 4 deletions tqdm/rich.py
Expand Up @@ -9,14 +9,17 @@
from warnings import warn

from rich.progress import (
BarColumn, Progress, ProgressColumn, Text, TimeElapsedColumn, TimeRemainingColumn, filesize)
BarColumn, Progress, ProgressColumn, Text, TimeElapsedColumn, TimeRemainingColumn,
filesize, TextColumn)

from .std import TqdmExperimentalWarning
from .std import tqdm as std_tqdm
from typing import TypeAlias

__author__ = {"github.com/": ["casperdcl"]}
__all__ = ['tqdm_rich', 'trrange', 'tqdm', 'trange']

PostfixColumn: TypeAlias = TextColumn

class FractionColumn(ProgressColumn):
"""Renders completed/total, e.g. '0.5/2.3 G'."""
Expand Down Expand Up @@ -97,17 +100,20 @@

warn("rich is experimental/alpha", TqdmExperimentalWarning, stacklevel=2)
d = self.format_dict
postfix = TextColumn(self.postfix) if self.postfix is not None else None

Check warning on line 103 in tqdm/rich.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tqdm/rich.py#L103

Unused variable 'postfix'

Check notice on line 103 in tqdm/rich.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tqdm/rich.py#L103

local variable 'postfix' is assigned to but never used (F841)
if progress is None:
progress = (
progress = [
"[progress.description]{task.description}"
"[progress.percentage]{task.percentage:>4.0f}%",
BarColumn(bar_width=None),
FractionColumn(
unit_scale=d['unit_scale'], unit_divisor=d['unit_divisor']),
"[", TimeElapsedColumn(), "<", TimeRemainingColumn(),
",", RateColumn(unit=d['unit'], unit_scale=d['unit_scale'],
unit_divisor=d['unit_divisor']), "]"
)
unit_divisor=d['unit_divisor'])]
if self.postfix is not None:
progress.extend([",", PostfixColumn("{task.fields[postfix]}")])
progress.append("]")
options.setdefault('transient', not self.leave)
self._prog = Progress(*progress, **options)
self._prog.__enter__()
Expand All @@ -122,6 +128,33 @@
def clear(self, *_, **__):
pass

def _update_postfix(self):
if self.postfix is None and type(self._prog.columns[-1]) is not PostfixColumn:

Check notice on line 132 in tqdm/rich.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tqdm/rich.py#L132

Use isinstance() rather than type() for a typecheck.
columns = list(self._prog.columns)
columns.pop(-1)
columns.pop(-1)
self._prog.columns = tuple(columns)
elif self.postfix is not None and type(self._prog.columns[-2]) is not PostfixColumn:

Check notice on line 137 in tqdm/rich.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tqdm/rich.py#L137

Use isinstance() rather than type() for a typecheck.
if type(self._prog.columns[-2]) is not PostfixColumn:

Check notice on line 138 in tqdm/rich.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tqdm/rich.py#L138

Use isinstance() rather than type() for a typecheck.
# Add "," and column for postfix if it does not exist
columns = list(self._prog.columns)
columns.insert(-1, ",")
columns.insert(-1,
PostfixColumn("{task.fields[postfix]}"))
self._prog.columns = tuple(columns)
self._prog.update(self._task_id, postfix=self.postfix)


def set_postfix(self, ordered_dict=None, refresh=True, **kwargs):
super_result = super().set_postfix(ordered_dict, refresh, **kwargs)

Check failure on line 149 in tqdm/rich.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tqdm/rich.py#L149

Assigning result of a function call, where the function has no return
self._update_postfix()
return super_result

def set_postfix_str(self, s='', refresh=True):
super_result = super().set_postfix_str(s, refresh)

Check failure on line 154 in tqdm/rich.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tqdm/rich.py#L154

Assigning result of a function call, where the function has no return
self._update_postfix()
return super_result

def display(self, *_, **__):
if not hasattr(self, '_prog'):
return
Expand Down