Skip to content

Implement Dynamic Progress Logger #12964

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions progress_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class ProgressLogger:
"""A utility class to log output dynamically with a progress bar."""

def __init__(self, total_steps: int, prefix: str = "Progress:") -> None:
"""
Initialize the ProgressLogger instance.

:param total_steps: The total number of steps to track.
:param prefix: The prefix message to display before the progress bar.
"""
self.total_steps = total_steps
self.prefix = prefix
self.current_step = 0

def __str__(self) -> str:
"""
Return the progress bar as a string representation.

:return: The progress bar string.
"""
progress = int(self.current_step / self.total_steps * 50)
return (
f"{self.prefix} [{'>' * progress + '.' * (50 - progress)}] "
f"{self.current_step}/{self.total_steps}"
)

def update_progress(self, step: int) -> None:
"""
Update the progress bar and print the new status.

:param step: The current step to update.
"""
self.current_step = step
print(self, end="\r")

# Example usage:

def function_from_issue_12873():
progress_logger = ProgressLogger(100)
for i in range(100):
# Some long-running task...
progress_logger.update_progress(i + 1)

def function_from_issue_12836():
progress_logger = ProgressLogger(500)
for i in range(500):
# Some long-running task...
progress_logger.update_progress(i + 1)

def function_from_issue_12785():
progress_logger = ProgressLogger(1000)
for i in range(1000):
# Some long-running task...
progress_logger.update_progress(i + 1)

def function_from_issue_12723():
progress_logger = ProgressLogger(5000)
for i in range(5000):
# Some long-running task...
progress_logger.update_progress(i + 1)

def function_from_issue_12654():
progress_logger = ProgressLogger(10000)
for i in range(10000):
# Some long-running task...
progress_logger.update_progress(i + 1)