<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          讓你的程序炫起來(lái)!少有人知道但超酷的 Python 進(jìn)度條開(kāi)源庫(kù)

          共 14271字,需瀏覽 29分鐘

           ·

          2021-04-10 11:54

          本文適合有 Python 基礎(chǔ)的朋友

          不知你是否有過(guò)這樣的經(jīng)歷:你寫(xiě)了一個(gè)程序,每次運(yùn)行都會(huì)耗費(fèi)很長(zhǎng)時(shí)間。在等待程序運(yùn)行期間你一次次的按下回車防止程序卡死。亦或者你的任務(wù)需要實(shí)時(shí)掌握程序運(yùn)行進(jìn)度但你根本不知道程序執(zhí)行到了哪里...

          現(xiàn)在,alive-progress 來(lái)了,它是一個(gè) Python 下的進(jìn)度條庫(kù),不僅使用方便而且支持多種炫酷顯示效果!讓我們先來(lái)看看示例效果:

          下面讓我們一起玩轉(zhuǎn)這個(gè)庫(kù)!

          一、安裝

          在 Python 下使用 pip 進(jìn)行安裝:

          pip install alive-progress

          二、快速入門(mén)

          2.1 直接使用

          在循環(huán)中使用 alive-progress 是最常見(jiàn)的用法,腳本可以這樣寫(xiě):

          # 導(dǎo)入 alive-progress 庫(kù)
          from alive_progress import alive_bar
          import time

          # 使用 with 語(yǔ)句創(chuàng)建一個(gè)進(jìn)度條
          with alive_bar(100as bar: # 給 alive_bar 傳入進(jìn)度條總數(shù)目(這里是 100)
              for item in range(100):
                  # 等待 1s
                  time.sleep(.1)
                  #更新進(jìn)度條,進(jìn)度 +1
                  bar()

          請(qǐng)注意,如果無(wú)法正常顯示動(dòng)畫(huà)則嘗試在 alive_bar 中加上 force_tty=True 參數(shù)。

          運(yùn)行以上代碼我們可以看到在終端中出現(xiàn)了一個(gè)還算華麗的動(dòng)態(tài)進(jìn)度條:

          需要注意的是 alive-progress 并不像 tqdm 等進(jìn)度條庫(kù)一樣會(huì)自動(dòng)更新,只有我們程序調(diào)用了 bar 才會(huì)讓進(jìn)度條 +1.

          當(dāng)然,我們也可以不給進(jìn)度條傳入總數(shù)目這個(gè)參數(shù),此時(shí)進(jìn)度條將不顯示進(jìn)度,并進(jìn)入未定義模式:

          有時(shí)候我們想直接操縱顯示的位置,這時(shí)候可以設(shè)定 alive_barmanual 參數(shù)為 True

          from alive_progress import alive_bar
          import time

          total = 100
          with alive_bar(total, manual=Trueas bar: # total 可以不指定,這時(shí)候只有百分比
              bar(0.5# 進(jìn)度到 50%
              time.sleep(0.5)
              bar(0.1# 進(jìn)度到 10% 
              time.sleep(0.5)
              bar(0.75# 進(jìn)度到 75%
              time.sleep(0.5)
              bar(1.0# 進(jìn)度到 100%
              time.sleep(0.5)
              bar(10# 進(jìn)度到 1000%
              for i in range(1,101):
                  bar(i/100# 設(shè)定進(jìn)度為 i%
                  time.sleep(0.05)

          當(dāng)然,在運(yùn)行過(guò)程中我們也需要輸出一些提示信息,直接使用 print 可以在不破壞進(jìn)度條的情況下輸出一行提示信息,text 方法則可以在進(jìn)度條尾部添加后綴字符,而 title 參數(shù)則可以給進(jìn)度條添加標(biāo)題(前綴信息),具體使用方法及效果如下:

          from alive_progress import alive_bar
          import time

          # 定義標(biāo)題(前綴字符)為 HelloGitHub
          with alive_bar(10, title="HelloGitHub"as bar:
              for i in range(10):
                  time.sleep(1)

                  bar()   # 讓進(jìn)度 +1
                  bar.text("Processing Work #%d"%(i+1))   # 更新進(jìn)度條后綴

                  print("Work #%d finished"%i)        # 輸出一行信息

          2.2 添點(diǎn)花樣

          看多了傳統(tǒng)的進(jìn)度條樣式想換換花樣?沒(méi)問(wèn)題,alive-progress 不僅內(nèi)置了多種進(jìn)度條樣式,還支持自定義格式。

          進(jìn)度條可以自定義的樣式分為兩種:barspinner,只需要在調(diào)用 alive_bar 的時(shí)候傳入對(duì)應(yīng)的參數(shù)即可。

          以這個(gè)進(jìn)度條為例,中間最長(zhǎng)的是 bar,旁邊來(lái)回晃動(dòng)的 www.HelloGitHub.comspinner

          alive-progress 內(nèi)置了多種 bar 和 spinner 樣式,只需要調(diào)用 show_bars 或者 show_spinners 即可快速預(yù)覽相應(yīng)的樣式,例如:

          from alive_progress import show_bars

          show_bars() # 查看內(nèi)置 bar 樣式
          from alive_progress import show_spinners

          show_spinners() # 查看內(nèi)置 spinner 樣式

          默認(rèn)樣式使用起來(lái)非常簡(jiǎn)單,例如我想使用 bubbles 這個(gè) bar 和 message_scrolling 這個(gè) spinner,直接傳入對(duì)應(yīng)名稱即可:

          from alive_progress import alive_bar
          import time

          # 直接傳入對(duì)應(yīng)名字即可
          with alive_bar(
                      100,
                      title="HelloGitHub"
                      bar="bubbles", spinner="message_scrolling"
                      ) as bar:

              for i in range(100):
                  time.sleep(.1)
                  bar()

          如果不知道 total 的數(shù)目,可以使用 unknown 參數(shù)(這時(shí)候?qū)⑻鎿Q bar 為 spinner):

          from alive_progress import alive_bar
          import time

          with alive_bar(
                      title="HelloGitHub"
                      # 注意:這里 bar 被換成了unknow,內(nèi)置樣式名稱與 spinner 的相同
                      unknown="stars", spinner="message_scrolling"
                      ) as bar:

              for i in range(100):
                  time.sleep(.1)
                  bar()

          三、私人定制

          或許比起直接使用內(nèi)置模板你更喜歡自己定制的進(jìn)度條,對(duì)此 alive-progress 也提供了對(duì)應(yīng)方法。

          3.1 定制 bar

          使用 standard_bar_factory 方法可以快速定制 bar,bar 可以設(shè)置的參數(shù)有五個(gè):

          • chars:正在執(zhí)行單元的動(dòng)畫(huà),按照進(jìn)度依次顯示。
          • borders:進(jìn)度條邊界,顯示在左右兩邊。
          • background:未執(zhí)行到單元顯示的內(nèi)容。
          • tip:執(zhí)行單元的前導(dǎo)符號(hào)。
          • errors:出錯(cuò)時(shí)(進(jìn)度未走全,超出 total 值等)時(shí)顯示的字符。

          例如我們想做一個(gè)如圖所示的 bar:

          則可以這樣來(lái)寫(xiě):

          from alive_progress import alive_bar, standard_bar_factory
          import time

          ##-------自定義 bar-------##
          my_bar = standard_bar_factory( # 以下參數(shù)均有默認(rèn)值,不必一次全部修改
                                      chars="123456789#"# 加載時(shí)根據(jù)進(jìn)度依次顯示,長(zhǎng)度任意
                                      borders="<>",  # bar 兩頭的邊界
                                      background=".",  # 未加載部分用 "." 填充
                                      tip=">",   # 指示進(jìn)度方向的引導(dǎo)符號(hào)(分割 "#" 與 ".")
                                      errors="??" # 發(fā)生錯(cuò)誤時(shí)顯示的內(nèi)容(未完成,溢出) 
                                      )
          ##-------自定義結(jié)束-------##

          ##--------動(dòng)畫(huà)演示-------##
          with alive_bar(
                      10,
                      title="HelloGitHub"
                      bar=my_bar, # 這里傳入剛剛自定義的 bar
                spinner="message_scrolling",
                      manual=True
                      ) as bar:

              for i in range(50):
                  time.sleep(.1)
                  bar(i/100)
              bar(.5)
              time.sleep(2)
              bar(10)
              print("上溢")
              time.sleep(1)
              bar(1)
              print("100% 完成")
              time.sleep(1)
              bar(.1)
              print("未完成")

          3.2 定制 spinner

          對(duì)于 spinner,alive-progress 提供了更多種的動(dòng)畫(huà)定義方式:

          frame_spinner_factory:將傳入的字符串挨個(gè)輸出:

          from alive_progress import alive_bar, frame_spinner_factory
          import time

          my_spinner = my_spinner = frame_spinner_factory(
                                          r'-----',
                                          r'1----',
                                          r'-2---',
                                          r'--3--',
                                          r'---4-',
                                          r'----5'
                                          ) # 直接傳入字符串

          with alive_bar(
                      title="HelloGitHub",
                      spinner=my_spinner
                      ) as bar:

              while True:
                  bar()
                  time.sleep(.1)

          可以看到字符串挨個(gè)循環(huán)輸出。

          scrolling_spinner_factory:將字符串滾動(dòng)播出

          from alive_progress import alive_bar, scrolling_spinner_factory
          import time

          my_spinner = scrolling_spinner_factory(
                                              chars="HelloGitHub"# 想要播放的字符串
                                              length=15# spinner 區(qū)域?qū)挾?/span>
                                              blank='.' # 空白部分填充字符
                                              )

          with alive_bar(
                      title="HelloGitHub",
                      spinner=my_spinner
                      ) as bar:

              while True:
                  bar()
                  time.sleep(.1)

          bouncing_spinner_factory:將兩個(gè)字符串交替滾動(dòng)播出

          from alive_progress import alive_bar, bouncing_spinner_factory
          import time

          my_spinner = bouncing_spinner_factory(
                                              right_chars="I love"# 從左邊進(jìn)入的字符串
                                              length=15# spinner 區(qū)域長(zhǎng)度
                                              left_chars="HelloGitHub"# 從右邊進(jìn)入的字符串
                                              blank='.',  # 空白區(qū)域填充字符
                                              )

          with alive_bar(
                      title="HelloGitHub",
                      spinner=my_spinner
                      ) as bar:

              while True:
                  bar()
                  time.sleep(.1)

          當(dāng)然,也可以省略 left_chars 這個(gè)參數(shù),其效果相當(dāng)于 I love 將會(huì)像彈球一樣左右彈動(dòng)。

          unknown_bar_factory:將 spinner 轉(zhuǎn)換為能使用在未定義模式中的格式:

          from alive_progress import alive_bar, unknown_bar_factory, bouncing_spinner_factory
          import time

          my_spinner = bouncing_spinner_factory("www.HelloGitHub.com",15,hiding=False)

          my_unknown_bar = unknown_bar_factory(my_spinner) # 傳入定義的 spinner
          with alive_bar(
                      title="HelloGitHub",
                      unknown=my_unknown_bar
                      ) as bar:

              while True:
                  bar()
                  time.sleep(.1)

          四、結(jié)尾

          到這里,相信你已經(jīng)掌握了 alive_progress 的基本玩法,alive-progress 還提供了一些在不同場(chǎng)合所需的特殊功能,有興趣的朋友可以通過(guò)閱讀官方文檔或源代碼進(jìn)行更加深入的了解。

          - END -
          ??「點(diǎn)擊關(guān)注」更多驚喜等待你的發(fā)現(xiàn)??
          瀏覽 83
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产鸡巴操逼视频 | 少妇太爽一区二区三区 | 日本乱伦一区二区三区 | 天堂一区在线观看 | 翔田千里在线激情视频 |