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
| import time from datetime import datetime, timedelta class ProgressMonitor: def __init__(self, total_count): self.total_count = total_count self.start_time = time.time() self.count = 0 def format_time(self, seconds): return str(timedelta(seconds=int(seconds))) def format_datetime(self, timestamp): return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S") def step(self, message=""): self.count += 1 current_time = time.time() elapsed_time = current_time - self.start_time if self.count > 0: time_per_item = elapsed_time / self.count remaining_items = self.total_count - self.count estimated_time_left = time_per_item * remaining_items estimated_completion_time = current_time + estimated_time_left progress = (self.count / self.total_count) * 100 status = ( f"Progress: {self.count}/{self.total_count} ({progress:.2f}%)\n" f"Current time: {self.format_datetime(current_time)}\n" f"Elapsed time: {self.format_time(elapsed_time)}\n" f"Estimated time remaining: {self.format_time(estimated_time_left)}\n" f"Estimated completion time: {self.format_datetime(estimated_completion_time)}\n" ) if message: status += f"Message: {message}\n" print(status) else: print("Started monitoring progress.") def finish(self): end_time = time.time() total_time = end_time - self.start_time print(f"\nTask completed in {self.format_time(total_time)}")
|