Track the progress of tasks execution with a progress bar. Tasks are done with a simple for-loop. Args: func (callable): The function to be applied to each task. tasks (list or tuple[Iterable, int]): A list of tasks or (tasks, total num). bar_width (int):
(func, tasks, bar_width=50, file=sys.stdout, **kwargs)
| 77 | |
| 78 | |
| 79 | def track_progress(func, tasks, bar_width=50, file=sys.stdout, **kwargs): |
| 80 | """Track the progress of tasks execution with a progress bar. |
| 81 | Tasks are done with a simple for-loop. |
| 82 | Args: |
| 83 | func (callable): The function to be applied to each task. |
| 84 | tasks (list or tuple[Iterable, int]): A list of tasks or |
| 85 | (tasks, total num). |
| 86 | bar_width (int): Width of progress bar. |
| 87 | Returns: |
| 88 | list: The task results. |
| 89 | """ |
| 90 | if isinstance(tasks, tuple): |
| 91 | assert len(tasks) == 2 |
| 92 | assert isinstance(tasks[0], Iterable) |
| 93 | assert isinstance(tasks[1], int) |
| 94 | task_num = tasks[1] |
| 95 | tasks = tasks[0] |
| 96 | elif isinstance(tasks, Iterable): |
| 97 | task_num = len(tasks) |
| 98 | else: |
| 99 | raise TypeError( |
| 100 | '"tasks" must be an iterable object or a (iterator, int) tuple') |
| 101 | prog_bar = ProgressBar(task_num, bar_width, file=file) |
| 102 | results = [] |
| 103 | for task in tasks: |
| 104 | results.append(func(task, **kwargs)) |
| 105 | prog_bar.update() |
| 106 | prog_bar.file.write('\n') |
| 107 | return results |
no test coverage detected
searching dependent graphs…