Spaces:
Runtime error
Runtime error
File size: 1,890 Bytes
1521ef5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | """
Progress tracking utility functions.
Provides common functionality for tracking and displaying progress.
"""
def get_completion_percentage(current, total):
"""
Calculate percentage completion.
Args:
current: Current progress value
total: Total progress value
Returns:
Integer percentage value between 0 and 100
"""
if total == 0:
return 100
return min(int((current / total) * 100), 100)
class ProgressTracker:
"""
A class for tracking and reporting progress of operations.
"""
def __init__(self, callback=None):
"""
Initialize a new progress tracker.
Args:
callback: A function to call with progress messages
"""
self.callback = callback
self.state = {
"current": 0,
"total": 0,
"stage": "initializing"
}
def update(self, message, current=None, total=None, stage=None):
"""
Update the progress tracker state and send a message.
Args:
message: The message to send
current: The current progress value (optional)
total: The total progress value (optional)
stage: The current stage of progress (optional)
"""
if current is not None:
self.state["current"] = current
if total is not None:
self.state["total"] = total
if stage is not None:
self.state["stage"] = stage
if self.callback:
self.callback(message)
def get_percentage(self):
"""
Get the current completion percentage.
Returns:
Integer percentage value between 0 and 100
"""
return get_completion_percentage(self.state["current"], self.state["total"]) |