-
Notifications
You must be signed in to change notification settings - Fork 12.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Progress bar.py #1950
Open
Piombacciaio
wants to merge
2
commits into
geekcomputers:master
Choose a base branch
from
Piombacciaio:patch-5
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Create Progress bar.py #1950
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import math | ||
import colorama | ||
from colorama import Fore, Style | ||
|
||
colorama.init() | ||
|
||
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 / 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 + 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}') | ||
|
||
numbers = [x * 5 for x in range(2000, 3000)] | ||
lenght = len(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(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!") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improvements made:
This improved version provides a clearer and more visually appealing progress bar while calculating factorials for the given numbers.