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

python的Tqdm模塊的使用

 更新時(shí)間:2018年01月10日 11:29:19   作者:langb2014  
這篇文章主要介紹了python的Tqdm模塊的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Tqdm 是一個(gè)快速,可擴(kuò)展的Python進(jìn)度條,可以在 Python 長(zhǎng)循環(huán)中添加一個(gè)進(jìn)度提示信息,用戶只需要封裝任意的迭代器 tqdm(iterator)。

我的系統(tǒng)是window環(huán)境,首先安裝python,接下來(lái)就是pip。

pip安裝:

在python根目錄下創(chuàng)建一個(gè)get-pip.py的文件,內(nèi)容:

https://bootstrap.pypa.io/get-pip.py 

然后在CMD窗口進(jìn)入python下面:

輸出:

python -m pip install -U pip 

由于Tqdm要求的pip版本是9.0所以需要手動(dòng)安裝pip9.0
http://pypi.python.org/pypi/pip

下載安裝包9.0

然后解壓進(jìn)入,CMD窗口輸入:python setup.py install

然后就可以安裝Tqdm了,

pip install tqdm 

安裝最新的開(kāi)發(fā)版的話

pip install -e git+https://github.com/tqdm/tqdm.git@master#egg=tqdm 

最后看看怎么用呢?https://pypi.python.org/pypi/tqdm

基本用法:

from tqdm import tqdm 
for i in tqdm(range(10000)): 
   sleep(0.01) 

當(dāng)然除了tqdm,還有trange,使用方式完全相同

for i in trange(100): 
    sleep(0.1) 

只要傳入list都可以:

pbar = tqdm(["a", "b", "c", "d"]) 
for char in pbar: 
  pbar.set_description("Processing %s" % char) 

也可以手動(dòng)控制更新

with tqdm(total=100) as pbar: 
  for i in range(10): 
    pbar.update(10) 

也可以這樣:

pbar = tqdm(total=100) 
for i in range(10): 
  pbar.update(10) 
pbar.close() 

在Shell的tqdm用法

統(tǒng)計(jì)所有python腳本的行數(shù):

$ time find . -name '*.py' -exec cat \{} \; | wc -l 
857365 
 
real  0m3.458s 
user  0m0.274s 
sys   0m3.325s 
 
$ time find . -name '*.py' -exec cat \{} \; | tqdm | wc -l 
857366it [00:03, 246471.31it/s] 
857365 
 
real  0m3.585s 
user  0m0.862s 
sys   0m3.358s 

使用參數(shù):

$ find . -name '*.py' -exec cat \{} \; | 
  tqdm --unit loc --unit_scale --total 857366 >> /dev/null 
100%|███████████████████████████████████| 857K/857K [00:04<00:00, 246Kloc/s] 

備份一個(gè)目錄:

$ 7z a -bd -r backup.7z docs/ | grep Compressing | 
  tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log 
100%|███████████████████████████████▉| 8014/8014 [01:37<00:00, 82.29files/s] 

通過(guò)看示范的代碼,我們能發(fā)現(xiàn)使用的核心是tqdm和trange這兩個(gè)函數(shù),從代碼層面分析tqdm的功能,那首先是init.py

__all__ = ['tqdm', 'tqdm_gui', 'trange', 'tgrange', 'tqdm_pandas', 
      'tqdm_notebook', 'tnrange', 'main', 'TqdmKeyError', 'TqdmTypeError', 
      '__version__'] 

跟蹤到_tqdm.py,能看到tqdm類的聲明,首先是初始化

def __init__(self, iterable=None, desc=None, total=None, leave=True, 
         file=sys.stderr, ncols=None, mininterval=0.1, 
         maxinterval=10.0, miniters=None, ascii=None, disable=False, 
         unit='it', unit_scale=False, dynamic_ncols=False, 
         smoothing=0.3, bar_format=None, initial=0, position=None, 
         gui=False, **kwargs): 
Parameters 
 
