mxivideo/python_core/utils/progress/generator.py

45 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""
进度生成器工具
"""
from .task import ProgressiveTask
class ProgressGenerator:
"""进度生成器工具类"""
@staticmethod
def for_iterable(iterable, task: ProgressiveTask, description: str = "处理中"):
"""为可迭代对象添加进度报告"""
total = len(iterable) if hasattr(iterable, '__len__') else 100
task.total_steps = total
for i, item in enumerate(iterable):
task.update(i, f"{description} {i+1}/{total}")
yield item
task.finish(f"{description}完成")
@staticmethod
def for_range(start: int, end: int, task: ProgressiveTask, description: str = "处理中"):
"""为范围添加进度报告"""
total = end - start
task.total_steps = total
for i in range(start, end):
task.update(i - start, f"{description} {i+1}/{total}")
yield i
task.finish(f"{description}完成")
@staticmethod
def for_steps(steps: int, task: ProgressiveTask, description: str = "处理中"):
"""为步数添加进度报告"""
task.total_steps = steps
for i in range(steps):
task.update(i, f"{description} {i+1}/{steps}")
yield i
task.finish(f"{description}完成")