Skip to content

Commit

Permalink
Merge pull request #98 from esheldon/pbar-simple
Browse files Browse the repository at this point in the history
Add simple pbar
  • Loading branch information
esheldon authored Oct 18, 2024
2 parents 6f4bf7a + 99bf4e0 commit ba4ee98
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Enhancements

- add simple option to pbar to spam log files less
- numpy_util.match works for non-integer data types

0.6.14
Expand Down
111 changes: 107 additions & 4 deletions esutil/pbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
https://github.com/noamraph/tqdm
"""
__all__ = ['PBar', 'pbar', 'prange']
__all__ = ['pbar', 'prange', 'PBar']

import sys
import time


def pbar(iterable, desc='', total=None, leave=True, file=sys.stderr,
mininterval=0.5, miniters=1, n_bars=20):
mininterval=0.5, miniters=1, n_bars=20, simple=False):
"""
Get an iterable object, and return an iterator which acts exactly like the
iterable, but prints a progress meter and updates it every time a value is
Expand Down Expand Up @@ -39,14 +39,60 @@ def pbar(iterable, desc='', total=None, leave=True, file=sys.stderr,
If less than mininterval seconds or miniters iterations have passed
since the last progress meter update, it is not updated again.
n_bars: int
Number of bars to show
simple: bool
If set to True, a simple countup 0 to 9 is show, independent
of the other inputs. Useful when you don't want to spam
a log file with lots of data
|0123456789|
"""
if simple:
return sbar(iterable, desc=desc, total=total, file=file)
else:
return _pbar_full(
iterable, desc=desc, total=total, file=file,
leave=leave, mininterval=mininterval, miniters=miniters,
n_bars=n_bars, simple=simple,
)


PBar = pbar


def _pbar_full(
iterable, desc='', total=None, leave=True, file=sys.stderr,
mininterval=0.5, miniters=1, n_bars=20, simple=False,
):
"""
See docs for pbar
"""
prefix = desc+': ' if desc else ''

if total is None:
try:
total = len(iterable)
except TypeError:
total = None

prefix = desc+': ' if desc else ''
if simple:
# we need enbed this because pbar is a generator
assert total is not None, (
'iterable must have len or send total for simple pbar'
)
_pnn(prefix + '|', file)
plast = -1
for i, obj in enumerate(iterable):
yield obj
i += 1

p = int(i / total * 10)
if p > plast:
_pnn(p, file)
plast = p

print('|', file=file, flush=True)
return

sp = StatusPrinter(file)
sp.print_status(prefix + format_meter(0, total, 0, n_bars=n_bars))
Expand Down Expand Up @@ -90,7 +136,60 @@ def pbar(iterable, desc='', total=None, leave=True, file=sys.stderr,
file.write('\n')


PBar = pbar
def sbar(iterable, desc='', total=None, file=sys.stderr):
"""
Get an iterable object, and return an iterator which acts exactly like the
iterable, but prints progress meter. This simple version does a countup 0
to 9, independent of the other inputs. Useful when you don't want to spam
a log file with lots of data
|0123456789|
parameters
----------
iterable: iterable
An iterable that is iterated over; the objects are yielded
desc: string, optional
An optional short string, describing the progress, that is added
in the beginning of the line.
total: int, optional
Optional number of expected iterations. If not given,
len(iterable) is used if it is defined.
file: file-like object, optional
A file-like object to output the progress message to. Default
stderr
"""

prefix = desc+': ' if desc else ''

if total is None:
try:
total = len(iterable)
except TypeError:
raise RuntimeError(
'for sbar you must send total= '
'if the iterable does not provide length'
)

def pnn(d):
print(d, end='', file=file, flush=True)

pnn(prefix + '|')
plast = -1
tm0 = time.time()
for i, obj in enumerate(iterable):
yield obj
i += 1

p = int(i / total * 10)

if p > plast:
pnn(p)
plast = p

tm = time.time() - tm0
tms = format_interval(tm)
print(f'| {tms}', file=file, flush=True)


def prange(*args, **kwargs):
Expand Down Expand Up @@ -205,3 +304,7 @@ def print_status(self, s):
self.file.write('\r'+s+' '*max(self.last_printed_len-len(s), 0))
self.file.flush()
self.last_printed_len = len(s)


def _pnn(d, file):
print(d, end='', file=file, flush=True)

0 comments on commit ba4ee98

Please sign in to comment.