iterable : iterable, optional 
Iterable to decorate with a progressbar. 
可迭代的進(jìn)度條。 
Leave blank to manually manage the updates. 
留空手動(dòng)管理更新?? 
desc : str, optional 
Prefix for the progressbar. 
進(jìn)度條的描述 
total : int, optional 
The number of expected iterations. If unspecified, 
len(iterable) is used if possible. As a last resort, only basic 
progress statistics are displayed (no ETA, no progressbar). 
If gui is True and this parameter needs subsequent updating, 
specify an initial arbitrary large positive integer, 
e.g. int(9e9). 
預(yù)期的迭代數(shù)目,默認(rèn)為None,則盡可能的迭代下去,如果gui設(shè)置為T(mén)rue,這里則需要后續(xù)的更新,將需要指定為一個(gè)初始隨意值較大的正整數(shù),例如int(9e9) 
leave : bool, optional 
If [default: True], keeps all traces of the progressbar 
upon termination of iteration. 
保留進(jìn)度條存在的痕跡,簡(jiǎn)單來(lái)說(shuō)就是會(huì)把進(jìn)度條的最終形態(tài)保留下來(lái),默認(rèn)為T(mén)rue 
file : io.TextIOWrapper or io.StringIO, optional 
Specifies where to output the progress messages 
[default: sys.stderr]. Uses file.write(str) and file.flush() 
methods. 
指定消息的輸出 
ncols : int, optional 
The width of the entire output message. If specified, 
dynamically resizes the progressbar to stay within this bound. 
If unspecified, attempts to use environment width. The 
fallback is a meter width of 10 and no limit for the counter and 
statistics. If 0, will not print any meter (only stats). 
整個(gè)輸出消息的寬度。如果指定,動(dòng)態(tài)調(diào)整的進(jìn)度停留在這個(gè)邊界。如果未指定,嘗試使用環(huán)境的寬度。如果為0,將不打印任何東西(只統(tǒng)計(jì))。 
mininterval : float, optional 
Minimum progress update interval, in seconds [default: 0.1]. 
最小進(jìn)度更新間隔,以秒為單位(默認(rèn)值:0.1)。 
maxinterval : float, optional 
Maximum progress update interval, in seconds [default: 10.0]. 
最大進(jìn)度更新間隔,以秒為單位(默認(rèn)值:10)。 
miniters : int, optional 
Minimum progress update interval, in iterations. 
If specified, will set mininterval to 0. 
最小進(jìn)度更新周期 
ascii : bool, optional 
If unspecified or False, use unicode (smooth blocks) to fill 
the meter. The fallback is to use ASCII characters 1-9 #. 
如果不設(shè)置,默認(rèn)為unicode編碼 
disable : bool, optional 
Whether to disable the entire progressbar wrapper 
[default: False]. 
是否禁用整個(gè)進(jìn)度條包裝(如果為T(mén)rue,進(jìn)度條不顯示) 
unit : str, optional 
String that will be used to define the unit of each iteration 
[default: it]. 
將被用來(lái)定義每個(gè)單元的字符串??? 
unit_scale : bool, optional 
If set, the number of iterations will be reduced/scaled 
automatically and a metric prefix following the 
International System of Units standard will be added 
(kilo, mega, etc.) [default: False]. 
如果設(shè)置,迭代的次數(shù)會(huì)自動(dòng)按照十、百、千來(lái)添加前綴,默認(rèn)為false 
dynamic_ncols : bool, optional 
If set, constantly alters ncols to the environment (allowing 
for window resizes) [default: False]. 
不斷改變ncols環(huán)境,允許調(diào)整窗口大小 
smoothing : float, optional 
Exponential moving average smoothing factor for speed estimates 
(ignored in GUI mode). Ranges from 0 (average speed) to 1 
(current/instantaneous speed) [default: 0.3]. 
 
bar_format : str, optional 
Specify a custom bar string formatting. May impact performance. 
If unspecified, will use ‘{l_bar}{bar}{r_bar}', where l_bar is 
‘{desc}{percentage:3.0f}%|' and r_bar is 
‘| {n_fmt}/{total_fmt} [{elapsed_str}<{remaining_str}, {rate_fmt}]' 
Possible vars: bar, n, n_fmt, total, total_fmt, percentage, 
rate, rate_fmt, elapsed, remaining, l_bar, r_bar, desc. 
自定義欄字符串格式化…默認(rèn)會(huì)使用{l_bar}{bar}{r_bar}的格式,格式同上 
 
initial : int, optional 
The initial counter value. Useful when restarting a progress 
bar [default: 0]. 
初始計(jì)數(shù)器值,默認(rèn)為0 
position : int, optional 
Specify the line offset to print this bar (starting from 0) 
Automatic if unspecified. 
Useful to manage multiple bars at once (eg, from threads). 
指定偏移,這個(gè)功能在多個(gè)條中有用 
gui : bool, optional 
WARNING: internal parameter - do not use. 
Use tqdm_gui(…) instead. If set, will attempt to use 
matplotlib animations for a graphical output [default: False]. 
內(nèi)部參數(shù)… 
Returns 
out : decorated iterator. 
返回為一個(gè)迭代器 

