-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprinter.py
81 lines (62 loc) · 2.15 KB
/
printer.py
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""A simple printer class that prints in different colors."""
class colors:
"""The colors class"""
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
def print_purple(output: str) -> None:
"""Print an error. Purple output.
Args:
output (str): The string to print.
"""
print(f"{colors.HEADER}{output}{colors.ENDC}")
def print_error(output: str) -> None:
"""Print an error. Red output.
Args:
output (str): The string to print.
"""
print(f"{colors.FAIL}{output}{colors.ENDC}")
def print_warning(output: str) -> None:
"""Print a warning. Yellow output.
Args:
output (str): The string to print.
"""
print(f"{colors.WARNING}{output}{colors.ENDC}")
def print_success(output: str) -> None:
"""Print a success message. Green output.
Args:
output (str): The string to print.
"""
print(f"{colors.OKGREEN}{output}{colors.ENDC}")
def print_progress(
current_iteration: int,
total: int,
decimals: int = 1,
bar_length: int = 100,
prefix: str = "",
suffix: str = "",
) -> None:
"""Print a message with a progress bar.
Args:
current_iteration (int): The current iteration.
total (int): The total iterations to completion
decimals (int, optional): number of decimals in complete percentage. Defaults to 1.
bar_length (int, optional): The length of the progress bar. Defaults to 100.
prefix (str, optional): A message for before the progress bar. Defaults to an empty string.
suffix (str, optional): A message for after the progress bar. Defaults to an empty string.
"""
percent = ("{0:." + str(decimals) + "f}").format(
100 * (current_iteration / float(total))
)
filled_length = int(bar_length * current_iteration // total)
bar = "█" * filled_length + ("-" * (bar_length - filled_length))
print(f"\r{prefix} |{bar}| {percent}% {suffix}", end="")
# Print newline on complete
if current_iteration == total:
print()