2020-06-10 16:04:54 +00:00
|
|
|
"""
|
|
|
|
Thin wrappers around common functions.
|
|
|
|
|
|
|
|
Subpackages contain potentially unstable extensions.
|
|
|
|
"""
|
|
|
|
import sys
|
2021-12-01 20:47:00 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from ..auto import tqdm as tqdm_auto
|
|
|
|
from ..std import tqdm
|
|
|
|
from ..utils import ObjectWrapper
|
|
|
|
|
2020-06-10 16:04:54 +00:00
|
|
|
__author__ = {"github.com/": ["casperdcl"]}
|
|
|
|
__all__ = ['tenumerate', 'tzip', 'tmap']
|
|
|
|
|
|
|
|
|
|
|
|
class DummyTqdmFile(ObjectWrapper):
|
|
|
|
"""Dummy file-like that will write to tqdm"""
|
2021-12-01 20:47:00 +00:00
|
|
|
|
|
|
|
def __init__(self, wrapped):
|
|
|
|
super(DummyTqdmFile, self).__init__(wrapped)
|
|
|
|
self._buf = []
|
|
|
|
|
2020-06-10 16:04:54 +00:00
|
|
|
def write(self, x, nolock=False):
|
2021-12-01 20:47:00 +00:00
|
|
|
nl = b"\n" if isinstance(x, bytes) else "\n"
|
|
|
|
pre, sep, post = x.rpartition(nl)
|
|
|
|
if sep:
|
|
|
|
blank = type(nl)()
|
|
|
|
tqdm.write(blank.join(self._buf + [pre, sep]),
|
|
|
|
end=blank, file=self._wrapped, nolock=nolock)
|
|
|
|
self._buf = [post]
|
|
|
|
else:
|
|
|
|
self._buf.append(x)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if self._buf:
|
|
|
|
blank = type(self._buf[0])()
|
|
|
|
try:
|
|
|
|
tqdm.write(blank.join(self._buf), end=blank, file=self._wrapped)
|
|
|
|
except (OSError, ValueError):
|
|
|
|
pass
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
def builtin_iterable(func):
|
|
|
|
"""Wraps `func()` output in a `list()` in py2"""
|
|
|
|
if sys.version_info[:1] < (3,):
|
|
|
|
@wraps(func)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
return list(func(*args, **kwargs))
|
|
|
|
return inner
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
|
|
def tenumerate(iterable, start=0, total=None, tqdm_class=tqdm_auto, **tqdm_kwargs):
|
2020-06-10 16:04:54 +00:00
|
|
|
"""
|
|
|
|
Equivalent of `numpy.ndenumerate` or builtin `enumerate`.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
tqdm_class : [default: tqdm.auto.tqdm].
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
import numpy as np
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if isinstance(iterable, np.ndarray):
|
2021-12-01 20:47:00 +00:00
|
|
|
return tqdm_class(np.ndenumerate(iterable), total=total or iterable.size,
|
|
|
|
**tqdm_kwargs)
|
|
|
|
return enumerate(tqdm_class(iterable, total=total, **tqdm_kwargs), start)
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
@builtin_iterable
|
|
|
|
def tzip(iter1, *iter2plus, **tqdm_kwargs):
|
2020-06-10 16:04:54 +00:00
|
|
|
"""
|
|
|
|
Equivalent of builtin `zip`.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
tqdm_class : [default: tqdm.auto.tqdm].
|
|
|
|
"""
|
2021-12-01 20:47:00 +00:00
|
|
|
kwargs = tqdm_kwargs.copy()
|
2020-06-10 16:04:54 +00:00
|
|
|
tqdm_class = kwargs.pop("tqdm_class", tqdm_auto)
|
2021-12-01 20:47:00 +00:00
|
|
|
for i in zip(tqdm_class(iter1, **kwargs), *iter2plus):
|
2020-06-10 16:04:54 +00:00
|
|
|
yield i
|
|
|
|
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
@builtin_iterable
|
|
|
|
def tmap(function, *sequences, **tqdm_kwargs):
|
2020-06-10 16:04:54 +00:00
|
|
|
"""
|
|
|
|
Equivalent of builtin `map`.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
tqdm_class : [default: tqdm.auto.tqdm].
|
|
|
|
"""
|
2021-12-01 20:47:00 +00:00
|
|
|
for i in tzip(*sequences, **tqdm_kwargs):
|
2020-06-10 16:04:54 +00:00
|
|
|
yield function(*i)
|