Skip to content

Commit a37257c

Browse files
besobeso
authored andcommitted
Implement Dynamic Progress Logger
Implements koii-network#12723 Implements koii-network#12654 # Implement Dynamic Progress Logger ## Task Write a function to log output dynamically updating a progress bar. ## Acceptance Criteria All tests must pass. ## Summary of Changes Added a new utility function that creates a dynamically updating progress bar for logging output. The function allows real-time tracking of progress with customizable parameters. ## Test Cases - Verify progress bar initializes correctly with given parameters - Confirm progress bar updates incrementally - Check that progress bar handles edge cases like 0% and 100% - Ensure progress bar displays correctly with different total steps - Test progress bar works with various output types and lengths This PR was created automatically by a Koii Network AI Agent powered by Together.ai.
1 parent c693c97 commit a37257c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

progress_logger.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class ProgressLogger:
2+
"""
3+
A class to log output dynamically updating a progress bar.
4+
5+
Attributes:
6+
total_steps (int): The total number of steps to track.
7+
current_step (int): The current step being logged.
8+
progress_bar (str): The progress bar string.
9+
"""
10+
11+
def __init__(self, total_steps: int, prefix: str = ""):
12+
"""
13+
Initialize the ProgressLogger instance.
14+
15+
Args:
16+
total_steps (int): The total number of steps to track.
17+
prefix (str, optional): The prefix message for the log. Defaults to "".
18+
"""
19+
self.total_steps = total_steps
20+
self.current_step = 0
21+
self.progress_bar = "[====> ]"
22+
self.prefix = prefix
23+
24+
def logging(self, current_step: int) -> None:
25+
"""
26+
Log the current step with a dynamically updating progress bar.
27+
28+
Args:
29+
current_step (int): The current step to log.
30+
31+
Raises:
32+
ValueError: If the current_step is less than 0 or greater than total_steps.
33+
"""
34+
if current_step < 0 or current_step > self.total_steps:
35+
raise ValueError("Current step must be between 0 and total steps.")
36+
37+
self.current_step = current_step
38+
percentage = (current_step / self.total_steps) * 100
39+
filled_length = int(round(percentage / 100 * len(self.progress_bar) - 1))
40+
self.progress_bar = (
41+
f"[{'=' * filled_length}>{' ' * (len(self.progress_bar) - filled_length - 1)}]"
42+
)
43+
44+
log_message = f"{self.prefix} - Step {current_step}/{self.total_steps} ({percentage:.2f}%): {self.progress_bar}"
45+
print(log_message, end="\r")

0 commit comments

Comments
 (0)