From 97a2d4f50872a2950c1ee6c337475763984cda4b Mon Sep 17 00:00:00 2001 From: Eric L Frederich Date: Mon, 18 May 2015 13:27:23 -0400 Subject: [PATCH 1/2] create shortcuts for pairwise chunks since pairs (key/val) are common --- boltons/iterutils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/boltons/iterutils.py b/boltons/iterutils.py index 23b38a5..629b0c8 100644 --- a/boltons/iterutils.py +++ b/boltons/iterutils.py @@ -165,6 +165,7 @@ def chunked(src, size, count=None, **kw): else: return list(itertools.islice(chunk_iter, count)) +pairwise = lambda src, *args, **kw: chunked(src, 2, *args, **kw) def chunked_iter(src, size, **kw): """Generates *size*-sized chunks from *src* iterable. Unless the @@ -213,6 +214,7 @@ def chunked_iter(src, size, **kw): yield postprocess(cur_chunk) return +pairwise_iter = lambda src, **kw: chunked(src, 2, **kw) def windowed(src, size): """Returns tuples with exactly length *size*. If the iterable is From 28715b4de4f24ce9367b8aa3536f8087c0915d9c Mon Sep 17 00:00:00 2001 From: Eric L Frederich Date: Mon, 18 May 2015 18:58:36 -0400 Subject: [PATCH 2/2] use functions instead of lambdas for documentation --- boltons/iterutils.py | 12 +++++++++--- docs/iterutils.rst | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/boltons/iterutils.py b/boltons/iterutils.py index 629b0c8..036eb5a 100644 --- a/boltons/iterutils.py +++ b/boltons/iterutils.py @@ -165,8 +165,6 @@ def chunked(src, size, count=None, **kw): else: return list(itertools.islice(chunk_iter, count)) -pairwise = lambda src, *args, **kw: chunked(src, 2, *args, **kw) - def chunked_iter(src, size, **kw): """Generates *size*-sized chunks from *src* iterable. Unless the optional *fill* keyword argument is provided, iterables not even @@ -214,7 +212,15 @@ def chunked_iter(src, size, **kw): yield postprocess(cur_chunk) return -pairwise_iter = lambda src, **kw: chunked(src, 2, **kw) +def pairwise(src, count=None, **kw): + """Shortcut for calling chunked with size set to 2 + """ + return chunked(src, 2, count, **kw) + +def pairwise_iter(src, **kw): + """Shortcut for calling chunked_iter with size set to 2 + """ + return chunked_iter(src, 2, **kw) def windowed(src, size): """Returns tuples with exactly length *size*. If the iterable is diff --git a/docs/iterutils.rst b/docs/iterutils.rst index ef90a4a..bdd206f 100644 --- a/docs/iterutils.rst +++ b/docs/iterutils.rst @@ -14,6 +14,8 @@ from the standard library. .. autofunction:: split_iter .. autofunction:: chunked .. autofunction:: chunked_iter +.. autofunction:: pairwise +.. autofunction:: pairwise_iter .. autofunction:: windowed .. autofunction:: windowed_iter