cornsnake.util_progress

Functions for tracking progress and updating a progress bar.

Documentation

 1"""
 2Functions for tracking progress and updating a progress bar.
 3
 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_progress.html)
 5"""
 6
 7import sys
 8
 9from . import config
10
11previous_percent = 0
12
13
14def progress(count, total):
15    """
16    Function to update progress based on count and total.
17
18    Args:
19    count (int): The current count.
20    total (int): The total count.
21
22    Returns:
23    None
24    """
25    global previous_percent
26
27    percent = round(100.0 * count / float(total), 1)
28
29    if config.MINIMIZE_PROGRESS_BAR_OUTPUT:
30        if percent < previous_percent:
31            # reset:
32            previous_percent = percent
33        else:
34            if percent - previous_percent < 10:
35                return
36    previous_percent = percent
37
38    _update_progress(percent)
39
40
41def _update_progress(percent):
42    """
43    Function to update the progress bar based on percentage.
44
45    Args:
46    percent (float): The percentage of progress.
47
48    Returns:
49    None
50    """
51    bar_len = 60
52    filled_len = int(round(bar_len * percent / float(100)))
53
54    bar = "=" * filled_len + "-" * (bar_len - filled_len)
55
56    fmt = "[%s] %s%s ..." % (bar, percent, "%")
57    print("\b" * len(fmt), end="")  # clears the line
58    sys.stdout.write(fmt)
59    sys.stdout.flush()
60
61
62def complete():
63    """
64    Function to complete the progress bar by updating it to 100% and printing a new line.
65
66    Returns:
67    None
68    """
69    _update_progress(float(100))
70    print("")  # ensure a new line
previous_percent = 0
def progress(count, total):
15def progress(count, total):
16    """
17    Function to update progress based on count and total.
18
19    Args:
20    count (int): The current count.
21    total (int): The total count.
22
23    Returns:
24    None
25    """
26    global previous_percent
27
28    percent = round(100.0 * count / float(total), 1)
29
30    if config.MINIMIZE_PROGRESS_BAR_OUTPUT:
31        if percent < previous_percent:
32            # reset:
33            previous_percent = percent
34        else:
35            if percent - previous_percent < 10:
36                return
37    previous_percent = percent
38
39    _update_progress(percent)

Function to update progress based on count and total.

Args: count (int): The current count. total (int): The total count.

Returns: None

def complete():
63def complete():
64    """
65    Function to complete the progress bar by updating it to 100% and printing a new line.
66
67    Returns:
68    None
69    """
70    _update_progress(float(100))
71    print("")  # ensure a new line

Function to complete the progress bar by updating it to 100% and printing a new line.

Returns: None