forked from tqdm/tqdm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
381 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from tqdm.contrib import tenumerate, tzip, tmap | ||
import numpy as np | ||
|
||
for _ in tenumerate(range(int(1e6)), desc="builtin enumerate"): | ||
pass | ||
|
||
for _ in tenumerate(np.random.random((999, 999)), desc="numpy.ndenumerate"): | ||
pass | ||
|
||
for _ in tzip(np.arange(1e6), np.arange(1e6) + 1, desc="builtin zip"): | ||
pass | ||
|
||
mapped = tmap(lambda x: x + 1, np.arange(1e6), desc="builtin map") | ||
assert (np.arange(1e6) + 1 == list(mapped)).all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Thin wrappers around `concurrent.futures`. | ||
""" | ||
from __future__ import absolute_import | ||
from tqdm.auto import tqdm as tqdm_auto | ||
from copy import deepcopy | ||
try: | ||
from os import cpu_count | ||
except ImportError: | ||
try: | ||
from multiprocessing import cpu_count | ||
except ImportError: | ||
def cpu_count(): | ||
return 4 | ||
import sys | ||
__author__ = {"github.com/": ["casperdcl"]} | ||
__all__ = ['thread_map', 'process_map'] | ||
|
||
|
||
def _executor_map(PoolExecutor, fn, *iterables, **tqdm_kwargs): | ||
""" | ||
Implementation of `thread_map` and `process_map`. | ||
Parameters | ||
---------- | ||
tqdm_class : [default: tqdm.auto.tqdm]. | ||
""" | ||
kwargs = deepcopy(tqdm_kwargs) | ||
kwargs.setdefault("total", len(iterables[0])) | ||
tqdm_class = kwargs.pop("tqdm_class", tqdm_auto) | ||
max_workers = kwargs.pop("max_workers", min(32, cpu_count() + 4)) | ||
pool_kwargs = dict(max_workers=max_workers) | ||
if sys.version_info[:2] >= (3, 7): | ||
# share lock in case workers are already using `tqdm` | ||
pool_kwargs.update( | ||
initializer=tqdm_class.set_lock, initargs=(tqdm_class.get_lock(),)) | ||
with PoolExecutor(**pool_kwargs) as ex: | ||
return list(tqdm_class(ex.map(fn, *iterables), **kwargs)) | ||
|
||
|
||
def thread_map(fn, *iterables, **tqdm_kwargs): | ||
""" | ||
Equivalent of `list(map(fn, *iterables))` | ||
driven by `concurrent.futures.ThreadPoolExecutor`. | ||
Parameters | ||
---------- | ||
tqdm_class : [default: tqdm.auto.tqdm]. | ||
""" | ||
from concurrent.futures import ThreadPoolExecutor | ||
return _executor_map(ThreadPoolExecutor, fn, *iterables, **tqdm_kwargs) | ||
|
||
|
||
def process_map(fn, *iterables, **tqdm_kwargs): | ||
""" | ||
Equivalent of `list(map(fn, *iterables))` | ||
driven by `concurrent.futures.ProcessPoolExecutor`. | ||
Parameters | ||
---------- | ||
tqdm_class : [default: tqdm.auto.tqdm]. | ||
""" | ||
from concurrent.futures import ProcessPoolExecutor | ||
return _executor_map(ProcessPoolExecutor, fn, *iterables, **tqdm_kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
Thin wrappers around `itertools`. | ||
""" | ||
from __future__ import absolute_import | ||
from tqdm.auto import tqdm as tqdm_auto | ||
from copy import deepcopy | ||
import itertools | ||
__author__ = {"github.com/": ["casperdcl"]} | ||
__all__ = ['product'] | ||
|
||
|
||
def product(*iterables, **tqdm_kwargs): | ||
""" | ||
Equivalent of `itertools.product`. | ||
Parameters | ||
---------- | ||
tqdm_class : [default: tqdm.auto.tqdm]. | ||
""" | ||
kwargs = deepcopy(tqdm_kwargs) | ||
tqdm_class = kwargs.pop("tqdm_class", tqdm_auto) | ||
try: | ||
lens = list(map(len, iterables)) | ||
except TypeError: | ||
total = None | ||
else: | ||
total = 1 | ||
for i in lens: | ||
total *= i | ||
kwargs.setdefault("total", total) | ||
with tqdm_class(**kwargs) as t: | ||
for i in itertools.product(*iterables): | ||
yield i | ||
t.update() |
Oops, something went wrong.