From 692b22bc467dd1e4d3677c2180775f35cee97dd2 Mon Sep 17 00:00:00 2001 From: Andrea Piombo Date: Sun, 30 Jul 2023 22:18:50 +0200 Subject: [PATCH 1/2] Create Progress bar.py --- Progress bar.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Progress bar.py diff --git a/Progress bar.py b/Progress bar.py new file mode 100644 index 00000000000..36390785f37 --- /dev/null +++ b/Progress bar.py @@ -0,0 +1,45 @@ +import math, colorama +from colorama import Fore + +colorama.init() +def progress_bar(progress:int = 0, total:int = 100, color=Fore.CYAN, complete_color=Fore.GREEN): + """ + - progress:int --> current progress of the operation (ex. index of an item inside a list) + - total:int --> the actual lenght of the operation (ex. the lenght of a list of items) + - color --> color for the bar and percentage value during the operation + - complete_color --> color for the bar and percentage value once the operation is complete + """ + + percent = 100 * (progress / float (total)) #Calculate the percentage of completion by multiplying the ratio of progress over total for 100 + bar = "█" * int(percent) + "-" * (100 - int(percent)) #Create the actual bar by multiplying the bar character for the percentage of completion and multiplying the remaining character for the difference + + print(f"\r|{color + bar + Fore.RESET}| {color}{percent:.2f}{Fore.RESET}%", end="\r") + #Using f-strings and print statement's parameter: + #\r --> Special escape character that allows to keep writing on the same section of the line (in this case from the beginning) + #{color + bar + Fore.RESET} --> Sums the Fore color character to create a pretty colored bar on the screen + #percent:.2f --> The :.xf allows to round to x value of decimal points (2 in this case). It is important, for this script, that the two print statement use the same x value to avoid misprinting + #{color}{percent:.2f}{Fore.RESET} --> Same as previous but since "percent" is a float it can't be directly added to a string. + # To fix this you can either use this sintax or convert it yourself to str by doing {color + str(percent:.2f) + Fore.RESET} + #end="\r" --> The "end" parameter specify how the line print should end. Here "\r" goes back to the previously printed one (at the start of the line) + + if progress == total: + print(f"\r|{complete_color + bar + Fore.RESET}| {complete_color}{percent:.2f}{Fore.RESET}%", end="\n") + #Same as the previous print statement but here it changes the color of the complete bar and add a new line indicaton at the end of the line (end="\n") + +#The simplest imlpementation of this progressbar script I could think of +numbers = [x * 5 for x in range (2000, 3000)] #Generate a list of 1000 ints +results = [] +lenght = len(numbers) #Get the total lenght of the operation + +progress_bar (0, total=lenght, color=Fore.CYAN, complete_color=Fore.GREEN) +# Set the bar progress to 0 + +for i, x in enumerate(numbers): + #Iterate over the list of ints with enumerate to get both the index "i" (used for progress) and the value of the int "x" to execute the operation + results.append(math.factorial(x)) + progress_bar(i + 1, total=lenght, color=Fore.CYAN, complete_color=Fore.GREEN) + #Expaination: + #i + 1 --> Since indexes start at "0", we add 1 to give the progress value to the first iteration and increase it by one at every following iteration for consistency + #total=lenght --> Used to calculate the percentage of progress. In this case is the lenght of the list of numbers to factorialize + #color=Fore.CYAN --> Is the color of the progress bar during the operation + #complete_color=Fore.GREEN --> Is the color of the bar on completion From 864f1bed3616b83f3ece0a46078a8fc575471ac3 Mon Sep 17 00:00:00 2001 From: Andrea Piombo Date: Mon, 31 Jul 2023 00:17:43 +0200 Subject: [PATCH 2/2] Update Progress bar.py Updated the program with the suggestions from NitkarshChourasia and added less repetitive comments --- Progress bar.py | 59 ++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/Progress bar.py b/Progress bar.py index 36390785f37..b0d34a56488 100644 --- a/Progress bar.py +++ b/Progress bar.py @@ -1,45 +1,44 @@ -import math, colorama -from colorama import Fore +import math +import colorama +from colorama import Fore, Style colorama.init() -def progress_bar(progress:int = 0, total:int = 100, color=Fore.CYAN, complete_color=Fore.GREEN): + +def progress_bar(progress, total, color=Fore.CYAN, complete_color=Fore.GREEN): """ - progress:int --> current progress of the operation (ex. index of an item inside a list) - total:int --> the actual lenght of the operation (ex. the lenght of a list of items) - color --> color for the bar and percentage value during the operation - complete_color --> color for the bar and percentage value once the operation is complete """ - - percent = 100 * (progress / float (total)) #Calculate the percentage of completion by multiplying the ratio of progress over total for 100 - bar = "█" * int(percent) + "-" * (100 - int(percent)) #Create the actual bar by multiplying the bar character for the percentage of completion and multiplying the remaining character for the difference - print(f"\r|{color + bar + Fore.RESET}| {color}{percent:.2f}{Fore.RESET}%", end="\r") + percent = 100 * (progress / total) + bar_length = 50 #Set fixed lenght of the bar to 50 chars (Thanks to NitkarshChourasia for improvement) + completed_length = int(bar_length * (progress / total)) + bar = '█' * completed_length + '-' * (bar_length - completed_length) + + print(f'\r|{color}{bar}{Style.RESET_ALL}| {color}{percent:.2f}%{Style.RESET_ALL}', end='', flush=True) #Using f-strings and print statement's parameter: #\r --> Special escape character that allows to keep writing on the same section of the line (in this case from the beginning) - #{color + bar + Fore.RESET} --> Sums the Fore color character to create a pretty colored bar on the screen - #percent:.2f --> The :.xf allows to round to x value of decimal points (2 in this case). It is important, for this script, that the two print statement use the same x value to avoid misprinting - #{color}{percent:.2f}{Fore.RESET} --> Same as previous but since "percent" is a float it can't be directly added to a string. - # To fix this you can either use this sintax or convert it yourself to str by doing {color + str(percent:.2f) + Fore.RESET} - #end="\r" --> The "end" parameter specify how the line print should end. Here "\r" goes back to the previously printed one (at the start of the line) - - if progress == total: - print(f"\r|{complete_color + bar + Fore.RESET}| {complete_color}{percent:.2f}{Fore.RESET}%", end="\n") - #Same as the previous print statement but here it changes the color of the complete bar and add a new line indicaton at the end of the line (end="\n") - -#The simplest imlpementation of this progressbar script I could think of -numbers = [x * 5 for x in range (2000, 3000)] #Generate a list of 1000 ints -results = [] -lenght = len(numbers) #Get the total lenght of the operation + #{color + bar + Style.RESET_ALL} --> Sums the Fore color character to create a pretty colored bar on the screen + #percent:.2f --> ":.xf" allows to round to x value of decimal points (2 in this case). It is important, for this script, that the two print statement use the same x value to avoid misprinting + #{color}{percent:.2f}{Style.RESET_ALL} --> Same as previous but since "percent" is of type float it can't be directly added to a string. + # To fix this you can either use this sintax or convert it to str by doing {color + str(percent:.2f) + Style.RESET_ALL} + #flush=True --> ensure real-time printing of the progress bar without buffering + + if progress == total: + print(f'\r|{complete_color}{bar}{Style.RESET_ALL}| {complete_color}{percent:.2f}%{Style.RESET_ALL}') -progress_bar (0, total=lenght, color=Fore.CYAN, complete_color=Fore.GREEN) -# Set the bar progress to 0 +numbers = [x * 5 for x in range(2000, 3000)] +lenght = len(numbers) -for i, x in enumerate(numbers): +progress_bar(0, total=lenght) + +results = [] +for i, x in enumerate(numbers): #Iterate over the list of ints with enumerate to get both the index "i" (used for progress) and the value of the int "x" to execute the operation results.append(math.factorial(x)) - progress_bar(i + 1, total=lenght, color=Fore.CYAN, complete_color=Fore.GREEN) - #Expaination: - #i + 1 --> Since indexes start at "0", we add 1 to give the progress value to the first iteration and increase it by one at every following iteration for consistency - #total=lenght --> Used to calculate the percentage of progress. In this case is the lenght of the list of numbers to factorialize - #color=Fore.CYAN --> Is the color of the progress bar during the operation - #complete_color=Fore.GREEN --> Is the color of the bar on completion + progress_bar(progress=(i + 1), total=lenght) + #Since indexes start at 0, add 1 to the first iteration and increase it by one at every following iteration for consistency + +print("\nFactorials calculated successfully!")