Skip to content
Snippets Groups Projects
Commit a9ad147e authored by Radek Hušek's avatar Radek Hušek
Browse files

parmap.py: add doctests

parent b9eeee86
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,15 @@ import multiprocessing
from itertools import chain
def chunkme(X, chunksize):
"""Return items generated by X in chunks (lists) of size chunksize."""
"""Return items generated by X in chunks (lists) of size chunksize.
EXAMPLE:
>>> list(chunkme(range(20), 7))
[[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19]]
>>> list(chunkme(range(0), 2))
[]
"""
chunk = []
for x in X:
chunk.append(x)
......@@ -62,6 +70,11 @@ def parmap(f, X, nprocs = None, chunksize = 1, chunks_in_flight = None,
in_order -- if True, input sequence may be reordered during processing
multimap -- if True, f is expected to return an Iterable and flattening is done
out_chunksize -- size of chunks send from workers to consumer
EXAMPLES:
>>> list(parmap(lambda x: x+1, range(10)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
"""
if nprocs is None:
nprocs = multiprocessing.cpu_count()
......@@ -127,3 +140,11 @@ def parmap(f, X, nprocs = None, chunksize = 1, chunks_in_flight = None,
return chain.from_iterable(get_chunk())
if __name__ == "__main__":
import doctest
(f, t) = doctest.testmod()
print "%s: %i tests of %i failed." % (__file__, f, t)
if f > 0:
exit(1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment