Last active
July 7, 2023 08:41
-
-
Save chavarera/37a730a6b10091605abdc21cd49673cb to your computer and use it in GitHub Desktop.
Displays a progress bar while iterating over an iterable.
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
""" | |
Author: Ravishankar Chavare | |
Version: 1.0 | |
File: pwbar.py | |
Description: This module provides a progress bar utility for tracking the | |
progress of iterative tasks. | |
1.1 | |
=== | |
- Added green text to percentage | |
- Added example in __main__ | |
1.0 | |
=== | |
- Initial release | |
""" | |
import time | |
import inspect | |
def pwbar(iterable, desc=None, bar_length=10): | |
""" | |
Displays a progress bar while iterating over an iterable. | |
Args: | |
iterable: The iterable to iterate over. | |
desc (str): Optional description for the task. If not provided, | |
the function name will be used. | |
bar_length (int): Length of the progress bar (default is 10). | |
Yields: | |
The items from the iterable. | |
Example: | |
>>> for item in pwbar(range(10), desc='Processing'): | |
... # process the item | |
""" | |
if desc: | |
task_name = desc | |
else: | |
caller_frame = inspect.currentframe().f_back | |
task_name = caller_frame.f_code.co_qualname | |
# Print the function name | |
print(task_name, ":") | |
# Get the total number of iterations from the iterable | |
total_iterations = len(iterable) | |
start_time = time.time() | |
for i, item in enumerate(iterable, start=1): | |
# Calculate progress percentage | |
progress = (i / total_iterations) * 100 | |
# Create the progress bar | |
filled_length = int(progress / 100 * bar_length) | |
# Calculate elapsed time | |
elapsed_time = time.time() - start_time | |
# Calculate estimated remaining time | |
remaining_items = total_iterations - i | |
estimated_remaining_time = (elapsed_time / i) * remaining_items | |
# Calculate color shade based on progress | |
color_code = "\033[91m" # Default to red | |
if progress >= 33: | |
color_code = "\033[93m" # Orange | |
if progress >= 66: | |
color_code = "\033[92m" # Green | |
# Create the progress bar | |
filled_length = int(progress / 100 * bar_length) | |
bar = ( | |
color_code | |
+ "█" * filled_length | |
+ "\033[0m" | |
+ "-" * (bar_length - filled_length) | |
) | |
# Print the progress bar, percentage, completed and remaining items, elapsed time, | |
# and estimated remaining time | |
print( | |
f"\r\t[{bar} \033[92m{progress:.2f}% \033[0m ({i}/{total_iterations}) <{estimated_remaining_time:.2f}s/{elapsed_time:.2f}s> ]", | |
end="", | |
) | |
yield item | |
# Print a newline character after completion | |
print() | |
if __name__=='__main__': | |
for i in pwbar(range(79),desc='Funtion Name'): | |
import time | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Example