Python 终端进度条评测 包含一个作者自定义的轻量案例源码

Python 终端进度条对比评测

  • 效果展示
    • TPDM
      • 代码部分
      • 输出包体 129M
    • RICH
      • 代码部分
      • 输出包体 68M
    • 当前案例
      • 代码部分
      • 包体大小
  • 总结
    • 你可以根据你的实际情况来做出选择

效果展示

TPDM

Python 终端进度条评测 包含一个作者自定义的轻量案例源码_第1张图片

可以看到效果是蛮好的
其次 也有一些效果版对其进行了自定义扩展 整体是蛮不错的
就是包体有点大

代码部分

import time
from tqdm import tqdm
for i in tqdm(range(100)):
    time.sleep(0.05)

输出包体 129M

Python 终端进度条评测 包含一个作者自定义的轻量案例源码_第2张图片

RICH

Python 终端进度条评测 包含一个作者自定义的轻量案例源码_第3张图片

是目前python里进度条最好看的一种 能支持彩色进度条
Python 终端进度条评测 包含一个作者自定义的轻量案例源码_第4张图片

代码部分

from rich.progress import Progress
from rich.progress import (
    BarColumn,
    DownloadColumn,
    Progress,
    SpinnerColumn,
    TaskProgressColumn,
    TimeElapsedColumn,
    TimeRemainingColumn,
)

progress = Progress(
    SpinnerColumn(),
    "{task.description}",
    BarColumn(),
    DownloadColumn(),
    TaskProgressColumn(),
    TimeElapsedColumn(),
    TimeRemainingColumn(),
)

with progress:
    with progress.open("test.exe", "rb", description="Extracting...") as file:
        pass

输出包体 68M

Python 终端进度条评测 包含一个作者自定义的轻量案例源码_第5张图片

当前案例

Python 终端进度条评测 包含一个作者自定义的轻量案例源码_第6张图片

代码部分

from time import sleep
from time import time
import sys


class ProgressBar(object):
    __pre_tick_time = 0    
    def __init__(self, total, desc="downloading", width=40,
                 output=sys.stderr):
        self.total = total
        self.width = width
        self.symbol = '⬜'
        self.output = output
        le = len(str(total))
        self.fmt = desc + " " + f'%(current){le}d/%(total){le}d (%(percent)3d%%) %(awt)s %(bar)s'
        self.current = 0
        self.__pre_tick_time = time()
        self.ewt = 1

    def __call__(self):
        percent = self.current / float(self.total)
        size = int(self.width * percent)
        awtime = f"{int(self.ewt)}s" if self.ewt < 100 else f"{format(float(self.ewt/60),'.1f')}m"
        bar = '|' + self.symbol * \
            size + '⬛' * (self.width - size) + '|'
        args = {
            'total': self.total,
            'bar': bar,
            'current': self.current,
            'percent': percent * 100,
            'awt': awtime
        }
        print('\r' + self.fmt % args, file=self.output, end='')


    def setProgress(self, p ):
        new_progress = min(p,self.total)
        now = time()
        interval = max(1/60.0,now - self.__pre_tick_time)
        self.__pre_tick_time = now
        advance_sum = abs(new_progress-self.current)
        if advance_sum > 0:
            growPerSeconds = advance_sum / interval
            surplus = self.total - self.current 
            self.ewt = int(surplus / growPerSeconds)
        else:
            self.ewt = 0
        self.current = new_progress
        self()

    def advance(self, step):
        self.setProgress(self.current + step)

    def done(self):
        self.current = self.total
        self()
        print('', file=self.output)

progress = ProgressBar(65563)
for x in range(progress.total):
    progress.advance(60)
    sleep(0.1)
progress.done()

包体大小

Python 终端进度条评测 包含一个作者自定义的轻量案例源码_第7张图片

总结

是否支持彩色 美观评分 ( 0 ~ 100 ) 空包大小
tpdm 60 129M
rich 99 68M
本案例 61 6M

你可以根据你的实际情况来做出选择

举个栗子: 比如我 就只是想要一个比较美观的效果 同时要控制包体越小越好 那我就会选择案例中自定义的ProgressBar

如果你希望你的项目展现的效果非常牛掰同时你又不需用考虑包体大小 那我推荐选择 rich库

你可能感兴趣的:(Python,python,开发语言)