Created
April 24, 2022 02:19
-
-
Save AdamBanham/fabb77421798af1489619cacbad38ea2 to your computer and use it in GitHub Desktop.
Making a coloured progress bar using python (3.9.9)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ast import Global | |
from math import factorial | |
from time import time | |
from colorama import Fore, init as colorama_int | |
colorama_int() | |
PRGRESS_CHAR = '█' | |
PROGRESS_LENGTH = 25 | |
PROGRESS_COLOUR_INC = Fore.YELLOW | |
PROGRESS_COLOR_FIN = Fore.GREEN | |
PROGRESS_START = 0.0 | |
PROGRESS_FIN = 0.0 | |
def progress_bar(progress:float, total:int, message:str="hello there"): | |
global PROGRESS_START, PROGRESS_FIN | |
percent = (progress/ float(total)) | |
if (percent == 0): | |
PROGRESS_START = time() | |
bar_percent = (percent * PROGRESS_LENGTH) | |
bar = PRGRESS_CHAR * int(bar_percent) + '-' * int(PROGRESS_LENGTH - bar_percent) | |
if progress < total: | |
print(PROGRESS_COLOUR_INC + f"\r {message}:{bar}| {(percent * 100):.2f}% ", end="\r" ) | |
print(Fore.RESET,end="\r") | |
else: | |
PROGRESS_FIN = time() | |
completed_time = (PROGRESS_FIN - PROGRESS_START) * 1000 | |
print(PROGRESS_COLOR_FIN + f"\r {message}:{bar}| done! took {completed_time:.0f}ms ", end="\r") | |
print(Fore.RESET,end="\r") | |
print() | |
def main(): | |
pass | |
if __name__ == "__main__": | |
numbers = [ x * 5 for x in range(2000,10000)] | |
results = [] | |
message = "factorising" | |
progress_bar(0, len(numbers),message) | |
for i, x in enumerate(numbers): | |
results.append(factorial(x)) | |
progress_bar(i+1, len(numbers), message) | |
del i,x | |
message = "factorise(x mod 10000)" | |
results_mod = [] | |
progress_bar(0, len(results),message) | |
for i, x in enumerate(results): | |
results_mod.append(factorial(x % 10000)) | |
progress_bar(i+1, len(results), message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment