(self, num_tasks=1)
| 48 | self.timer = Timer() |
| 49 | |
| 50 | def update(self, num_tasks=1): |
| 51 | assert num_tasks > 0 |
| 52 | self.completed += num_tasks |
| 53 | elapsed = self.timer.since_start() |
| 54 | if elapsed > 0: |
| 55 | fps = self.completed / elapsed |
| 56 | else: |
| 57 | fps = float('inf') |
| 58 | if self.task_num > 0: |
| 59 | percentage = self.completed / float(self.task_num) |
| 60 | eta = int(elapsed * (1 - percentage) / percentage + 0.5) |
| 61 | msg = f'\r[{{}}] {self.completed}/{self.task_num}, ' \ |
| 62 | f'{fps:.1f} task/s, elapsed: {int(elapsed + 0.5)}s, ' \ |
| 63 | f'ETA: {eta:5}s' |
| 64 | |
| 65 | bar_width = min(self.bar_width, |
| 66 | int(self.terminal_width - len(msg)) + 2, |
| 67 | int(self.terminal_width * 0.6)) |
| 68 | bar_width = max(2, bar_width) |
| 69 | mark_width = int(bar_width * percentage) |
| 70 | bar_chars = '>' * mark_width + ' ' * (bar_width - mark_width) |
| 71 | self.file.write(msg.format(bar_chars)) |
| 72 | else: |
| 73 | self.file.write( |
| 74 | f'completed: {self.completed}, elapsed: {int(elapsed + 0.5)}s,' |
| 75 | f' {fps:.1f} tasks/s') |
| 76 | self.file.flush() |
| 77 | |
| 78 | |
| 79 | def track_progress(func, tasks, bar_width=50, file=sys.stdout, **kwargs): |
no test coverage detected