網站首頁 編程語言 正文
前言:
在下載某些文件的時候你一定會不時盯著進度條,在寫代碼的時候使用進度條可以便捷的觀察任務處理情況。
除了使用 print 來打印之外,今天本文我來給大家介紹幾種酷炫的進度條的方式。
1、自定義ProgressBar
最原始的辦法就是不借助任何第三方工具,自己寫一個進度條函數,使用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的一種默認輸出格式,而sys.stdout.write()
可以不換行打印,sys.stdout.flush()
可以立即刷新輸出的內容。當然也可以封裝成類來更好的使用,但效果是類似的。
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
即可,使用也很簡單,幾行代碼即可實現上面的進度條
from tqdm import trange import time for i in trange(10):? ? ? time.sleep(1)
當然tqdm作為老牌的Python進度條工具,循環處理、多進程、多線程、遞歸處理等都是支持的,你可以在官方GitHub上學習 、解鎖更多的玩法。
3、Rich
上面兩種實現Python進度條的方法都學會了嗎,雖然簡單但是看上去并不漂亮,顏色也比較單調。所以最后壓軸出場的就是一款比較小眾的第三方庫Rich 。Rich主要是用于在終端中打印豐富多彩的文本(最高支持1670萬色)
所以當然可以使用Rich打印進度條,顯示完成百分比,剩余時間,數據傳輸速度等都可以。并且樣式更加酷炫,并且它是高度可配置的,因此我們可以對其進行自定義以顯示所需的任何信息。使用也很簡單,比如我們使用Rich來實現一個最簡單的進度條
from rich.progress import track import ?time for step in track(range(30)): ? ? time.sleep(0.5)
同時Rich支持多個進度條,這在多任務情況下監控的進度很有用
原文鏈接:https://blog.csdn.net/weixin_38037405/article/details/124031504
相關推薦
- 2022-05-20 python使用數字與字符串方法技巧_python
- 2022-06-06 python可視化數據分析pyecharts初步嘗試_python
- 2023-02-12 JetpackCompose?Scaffold組件使用教程_Android
- 2022-11-16 C語言數據結構不掛科指南之線性表詳解_C 語言
- 2022-04-25 C#使用NPOI實現Excel和DataTable的互轉_C#教程
- 2022-05-18 C++?qt實現打開關閉狀態按鈕的代碼_C 語言
- 2022-10-06 Iptables防火墻connlimit與time模塊擴展匹配規則_安全相關
- 2022-08-31 C++?OpenCV裁剪圖片時發生報錯的解決方式_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支