diff --git a/parmap.py b/parmap.py index f5ae309ce4b457f54358a520b7c0996613ce0e15..86374608ec047ae68ccac26cf47a499517cf462a 100644 --- a/parmap.py +++ b/parmap.py @@ -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) +