Beispiel #1
0
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'
Beispiel #2
0
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)
Beispiel #3
0
 def cli():
     with click.progressbar(range(10), label="test") as progress:
         while True:
             try:
                 next(progress)
             except StopIteration:
                 break
Beispiel #4
0
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.'
Beispiel #5
0
 def cli():
     with click.progressbar(range(10), label="test") as progress:
         while True:
             try:
                 next(progress)
                 fake_clock.advance_time()
             except StopIteration:
                 break
Beispiel #6
0
 def cli():
     with click.progressbar(Hinted(10), label='test') as progress:
         for thing in progress:
             fake_clock.advance_time()
Beispiel #7
0
 def cli():
     with click.progressbar(range(4)) as progress:
         for _ in progress:
             fake_clock.advance_time()
             print("")
Beispiel #8
0
def test_progressbar_yields_all_items(runner):
    with click.progressbar(range(3)) as progress:
        assert len(list(progress)) == 3
Beispiel #9
0
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
Beispiel #10
0
 def cli():
     with click.progressbar(Hinted(10), label="test") as progress:
         for _ in progress:
             pass
Beispiel #11
0
 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()
Beispiel #12
0
 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}")
Beispiel #13
0
def test_progressbar_iter_outside_with_exceptions(runner):
    with pytest.raises(RuntimeError, match="with block"):
        progress = click.progressbar(length=2)
        iter(progress)
Beispiel #14
0
def test_progressbar_init_exceptions(runner):
    with pytest.raises(TypeError, match="iterable or length is required"):
        click.progressbar()
Beispiel #15
0
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)
Beispiel #16
0
 def cli():
     with click.progressbar(Hinted(10), label="test") as progress:
         for _ in progress:
             fake_clock.advance_time()