bag.show_progress module

2 solutions for showing progress periodically on the console.

To use them, encapsulate your iterable with these generators.

When you know how many items you’re going to process, you can print the percentage done and the time remaining. Otherwise, you can only print the iteration index. The output messages are configurable; here are examples:

$ python -c "from bag.show_progress import *; test_percentage()"
7.0% done, 0:00:53 left...
15.0% done, 0:00:45 left...
23.0% done, 0:00:40 left...
31.0% done, 0:00:35 left...
39.0% done, 0:00:31 left...
47.0% done, 0:00:27 left...
55.0% done, 0:00:22 left...
63.0% done, 0:00:18 left...
71.0% done, 0:00:14 left...
79.0% done, 0:00:10 left...
87.0% done, 0:00:06 left...
95.0% done, 0:00:02 left...

$ python -c "from bag.show_progress import *; test_progress()"
Item #17 done. Working...
Item #34 done. Working...
Item #51 done. Working...
Item #68 done. Working...
Item #85 done. Working...
Done in 0:00:23.726902! Total items: 100

Both solutions decide when to print out a progress message based on time, not on the number of iterations, so updates tend to appear steadily on the screen.

class bag.show_progress.PercentageDone(max, granularity=6)[source]

Bases: object

When you are processing a long iterable and it takes minutes, you should let the user know that your application is still working. This class helps do that in the console, without creating too much output.


Takes val (the current position relative to max and calculates:

  • self.current (int): the current percentage done
  • (timedelta): time elapsed since self.start
  • self.estimate (timedelta): how long this is going to take (total)
  • self.remaining (timedelta): how long you still have to wait

Returns self.remaining.


Calls self.calc() and prints the percentage done and how long the user still has to wait.

But only does so every X seconds, where X is granularity. Does nothing if the granularity has not elapsed yet.

class bag.show_progress.ShowingPercentage(iterable, max, **k)[source]

Bases: bag.show_progress.PercentageDone

A generator that encapsulates your iterable, printing the percentage done.


p = ShowingPercentage(iterable, len(iterable), granularity=6)
# Then use p instead of your iterable:
for index, something in p:
class bag.show_progress.ShowingProgress(iterable, message='Item #{} done. Working...', seconds=6, done='Done in {time}! Total items: {total}')[source]

Bases: object

A generator that encapsulates your iterable.

It prints the progress every so many seconds. Usage:

p = ShowingProgress(iterable, seconds=6)
# Then use p instead of your iterable:
for index, something in p:

Demonstration of ShowingPercentage usage.


Demonstration of ShowingProgress usage.