Custom Progress Tracking
Designing Custom Progress Trackers
graph TD
A[Custom Progress Tracking] --> B[Manual Implementation]
A --> C[Decorator Approach]
A --> D[Class-Based Solutions]
Manual Progress Tracking Implementation
Basic Custom Progress Tracker
import sys
import time
def custom_progress_bar(current, total, bar_length=50):
fraction = current / total
arrow = int(fraction * bar_length - 1) * '=' + '>'
padding = (bar_length - len(arrow)) * ' '
ending = '\n' if current == total else '\r'
print(f'Progress: [{arrow}{padding}] {int(fraction*100)}%', end=ending)
sys.stdout.flush()
def process_items(items):
total = len(items)
for index, item in enumerate(items, 1):
## Simulate processing
time.sleep(0.1)
custom_progress_bar(index, total)
Decorator-Based Progress Tracking
Progress Tracking Decorator
import time
from functools import wraps
def track_progress(func):
@wraps(func)
def wrapper(items):
total = len(items)
for index, item in enumerate(items, 1):
result = func(item)
percentage = (index / total) * 100
print(f"Progress: {percentage:.2f}% ({index}/{total})")
return result
return wrapper
@track_progress
def process_item(item):
time.sleep(0.1)
return item
Class-Based Progress Tracking
Advanced Progress Tracker
class ProgressTracker:
def __init__(self, total_items):
self.total_items = total_items
self.current_item = 0
self.start_time = time.time()
def update(self, processed_item):
self.current_item += 1
elapsed_time = time.time() - self.start_time
percentage = (self.current_item / self.total_items) * 100
estimated_total_time = elapsed_time / (self.current_item / self.total_items)
remaining_time = estimated_total_time - elapsed_time
print(f"""
Progress: {percentage:.2f}%
Processed: {self.current_item}/{self.total_items}
Elapsed Time: {elapsed_time:.2f}s
Estimated Remaining: {remaining_time:.2f}s
""")
Progress Tracking Strategies
Strategy |
Complexity |
Use Case |
Performance |
Manual Implementation |
Low |
Simple Projects |
High |
Decorator Approach |
Medium |
Functional Programming |
Medium |
Class-Based Solution |
High |
Complex Workflows |
Low |
Advanced Considerations
Key Design Principles
- Minimal Performance Overhead
- Clear and Informative Output
- Flexibility and Customization
- Error Handling
LabEx Recommended Approach
LabEx suggests developing a flexible progress tracking solution that can be easily adapted to different project requirements while maintaining clean, readable code.
Complete Custom Progress Tracking Example
def advanced_progress_tracking(items, batch_size=10):
tracker = ProgressTracker(len(items))
for batch in [items[i:i+batch_size] for i in range(0, len(items), batch_size)]:
for item in batch:
## Process item
time.sleep(0.1)
tracker.update(item)
Custom progress tracking allows developers to create tailored solutions that precisely meet their specific project needs.