Skip to content

Commit

Permalink
document format_dict overriding
Browse files Browse the repository at this point in the history
- fully:
    + fixes #562, fixes #482, fixes #494, fixes #656
  • Loading branch information
casperdcl committed Jan 26, 2019
1 parent 3d1ced4 commit ad39d77
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
20 changes: 20 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,26 @@ Points to remember when using ``{postfix[...]}`` in the ``bar_format`` string:
object. To prevent this behaviour, insert an extra item into the dictionary
where the key is not a string.

Additional ``bar_format`` parameters may also be defined by overriding
``format_dict``:

.. code:: python
from tqdm import tqdm
class TqdmExtraFormat(tqdm):
"""Provides a `total_time` format parameter"""
@property
def format_dict(self):
d = super(TqdmExtraFormat, self).format_dict
total_time = d["elapsed"] * (d["total"] or 0) / max(d["n"], 1)
d.update(total_time=self.format_interval(total_time) + " in total")
return d
for i in TqdmExtraFormat(
range(10),
bar_format="{total_time}: {percentage:.0f}%|{bar}{r_bar}"):
pass
Nested progress bars
~~~~~~~~~~~~~~~~~~~~

Expand Down
5 changes: 1 addition & 4 deletions tqdm/tests/tests_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,10 +1033,7 @@ class TqdmExtraFormat(tqdm):
@property
def format_dict(self):
d = super(TqdmExtraFormat, self).format_dict
try:
total_time = d["elapsed"] * d["total"] / d["n"]
except ZeroDivisionError:
total_time = 0
total_time = d["elapsed"] * (d["total"] or 0) / max(d["n"], 1)
d.update(total_time=self.format_interval(total_time) + " in total")
return d

Expand Down

0 comments on commit ad39d77

Please sign in to comment.