泡沫是简单的进步工具
frogress的Python项目详细描述
/----------------------------------------------------------------------------------\ | | @..@ /| [###.......] Progress: 34.2MB / 125.8MB | 25.0% | Time: 14min3s | ETA: 19min52s | (----) / | | ( >__< ) \----------------------------------------------------------------------------------/ ^^ ~~ ^^
Frogress是用于快速成型的小进度指示工具。 为什么frogress?因为这是一个酒吧(大多数时候)它会跳下去 你的终端,这就是原因!
- 不会中断您的工作流程(在大多数情况下,不需要调用 进度条渲染自己)
- 它可以猜测您是否iterate over a list(或类似的iterable)..
- 或者在文件上迭代…
- 或者如果迭代生成器-只要知道它的总长度…
- 或者不!(没有预计到达时间,没有总步骤,没有百分比和指示器,而不是条 但它是有效的!)
- 您可以很容易地教它如何显示胖的、gzip格式的xml文件的进度 当使用lxml解析它时
- 支持Python2.6+、Python3、PyPy
- 全面测试
迭代示例
遍历列表
>>> import frogress >>> items = [1, 2, 3, 4, 5] >>> for item in frogress.bar(items): ... pass # do something with item [##........] Step 2/5 | 20.0% | Time: 0.1s | ETA: 0.5s
在文件上迭代
>>> import frogress >>> for line in frogress.bar(open('/path/to/file', steps_label='Progress')): ... pass # do something cruel with a line [###.......] Progress: 3.2MB / 12.8MB | 25.0% | Time: 14min3s | ETA: 19min52s
在生成器上迭代
>>> import frogress >>> count = 100 >>> items = range(count) >>> for item in frogress.bar(items, steps=count): ... pass # do something with item [#########.] Step 86/100 | 86.0% | Time: 1.2s | ETA: 7.3s
在步骤总数未知的生成器上迭代
>>> import frogress >>> def counter(): ... num = 1 ... while True: ... yield num ... num += 1 ... >>> items = counter() >>> for item in frogress.bar(items): ... pass # do something with item [........#.] Step: 1410 | Time: 2min14s [.........#] Step: 1411 | Time: 2min15s [........#.] Step: 1412 | Time: 2min16s [.......#..] Step: 1413 | Time: 2min17s
使用lxml在gzip xml文件上迭代
如何显示正在处理的文件进度的唯一问题 是Frogress从中提取进度信息的来源。我们可以 尝试这种简单的方法(不知道文件已经有多少 或者给frogressasource。
简单方法
>>> import frogress >>> import gzip >>> from lxml.etree import iterparse >>> stream = gzip.open('my-fat.xml.gz') >>> context = iterparse(stream) >>> for action, element in frogress.bar(context): ... pass # do something with element ... element.clear() # don't forget about the memory! [...#......] Progress: 41923 | Time: 1h42min
这很好:我们通过了一个不提供信息的iterable 总共要处理多少个项目,所以我们有一个酒吧活动 指标,没有进展的总步骤,也没有预计到达时间。
然而,显然有一种方法可以检索这些信息-毕竟 只是一个正在处理的文件。该文件应作为 source参数指向frogress.bar函数。
通过源
>>> import frogress >>> import gzip >>> from lxml.etree import iterparse >>> stream = gzip.open('my-fat.xml.gz') >>> context = iterparse(stream) >>> for action, element in frogress.bar(context, source=stream.myfileobj): ... pass # do something with element ... element.clear() # don't forget about the memory! [#####.....] Progress: 73.5MB / 156.4MB | 47.3% | Time: 1h42min | ETA: 1h53min
只需记住传递实际处理过的文件,而不是包装器!标准 文件将直接传递,但是在示例中,gzipmodule包装流 它正在工作,可用作属性myfileobj。另一方面 handbz2模块不包装流。等等。frogress可以猜测 流是类似文件的对象,但是传递正确的源是责任 用户的。
进度条类API
大多数情况下,您不需要直接调用这些api-frogress.bar 功能应该适用于大多数用例。但是,如果你觉得 您需要进行一些定制,这里我们提供一些示例:
>>> import frogress >>> items = [1, 2, 3, 4, 5] >>> progressbar = frogress.Bar(items) >>> progressbar.step 0 >>> progressbar.started # it's still None >>> progressbar.finished # here too >>> for item in progressbar: ... pass # process the item (it will draw progressbar during iteration) >>> progressbar.step 5 >>> progressbar.widgets [<BarWidget>, <ProgressWidget>, <PercentageWidget>, <EtaWidget>, <TimeWidget>] >>> len(progressbar) 5 >>> progressbar.output <open file '<stderr>', mode 'w' at 0x103df61e0> >>> progressbar.started datetime.datetime(2013, 5, 12, 22, 2, 26, 752454) >>> progressbar.finished datetime.datetime(2013, 5, 12, 22, 2, 26, 792901)
提示和技巧
如何更改进度小部件的标签
>>> import frogress >>> items = [1, 2, 3, 4, 5] >>> widgets = [frogress.BarWidget, frogress.ProgressWidget('Items: '), frogress.TimerWidget] >>> for item in frogress.bar(items, widgets=widgets): >>> pass