def test_progressbar_init_exceptions(runner): try: click.progressbar() except TypeError as e: assert str(e) == 'iterable or length is required' else: assert False, 'Expected an exception because unspecified arguments'
def progress(count): """Demonstrates the progress bar.""" items = range_type(count) def process_slowly(item): time.sleep(0.002 * random.random()) def filter(items): for item in items: if random.random() > 0.3: yield item with click.progressbar(items, label='Processing accounts', fill_char=click.style('#', fg='green')) as bar: for item in bar: process_slowly(item) def show_item(item): if item is not None: return 'Item #%d' % item with click.progressbar(filter(items), label='Committing transaction', fill_char=click.style('#', fg='yellow'), item_show_func=show_item) as bar: for item in bar: process_slowly(item) with click.progressbar(length=count, label='Counting', bar_template='%(label)s %(bar)s | %(info)s', fill_char=click.style(u'█', fg='cyan'), empty_char=' ') as bar: for item in bar: process_slowly(item) with click.progressbar(length=count, width=0, show_percent=False, show_eta=False, fill_char=click.style('#', fg='magenta')) as bar: for item in bar: process_slowly(item) # 'Non-linear progress bar' steps = [math.exp( x * 1. / 20) - 1 for x in range(20)] count = int(sum(steps)) with click.progressbar(length=count, show_percent=False, label='Slowing progress bar', fill_char=click.style(u'█', fg='green')) as bar: for item in steps: time.sleep(item) bar.update(item)
def cli(): with click.progressbar(range(10), label="test") as progress: while True: try: next(progress) except StopIteration: break
def test_progressbar_iter_outside_with_exceptions(runner): try: progress = click.progressbar(length=2) iter(progress) except RuntimeError as e: assert str(e) == 'You need to use progress bars in a with block.' else: assert False, 'Expected an exception because of abort-related inputs.'
def cli(): with click.progressbar(range(10), label="test") as progress: while True: try: next(progress) fake_clock.advance_time() except StopIteration: break
def cli(): with click.progressbar(Hinted(10), label='test') as progress: for thing in progress: fake_clock.advance_time()
def cli(): with click.progressbar(range(4)) as progress: for _ in progress: fake_clock.advance_time() print("")
def test_progressbar_yields_all_items(runner): with click.progressbar(range(3)) as progress: assert len(list(progress)) == 3
def _create_progress(length=10, length_known=True, **kwargs): progress = click.progressbar(tuple(range(length))) for key, value in kwargs.items(): setattr(progress, key, value) progress.length_known = length_known return progress
def cli(): with click.progressbar(Hinted(10), label="test") as progress: for _ in progress: pass
def cli(): with click.progressbar( length=6, item_show_func=lambda x: f"Custom {x}") as progress: while not progress.finished: progress.update(2, progress.pos) click.echo()
def cli(): with click.progressbar(range(3), item_show_func=lambda x: str(x)) as progress: for item in progress: click.echo(f" item {item}")
def test_progressbar_iter_outside_with_exceptions(runner): with pytest.raises(RuntimeError, match="with block"): progress = click.progressbar(length=2) iter(progress)
def test_progressbar_init_exceptions(runner): with pytest.raises(TypeError, match="iterable or length is required"): click.progressbar()
def progress(count): """Demonstrates the progress bar.""" items = range(count) def process_slowly(item): time.sleep(0.002 * random.random()) def filter(items): for item in items: if random.random() > 0.3: yield item with click.progressbar( items, label="Processing accounts", fill_char=click.style("#", fg="green") ) as bar: for item in bar: process_slowly(item) def show_item(item): if item is not None: return f"Item #{item}" with click.progressbar( filter(items), label="Committing transaction", fill_char=click.style("#", fg="yellow"), item_show_func=show_item, ) as bar: for item in bar: process_slowly(item) with click.progressbar( length=count, label="Counting", bar_template="%(label)s %(bar)s | %(info)s", fill_char=click.style("█", fg="cyan"), empty_char=" ", ) as bar: for item in bar: process_slowly(item) with click.progressbar( length=count, width=0, show_percent=False, show_eta=False, fill_char=click.style("#", fg="magenta"), ) as bar: for item in bar: process_slowly(item) # 'Non-linear progress bar' steps = [math.exp(x * 1.0 / 20) - 1 for x in range(20)] count = int(sum(steps)) with click.progressbar( length=count, show_percent=False, label="Slowing progress bar", fill_char=click.style("█", fg="green"), ) as bar: for item in steps: time.sleep(item) bar.update(item)
def cli(): with click.progressbar(Hinted(10), label="test") as progress: for _ in progress: fake_clock.advance_time()