Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit befa37d

Browse files
committed
Minor updates:
* Updated comment on design of imap() * Added untraversed object in izip() structure * Replaced the pairwise() example with a more general window() example
1 parent 3a8fbe7 commit befa37d

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

Doc/lib/libitertools.tex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,6 @@ \subsection{Examples \label{itertools-example}}
358358
... "Returns True if pred(x) is False for every element in the iterable"
359359
... return not nth(ifilter(pred, seq), 0)
360360
361-
>>> def pairwise(seq):
362-
... "s -> (s0,s1), (s1,s2), (s2, s3), ..."
363-
... return izip(seq, islice(seq,1,None))
364-
365361
>>> def padnone(seq):
366362
... "Returns the sequence elements and then returns None indefinitely"
367363
... return chain(seq, repeat(None))
@@ -373,4 +369,15 @@ \subsection{Examples \label{itertools-example}}
373369
>>> def dotproduct(vec1, vec2):
374370
... return sum(imap(operator.mul, vec1, vec2))
375371
372+
>>> def window(seq, n=2):
373+
... "Returns a sliding window (of width n) over data from the iterable"
374+
... " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
375+
... it = iter(seq)
376+
... result = tuple(islice(it, n))
377+
... if len(result) == n:
378+
... yield result
379+
... for elem in it:
380+
... result = result[1:] + (elem,)
381+
... yield result
382+
376383
\end{verbatim}

Lib/test/test_itertools.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,6 @@ def test_dropwhile(self):
427427
... "Returns True if pred(x) is False for every element in the iterable"
428428
... return not nth(ifilter(pred, seq), 0)
429429
430-
>>> def pairwise(seq):
431-
... "s -> (s0,s1), (s1,s2), (s2, s3), ..."
432-
... return izip(seq, islice(seq,1,len(seq)))
433-
434430
>>> def padnone(seq):
435431
... "Returns the sequence elements and then returns None indefinitely"
436432
... return chain(seq, repeat(None))
@@ -442,6 +438,16 @@ def test_dropwhile(self):
442438
>>> def dotproduct(vec1, vec2):
443439
... return sum(imap(operator.mul, vec1, vec2))
444440
441+
>>> def window(seq, n=2):
442+
... "Returns a sliding window (of width n) over data from the iterable"
443+
... " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
444+
... it = iter(seq)
445+
... result = tuple(islice(it, n))
446+
... if len(result) == n:
447+
... yield result
448+
... for elem in it:
449+
... result = result[1:] + (elem,)
450+
... yield result
445451
446452
This is not part of the examples but it tests to make sure the definitions
447453
perform as purported.
@@ -473,9 +479,12 @@ def test_dropwhile(self):
473479
>>> no(lambda x: x%2==0, [1, 2, 5, 9])
474480
False
475481
476-
>>> list(pairwise('abc'))
482+
>>> list(window('abc'))
477483
[('a', 'b'), ('b', 'c')]
478484
485+
>>> list(window('abc',5))
486+
[]
487+
479488
>>> list(islice(padnone('abc'), 0, 6))
480489
['a', 'b', 'c', None, None, None]
481490

Modules/itertoolsmodule.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ the following reasons:
882882
None.
883883
884884
4) If a need does arise, it can be met by __builtins__.map() or by
885-
writing a generator.
885+
writing: chain(iterable, repeat(None)).
886886
887887
5) Similar toolsets in Haskell and SML do not have automatic None fill-in.
888888
*/
@@ -1574,8 +1574,18 @@ izip_dealloc(izipobject *lz)
15741574
static int
15751575
izip_traverse(izipobject *lz, visitproc visit, void *arg)
15761576
{
1577-
if (lz->ittuple)
1578-
return visit(lz->ittuple, arg);
1577+
int err;
1578+
1579+
if (lz->ittuple) {
1580+
err = visit(lz->ittuple, arg);
1581+
if (err)
1582+
return err;
1583+
}
1584+
if (lz->result) {
1585+
err = visit(lz->result, arg);
1586+
if (err)
1587+
return err;
1588+
}
15791589
return 0;
15801590
}
15811591

0 commit comments

Comments
 (0)