Skip to content

Commit

Permalink
test parallel generators. map accepts enum or enumerate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejolly committed Oct 22, 2023
1 parent f9da6a9 commit a3e5185
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ cachedir
.venv
dist
!**/mtcars.csv
tricks.ipynb
2 changes: 1 addition & 1 deletion docs/intro.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@
}
],
"source": [
"# myfunc needs to accept an 'random_seed' argument\n",
"# myfunc needs to accept an 'random_state' argument\n",
"def myfunc(x, random_state=None):\n",
" \"\"\"Simulate expensive function\"\"\"\n",
" from utilz import check_random_state\n",
Expand Down
4 changes: 2 additions & 2 deletions utilz/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def map(
func (callable): function to map
iterme (iterable): an iterable for which each element will be passed to func
into a single list, array, or dataframe based on `axis`; Default True
enum (bool, optional): whether the value of the current iteration should be passed to `func` as the special kwarg `idx`. Make sure `func` can handle a kwarg named `idx`. Default False
enum/enumerate (bool, optional): whether the value of the current iteration should be passed to `func` as the special kwarg `idx`. Make sure `func` can handle a kwarg named `idx`. Default False
random_state (bool/int, optional): whether a randomly initialized seed should be
passed to `func` as the special kwarg `random_state`. The function should pass
this seed to the `utilz.check_random_state` helper to generate a random number
Expand Down Expand Up @@ -262,7 +262,7 @@ def map(
"""

enum = kwargs.pop("enum", False)
enum = kwargs.pop("enum", False) or kwargs.pop("enumerate", False)
random_state = kwargs.pop("random_state", False)
n_jobs = kwargs.pop("n_jobs", 1)
backend = kwargs.pop("backend", None)
Expand Down
23 changes: 22 additions & 1 deletion utilz/tests/test_genz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from types import GeneratorType
from utilz.genz import make_gen, combine_gens
import pytest
from utilz.maps import map
from timeit import timeit
from time import sleep


def test_make_gen():
Expand All @@ -17,3 +19,22 @@ def test_combine_gens():
out = list(combine_gens(l1, l2, l3, l4))
assert len(out) == len(l1) * len(l2) * len(l3) * len(l4)
assert out[-1] == (9, 4, 2, 19)


def test_parallel_gens():
# NOTE: Parallelizing generators does not preserve iteration order!
# Note combining generators means function
# should handle variable args and unpack them
# as needed
def sleepy_sum(*args):
# a, b = args
sleep(1)
return sum(*args)

l1 = list(range(3))
l2 = list(range(2))

single = timeit(lambda: map(sleepy_sum, combine_gens(l1, l2)), number=1)
par = timeit(lambda: map(sleepy_sum, combine_gens(l1, l2), n_jobs=2), number=1)

assert single > par

0 comments on commit a3e5185

Please sign in to comment.