Skip to content

progress

progress_bar(progress, bar_length=10)

Displays or updates a console progress bar. See: https://stackoverflow.com/a/15860757

Parameters:

Name Type Description Default
(float) progress

a float between 0 and 1. Any int will be converted to a float.

required
Source code in spotPython/utils/progress.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def progress_bar(progress, bar_length=10):
    """
    Displays or updates a console progress bar.
    See: https://stackoverflow.com/a/15860757

    Args:
        (float) progress: a float between 0 and 1. Any int will be converted to a float.
        A value under 0 represents a halt.
        A value at 1 or bigger represents 100%.
    """

    status = ""
    if isinstance(progress, int):
        progress = float(progress)
    if not isinstance(progress, float):
        progress = 0
        status = "error: progress var must be float\r\n"
    if progress < 0:
        progress = 0
        status = "Halt...\r\n"
    if progress >= 1:
        progress = 1
        status = "Done...\r\n"
    block = int(round(bar_length * progress))
    text = "spotPython tuning: [{0}] {1:.2f}% {2}\r".format(
        "#" * block + "-" * (bar_length - block), progress * 100, status
    )
    stdout.write(text)
    stdout.flush()