Created
January 26, 2018 15:38
-
-
Save edvardm/5d7f494b5c73ccd44ae904bc31cfb4e0 to your computer and use it in GitHub Desktop.
Simple python progress bar updater, using progressbar2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contextlib | |
import os | |
import progressbar | |
@contextlib.contextmanager | |
def build_updater(**kwargs): | |
kwargs['max_value'] = kwargs.get('max_value', progressbar.UnknownLength) | |
update_interval = kwargs.get('update_interval', 1) | |
progress_bar = build_progress_bar(**kwargs) | |
yield _get_updater(progress_bar, update_interval) | |
def build_progress_bar(**kwargs): | |
if os.getenv('PROGRESS_BAR'): | |
return progressbar.ProgressBar(**kwargs) | |
return progressbar.NullBar(**kwargs) | |
def _get_updater(progress_bar, interval=1): | |
def _updater(i): | |
if i % interval == 0: | |
progress_bar.update(i) | |
if interval == 1: | |
return progress_bar | |
return _updater | |
# usage: | |
# with build_updater(max_value=10_000, update_interval=100) as pb: | |
# for idx, elt in enumerate(rather_fast_generator()): | |
# pb(idx) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment