Skip to content

Commit c6272f2

Browse files
besobeso
authored andcommitted
Implement Dynamic Progress Logger
Implements koii-network#12873 Implements koii-network#12836 Implements koii-network#12785 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. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai.
1 parent c693c97 commit c6272f2

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

progress_logger.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
3+
class ProgressLogger:
4+
"""A utility class to log output dynamically with a progress bar."""
5+
6+
def __init__(self, total_steps: int, width: int = 50, fill: str = "=", empty: str = " ", prefix: str = "[", suffix: str = "]") -> None:
7+
"""
8+
Initialize the ProgressLogger instance.
9+
10+
:param total_steps: The total number of steps to track.
11+
:param width: The width of the progress bar. Default is 50.
12+
:param fill: The character to use for the filled part of the progress bar. Default is "=".
13+
:param empty: The character to use for the empty part of the progress bar. Default is " ".
14+
:param prefix: The character to use as the prefix of the progress bar. Default is "[".
15+
:param suffix: The character to use as the suffix of the progress bar. Default is "]".
16+
"""
17+
self.total_steps = total_steps
18+
self.width = width
19+
self.fill = fill
20+
self.empty = empty
21+
self.prefix = prefix
22+
self.suffix = suffix
23+
self.current_step = 0
24+
self.print_progress_bar()
25+
26+
def update(self, step: int) -> None:
27+
"""
28+
Update the progress bar and print the new status.
29+
30+
:param step: The current step number.
31+
"""
32+
self.current_step = step
33+
self.print_progress_bar()
34+
35+
def print_progress_bar(self) -> None:
36+
"""Print the progress bar with the current progress."""
37+
percentage = (self.current_step / self.total_steps) * 100
38+
filled_length = int(self.width * (percentage / 100))
39+
empty_length = self.width - filled_length
40+
41+
progress_bar = self.prefix + self.fill * filled_length + self.empty * empty_length + self.suffix
42+
sys.stdout.write("\r{:3.0f}% {}".format(percentage, progress_bar))
43+
sys.stdout.flush()
44+
45+
if self.current_step == self.total_steps:
46+
print() # Move to a new line when finished

0 commit comments

Comments
 (0)