A progress tool for humans
Project description
frogress - progress tool for snakes
/----------------------------------------------------------------------------------\
| |
@..@ /| [###.......] Progress: 34.2MB / 125.8MB | 25.0% | Time: 14min3s | ETA: 19min52s |
(----) / | |
( >__< ) \----------------------------------------------------------------------------------/
^^ ~~ ^^
frogress is small progress indication tool to be used for fast prototyping. Why frogress anyway? Because it's a bar (most of the time) and it jumps on your terminal, that's why!
- Does NOT break your workflow (in most cases there is no need to call progress bar to render itself)
- It can guess if you iterate over a list (or similar iterable) ...
- or if iterate over a file ...
- or if iterate over generator - provided you know it's total length ...
- or not! (no eta, no total steps, no percentage and indicator instead of a bar but it works!)
- And you can easily teach it how to show progress of fat, gzipped xml file when using lxml to parse it
- Supports Python 2.6+, Python 3, PyPY
- Fully tested
Iteration examples
Iterate over a list
>>> 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
Iterate over a file
>>> 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
Iterate over generator
>>> 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
Iterate over a generator with unknown total number of steps
>>> 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
Iterate over gzipped xml file using lxml
The only problem with how to present a progress of file that's being processed
is the source from which frogress should extract progress information. We can
try to do this simple way (without knowledge of how much of the file is already
processed) or give frogress
a source.
Simple way
>>> 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
This is perfectly fine: we passed an iterable that doesn't provide information on how many total items there is to process - so we have an bar activity indicator, no total steps at the progress and no ETA.
However, there is clearly a way of retrieving this information - after all this
is only a file that's being processed. And that file should be passed as
source
argument to the frogress.bar
function.
Pass source
>>> 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
Just remember to pass file that is actually processed, not a wrapper! Standard
file would be passed directly, however in example, gzip
module wraps stream
it is working on and it's available as attribute myfileobj
. On the other
hand bz2
module doesn't wrap streams. And so on. frogress
can guess if
a stream is file like object, however passing proper source is responsibility
of the user.
Spinner without actual iterable
Sometimes we just want to show that program is doing something but we don't really know how long it would take (i.e. perform couple of API requests).
Example
import frogress
import time
def cmd(s=0):
time.sleep(s)
def main():
with frogress.spinner("Waiting for response 1", done="OK"):
cmd(0.5)
with frogress.spinner("Waiting for response 2", done="Done"):
cmd(0.5)
with frogress.spinner("Waiting for response 3", done="All done, really!"):
cmd(0.5)
main()
Progress bar class API
Most of the time you won't need to call those API directly - frogress.bar
function should work for majority of the use cases. If, however, you feel like
you need to make some customization, here we present some examples
>>> 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)
Tips & Tricks
How to change label of the progress widget
>>> 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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file frogress-0.10.0.tar.gz
.
File metadata
- Download URL: frogress-0.10.0.tar.gz
- Upload date:
- Size: 18.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19c801a520fc977f29ed599404ee09d1f77e25b5202146bf5ebabebca3ee53a5 |
|
MD5 | 5465e47f1a791bd49bf660ebb10d626d |
|
BLAKE2b-256 | f14e76125d6a946e95f018aaa8940cde118c1ef3b8557b74c7bd4ad7a11edcd5 |
File details
Details for the file frogress-0.10.0-py3-none-any.whl
.
File metadata
- Download URL: frogress-0.10.0-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8add87f34b841ad2abbb226a9181ae4850389ed622cea3f71b7d7b7e6942756b |
|
MD5 | fef4d82e3dec08c6f7fc16b5efa10e72 |
|
BLAKE2b-256 | c2dc459638fd52e1c79c349bd8b4e4db3293fffef94008168777516243525cd9 |