其實(shí)不用分析更多代碼,多看看幾個(gè)例子:(官網(wǎng)的例子)

7zx.py壓縮進(jìn)度條

# -*- coding: utf-8 -*- 
"""Usage: 
 7zx.py [--help | options] <zipfiles>... 
Options: 
 -h, --help   Print this help and exit 
 -v, --version Print version and exit 
 -c, --compressed    Use compressed (instead of uncompressed) file sizes 
 -s, --silent  Do not print one row per zip file 
 -y, --yes   Assume yes to all queries (for extraction) 
 -D=<level>, --debug=<level> 
         Print various types of debugging information. Choices: 
             CRITICAL|FATAL 
             ERROR 
             WARN(ING) 
             [default: INFO] 
             DEBUG 
             NOTSET 
 -d, --debug-trace   Print lots of debugging information (-D NOTSET) 
""" 
from __future__ import print_function 
from docopt import docopt 
import logging as log 
import subprocess 
import re 
from tqdm import tqdm 
import pty 
import os 
import io 
__author__ = "Casper da Costa-Luis <casper.dcl@physics.org>" 
__licence__ = "MPLv2.0" 
__version__ = "0.2.0" 
__license__ = __licence__ 
 
 
RE_SCN = re.compile("([0-9]+)\s+([0-9]+)\s+(.*)$", flags=re.M) 
 
 
def main(): 
  args = docopt(__doc__, version=__version__) 
  if args.pop('--debug-trace', False): 
    args['--debug'] = "NOTSET" 
  log.basicConfig(level=getattr(log, args['--debug'], log.INFO), 
          format='%(levelname)s: %(message)s') 
  log.debug(args) 
 
  # Get compressed sizes 
  zips = {} 
  for fn in args['<zipfiles>']: 
    info = subprocess.check_output(["7z", "l", fn]).strip() 
    finfo = RE_SCN.findall(info) 
 
    # builtin test: last line should be total sizes 
    log.debug(finfo) 
    totals = map(int, finfo[-1][:2]) 
    # log.debug(totals) 
    for s in range(2): 
      assert(sum(map(int, (inf[s] for inf in finfo[:-1]))) == totals[s]) 
    fcomp = dict((n, int(c if args['--compressed'] else u)) 
           for (u, c, n) in finfo[:-1]) 
    # log.debug(fcomp) 
    # zips : {'zipname' : {'filename' : int(size)}} 
    zips[fn] = fcomp 
 
  # Extract 
  cmd7zx = ["7z", "x", "-bd"] 
  if args['--yes']: 
    cmd7zx += ["-y"] 
  log.info("Extracting from {:d} file(s)".format(len(zips))) 
  with tqdm(total=sum(sum(fcomp.values()) for fcomp in zips.values()), 
       unit="B", unit_scale=True) as tall: 
    for fn, fcomp in zips.items(): 
      md, sd = pty.openpty() 
      ex = subprocess.Popen(cmd7zx + [fn], 
                 bufsize=1, 
                 stdout=md, # subprocess.PIPE, 
                 stderr=subprocess.STDOUT) 
      os.close(sd) 
      with io.open(md, mode="rU", buffering=1) as m: 
        with tqdm(total=sum(fcomp.values()), disable=len(zips) < 2, 
             leave=False, unit="B", unit_scale=True) as t: 
          while True: 
            try: 
              l_raw = m.readline() 
            except IOError: 
              break 
            l = l_raw.strip() 
            if l.startswith("Extracting"): 
              exname = l.lstrip("Extracting").lstrip() 
              s = fcomp.get(exname, 0) # 0 is likely folders 
              t.update(s) 
              tall.update(s) 
            elif l: 
              if not any(l.startswith(i) for i in 
                    ("7-Zip ", 
                    "p7zip Version ", 
                    "Everything is Ok", 
                    "Folders: ", 
                    "Files: ", 
                    "Size: ", 
                    "Compressed: ")): 
                if l.startswith("Processing archive: "): 
                  if not args['--silent']: 
                    t.write(t.format_interval( 
                      t.start_t - tall.start_t) + ' ' + 
                      l.lstrip("Processing archive: ")) 
                else: 
                  t.write(l) 
      ex.wait() 
 
 
main.__doc__ = __doc__ 
 
 
if __name__ == "__main__": 
  main() 

