欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

3種Python 實現(xiàn)酷炫進度條的實用方法

 更新時間:2022年04月11日 18:26:19   作者:Python學(xué)習(xí)與數(shù)據(jù)挖掘  
這篇文章主要介紹了3種Python 實現(xiàn)酷炫進度條的實用方法,文章圍繞Python的相關(guān)資料展開對實現(xiàn)進度條的介紹,需要的小伙伴可以參考一下

前言:

在下載某些文件的時候你一定會不時盯著進度條,在寫代碼的時候使用進度條可以便捷的觀察任務(wù)處理情況。

除了使用 print 來打印之外,今天本文我來給大家介紹幾種酷炫的進度條的方式。

1、自定義ProgressBar

最原始的辦法就是不借助任何第三方工具,自己寫一個進度條函數(shù),使用time模塊配合sys模塊即可

import sys
import time

def progressbar(it, prefix="", size=60, file=sys.stdout):
? ? count = len(it)
? ? def show(j):
? ? ? ? x = int(size*j/count)
? ? ? ? file.write("%s[%s%s] %i/%i\r" % (prefix, "#"*x, "."*(size-x), j, count))
? ? ? ? file.flush() ? ? ? ?
? ? show(0)
? ? for i, item in enumerate(it):
? ? ? ? yield item
? ? ? ? show(i+1)
? ? file.write("\n")
? ? file.flush()

? ??
for i in progressbar(range(15), "Computing: ", 40):
? ? do_something()
? ? time.sleep(0.1)

自己定義的好處就是可以將進度條定義成我們想要的形式比如上面就是使用#與·來輸出,為什么不用print?因為sys.stdout就是print的一種默認(rèn)輸出格式,而sys.stdout.write()可以不換行打印,sys.stdout.flush()可以立即刷新輸出的內(nèi)容。當(dāng)然也可以封裝成類來更好的使用,但效果是類似的。

from __future__ import print_function
import sys
import re


class ProgressBar(object):
? ? DEFAULT = 'Progress: %(bar)s %(percent)3d%%'
? ? FULL = '%(bar)s %(current)d/%(total)d (%(percent)3d%%) %(remaining)d to go'

? ? def __init__(self, total, width=40, fmt=DEFAULT, symbol='=',
? ? ? ? ? ? ? ? ?output=sys.stderr):
? ? ? ? assert len(symbol) == 1

? ? ? ? self.total = total
? ? ? ? self.width = width
? ? ? ? self.symbol = symbol
? ? ? ? self.output = output
? ? ? ? self.fmt = re.sub(r'(?P<name>%\(.+?\))d',
? ? ? ? ? ? r'\g<name>%dd' % len(str(total)), fmt)

? ? ? ? self.current = 0

? ? def __call__(self):
? ? ? ? percent = self.current / float(self.total)
? ? ? ? size = int(self.width * percent)
? ? ? ? remaining = self.total - self.current
? ? ? ? bar = '[' + self.symbol * size + ' ' * (self.width - size) + ']'

? ? ? ? args = {
? ? ? ? ? ? 'total': self.total,
? ? ? ? ? ? 'bar': bar,
? ? ? ? ? ? 'current': self.current,
? ? ? ? ? ? 'percent': percent * 100,
? ? ? ? ? ? 'remaining': remaining
? ? ? ? }
? ? ? ? print('\r' + self.fmt % args, file=self.output, end='')

? ? def done(self):
? ? ? ? self.current = self.total
? ? ? ? self()
? ? ? ? print('', file=self.output)
? ? ? ??
from time import sleep

progress = ProgressBar(80, fmt=ProgressBar.FULL)

for x in range(progress.total):
? ? progress.current += 1
? ? progress()
? ? sleep(0.1)
progress.done()

2、tqdm

之前我們說了,自定義的好處就是可以自己修改,那么使用第三方庫的好處就是可以偷懶,不用自己寫,拿來就能用。比如提到Python進度條那肯定會想到常用的tqdm,安裝很簡單pip install tqdm即可,使用也很簡單,幾行代碼即可實現(xiàn)上面的進度條

from tqdm import trange
import time
for i in trange(10):?
? ? time.sleep(1)

當(dāng)然tqdm作為老牌的Python進度條工具,循環(huán)處理、多進程、多線程、遞歸處理等都是支持的,你可以在官方GitHub上學(xué)習(xí) 、解鎖更多的玩法。

3、Rich

上面兩種實現(xiàn)Python進度條的方法都學(xué)會了嗎,雖然簡單但是看上去并不漂亮,顏色也比較單調(diào)。所以最后壓軸出場的就是一款比較小眾的第三方庫Rich 。Rich主要是用于在終端中打印豐富多彩的文本(最高支持1670萬色)

所以當(dāng)然可以使用Rich打印進度條,顯示完成百分比,剩余時間,數(shù)據(jù)傳輸速度等都可以。并且樣式更加酷炫,并且它是高度可配置的,因此我們可以對其進行自定義以顯示所需的任何信息。使用也很簡單,比如我們使用Rich來實現(xiàn)一個最簡單的進度條

from rich.progress import track
import ?time

for step in track(range(30)):
? ? time.sleep(0.5)

同時Rich支持多個進度條,這在多任務(wù)情況下監(jiān)控的進度很有用

到此這篇關(guān)于3種Python 實現(xiàn)酷炫進度條的實用方法的文章就介紹到這了,更多相關(guān)Python實現(xiàn)進度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論