Skip to content
Open
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions src/progress_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
import time

def dynamic_progress_logger(iterable, description='Progress', bar_length=50):
"""
A generator function that logs progress dynamically using a progress bar.

Args:
iterable (iterable): The input iterable to track progress for
description (str, optional): Description of the progress. Defaults to 'Progress'.
bar_length (int, optional): Length of the progress bar. Defaults to 50.

Yields:
Items from the input iterable
"""
total = len(iterable)

for i, item in enumerate(iterable, 1):
# Calculate percentage and progress
percent = float(i) / total
filled_length = int(bar_length * percent)

# Create progress bar
bar = '=' * filled_length + '-' * (bar_length - filled_length)

# Print progress bar
sys.stdout.write(f'\r{description}: [{bar}] {int(percent * 100)}% Complete')
sys.stdout.flush()

yield item

# Add a newline after completion
sys.stdout.write('\n')
62 changes: 62 additions & 0 deletions tests/test_progress_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import sys
import io
import pytest
from src.progress_logger import dynamic_progress_logger

def test_dynamic_progress_logger():
# Capture stdout
captured_output = io.StringIO()
sys.stdout = captured_output

# Test with a simple list
test_list = list(range(10))
result = list(dynamic_progress_logger(test_list, description='Test'))

# Restore stdout
sys.stdout = sys.__stdout__

# Check if the output contains expected progress indicators
output = captured_output.getvalue()
assert 'Test: [' in output
assert ']' in output
assert '%' in output

# Verify the result is the same as the input list
assert result == test_list

def test_dynamic_progress_logger_empty_list():
# Capture stdout
captured_output = io.StringIO()
sys.stdout = captured_output

# Test with an empty list
test_list = []
result = list(dynamic_progress_logger(test_list, description='Empty'))

# Restore stdout
sys.stdout = sys.__stdout__

# Empty list should not produce any output
output = captured_output.getvalue()
assert output == ''
assert result == []

def test_dynamic_progress_logger_custom_bar_length():
# Capture stdout
captured_output = io.StringIO()
sys.stdout = captured_output

# Test with a custom bar length
test_list = list(range(5))
result = list(dynamic_progress_logger(test_list, description='Custom', bar_length=20))

# Restore stdout
sys.stdout = sys.__stdout__

# Check if the output contains expected progress indicators
output = captured_output.getvalue()
assert 'Custom: [' in output
assert len(output.split('[')[1].split(']')[0]) <= 20

# Verify the result is the same as the input list
assert result == test_list