tqdm_wget.py

"""An example of wrapping manual tqdm updates for urllib reporthook. 
# urllib.urlretrieve documentation 
> If present, the hook function will be called once 
> on establishment of the network connection and once after each block read 
> thereafter. The hook will be passed three arguments; a count of blocks 
> transferred so far, a block size in bytes, and the total size of the file. 
Usage: 
  tqdm_wget.py [options] 
Options: 
-h, --help 
  Print this help message and exit 
-u URL, --url URL : string, optional 
  The url to fetch. 
  [default: http://www.doc.ic.ac.uk/~cod11/matryoshka.zip] 
-o FILE, --output FILE : string, optional 
  The local file path in which to save the url [default: /dev/null]. 
""" 
 
import urllib 
from tqdm import tqdm 
from docopt import docopt 
 
 
def my_hook(t): 
  """ 
  Wraps tqdm instance. Don't forget to close() or __exit__() 
  the tqdm instance once you're done with it (easiest using `with` syntax). 
  Example 
  ------- 
  >>> with tqdm(...) as t: 
  ...   reporthook = my_hook(t) 
  ...   urllib.urlretrieve(..., reporthook=reporthook) 
  """ 
  last_b = [0] 
 
  def inner(b=1, bsize=1, tsize=None): 
    """ 
    b : int, optional 
      Number of blocks just transferred [default: 1]. 
    bsize : int, optional 
      Size of each block (in tqdm units) [default: 1]. 
    tsize : int, optional 
      Total size (in tqdm units). If [default: None] remains unchanged. 
    """ 
    if tsize is not None: 
      t.total = tsize 
    t.update((b - last_b[0]) * bsize) 
    last_b[0] = b 
  return inner 
 
 
opts = docopt(__doc__) 
 
eg_link = opts['--url'] 
eg_file = eg_link.replace('/', ' ').split()[-1] 
with tqdm(unit='B', unit_scale=True, leave=True, miniters=1, 
     desc=eg_file) as t: # all optional kwargs 
  urllib.urlretrieve(eg_link, filename=opts['--output'], 
            reporthook=my_hook(t), data=None) 

examples.py

""" 
# Simple tqdm examples and profiling 
# Benchmark 
for i in _range(int(1e8)): 
  pass 
# Basic demo 
import tqdm 
for i in tqdm.trange(int(1e8)): 
  pass 
# Some decorations 
import tqdm 
for i in tqdm.trange(int(1e8), miniters=int(1e6), ascii=True, 
           desc="cool", dynamic_ncols=True): 
  pass 
# Nested bars 
from tqdm import trange 
for i in trange(10): 
  for j in trange(int(1e7), leave=False, unit_scale=True): 
    pass 
# Experimental GUI demo 
import tqdm 
for i in tqdm.tgrange(int(1e8)): 
  pass 
# Comparison to https://code.google.com/p/python-progressbar/ 
try: 
  from progressbar.progressbar import ProgressBar 
except ImportError: 
  pass 
else: 
  for i in ProgressBar()(_range(int(1e8))): 
    pass 
# Dynamic miniters benchmark 
from tqdm import trange 
for i in trange(int(1e8), miniters=None, mininterval=0.1, smoothing=0): 
  pass 
# Fixed miniters benchmark 
from tqdm import trange 
for i in trange(int(1e8), miniters=4500000, mininterval=0.1, smoothing=0): 
  pass 
""" 
 
from time import sleep 
from timeit import timeit 
import re 
 
# Simple demo 
from tqdm import trange 
for i in trange(16, leave=True): 
  sleep(0.1) 
 
# Profiling/overhead tests 
stmts = filter(None, re.split(r'\n\s*#.*?\n', __doc__)) 
for s in stmts: 
  print(s.replace('import tqdm\n', '')) 
  print(timeit(stmt='try:\n\t_range = xrange' 
           '\nexcept:\n\t_range = range\n' + s, number=1), 
     'seconds') 

pandas_progress_apply.py

import pandas as pd 
import numpy as np 
from tqdm import tqdm 
 
df = pd.DataFrame(np.random.randint(0, 100, (100000, 6))) 
 
# Register `pandas.progress_apply` and `pandas.Series.map_apply` with `tqdm` 
# (can use `tqdm_gui`, `tqdm_notebook`, optional kwargs, etc.) 
tqdm.pandas(desc="my bar!") 
 
