[蟒蛇菜谱] Python方便使用的级联进度信息

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
68
69
70
71
class StepedProgress:
    '''方便显示进度的级联进度信息。
    '''
    def __init__(self, stockPercent=[1], parentProgress=None):
        self.percent = 0
        self.info = ''
        self.subProgress = []
        self.cur_running_process = 0
        self.stockPercent = stockPercent
        self.parentProgress = parentProgress
 
        # 重新计算进度比,防止初始化时的值加起来不是1
        w = 0.0
        for p in self.stockPercent:
            w += p
        for i in range(0, len(stockPercent)):
            stockPercent[i] = stockPercent[i]/w
 
        # 初始化子进度
        if len(stockPercent) == 1:
            self.subProgress = None
        else:
            for p in self.stockPercent:
                self.subProgress.append(StepedProgress(parentProgress=self))
 
    def subprogress(self, index):
        if index >= self.subcount():
            return self.subProgress[self.subcount()-1]
        elif index < self.cur_running_process:
            return self.subProgress[self.cur_running_process]
        else:
            self.cur_running_process = index
            return self.subProgress[index]
 
    def subcount(self):
        return len(self.subProgress)
 
    def notifyParentProgress(self, percent, info=None):
        new_percent = 0.0
        for i in range(0, self.cur_running_process):
            new_percent += self.stockPercent[i]
        new_percent += percent/100.0 * self.stockPercent[self.cur_running_process]
        new_percent *= 100.0
        self.notifyProgress(new_percent, info)
 
    def notifyProgress(self, percent, info=None):
        if percent > self.percent:
            self.percent = percent
        if info is not None:
            self.info = info
        if self.parentProgress is not None:
            self.parentProgress.notifyParentProgress(percent, info)
        else:
            print self.info[:77].ljust(80, '.'), "[%0.1f%%]"%self.percent
 
if __name__ == "__main__":
    s = StepedProgress([60, 40])
    s.notifyProgress(10, 'aaa')
 
    s1 = s.subprogress(0)
    s1.notifyProgress(50, 'bbb')
 
    s3 = s.subprogress(1)
    s3 = StepedProgress([1, 1], parentProgress=s3.parentProgress) #级联子进度
    s3.notifyProgress(20, 'ddd')
 
    s4 = s3.subprogress(0)
    s4.notifyProgress(50, 'eee')
 
    s5 = s3.subprogress(1)
    s5.notifyProgress(50, 'fff')

 

输出结果:

aaa............................................................................. [10.0%]
bbb............................................................................. [30.0%]
ddd............................................................................. [68.0%]
eee............................................................................. [70.0%]
fff............................................................................. [90.0%]

posted @   鸪斑兔  阅读(271)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示