Progress bar
When doing work that requires a lot of time (usually anything that requires I/O to slower endpoints, such as disk or network), it's a good idea to let your user know that you are moving forward and how much work is left to do. Progress bars, while not precise, are a very good way to give our users an overview of how much work we have done so far and how much we have left to do.
How to do it...
The recipe steps are as follows:
- The progress bar itself will be displayed by a decorator, so that we can apply it to any function for which we want to report progress with minimum effort:
import shutil, sys def withprogressbar(func): """Decorates ``func`` to display a progress bar while running. The decorated function can yield values from 0 to 100 to display the progress. """ def _func_with_progress(*args, **kwargs): max_width, _ = shutil.get_terminal_size() gen = func(*args, **kwargs) while True: try: progress =...