# Now you can use `progress_apply` instead of `apply` 
# and `progress_map` instead of `map` 
df.progress_apply(lambda x: x**2) 
# can also groupby: 
# df.groupby(0).progress_apply(lambda x: x**2) 
 
 
# -- Source code for `tqdm_pandas` (really simple!) 
# def tqdm_pandas(t): 
#  from pandas.core.frame import DataFrame 
#  def inner(df, func, *args, **kwargs): 
#    t.total = groups.size // len(groups) 
#    def wrapper(*args, **kwargs): 
#      t.update(1) 
#      return func(*args, **kwargs) 
#    result = df.apply(wrapper, *args, **kwargs) 
#    t.close() 
#    return result 
#  DataFrame.progress_apply = inner 

引用tqdm并非強(qiáng)制作為依賴:

include_no_requirements.py

# How to import tqdm without enforcing it as a dependency 
try: 
  from tqdm import tqdm 
except ImportError: 
  def tqdm(*args, **kwargs): 
    if args: 
      return args[0] 
    return kwargs.get('iterable', None) 

參考:
https://github.com/tqdm/tqdm/tree/master/examples
https://pypi.python.org/pypi/tqdm
https://github.com/tqdm/tqdm

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Matplotlib可視化之添加讓統(tǒng)計(jì)圖變得簡(jiǎn)單易懂的注釋

    Matplotlib可視化之添加讓統(tǒng)計(jì)圖變得簡(jiǎn)單易懂的注釋

    今天給大家?guī)?lái)的文章是關(guān)于Python的,文章圍繞著Python Matplotlib可視化展開(kāi),文中非常詳細(xì)的介紹了如何給統(tǒng)計(jì)圖添加注釋,需要的朋友可以參考下
    2021-06-06
  • ID3決策樹(shù)以及Python實(shí)現(xiàn)詳細(xì)過(guò)程

    ID3決策樹(shù)以及Python實(shí)現(xiàn)詳細(xì)過(guò)程

    決策樹(shù)是我本人非常喜歡的機(jī)器學(xué)習(xí)模型,非常直觀容易理解,并且和數(shù)據(jù)結(jié)構(gòu)的結(jié)合很緊密,下面這篇文章主要給大家介紹了關(guān)于ID3決策樹(shù)以及Python實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • 如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具

    如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具

    這篇文章主要介紹了如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 詳解python發(fā)送各類郵件的主要方法

    詳解python發(fā)送各類郵件的主要方法

    python中email模塊使得處理郵件變得比較簡(jiǎn)單,這篇文章主要介紹了詳解python發(fā)送各類郵件的主要方法,有興趣的可以了解一下。
    2016-12-12
  • Python中的二叉樹(shù)查找算法模塊使用指南

    Python中的二叉樹(shù)查找算法模塊使用指南

    二叉樹(shù)查找算法,在開(kāi)發(fā)實(shí)踐中,會(huì)經(jīng)常用到。按照慣例,對(duì)于這么一個(gè)常用的東西,Python一定會(huì)提供輪子的。是的,python就是這樣,一定會(huì)讓開(kāi)發(fā)者省心,降低開(kāi)發(fā)者的工作壓力。
    2014-07-07
  • 聊聊Pytorch torch.cat與torch.stack的區(qū)別

    聊聊Pytorch torch.cat與torch.stack的區(qū)別

    這篇文章主要介紹了Pytorch torch.cat與torch.stack的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python三方庫(kù)之requests的快速上手

    python三方庫(kù)之requests的快速上手

    這篇文章主要介紹了python三方庫(kù)之requests的快速上手,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python使用中文正則表達(dá)式匹配指定中文字符串的方法示例

    Python使用中文正則表達(dá)式匹配指定中文字符串的方法示例

    這篇文章主要介紹了Python使用中文正則表達(dá)式匹配指定中文字符串的方法,結(jié)合實(shí)例形式分析了Python正則匹配及字符編碼相關(guān)操作技巧,需要的朋友可以參考下
    2017-01-01
  • 如何使用Python最小二乘法擬合曲線代碼詳解

    如何使用Python最小二乘法擬合曲線代碼詳解

    最小二乘法Least Square Method做為分類回歸算法的基礎(chǔ),有著悠久的歷史,下面這篇文章主要給大家介紹了關(guān)于如何使用Python最小二乘法擬合曲線的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • python 元組的使用方法

    python 元組的使用方法

    這篇文章主要介紹了python 元組的使用方法,文中講解非常細(xì)致,代碼幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06

最新評(píng)論