cpython (2.7): Add an itertools recipe showing how to use t.__copy__().

http://hg.python.org/cpython/rev/1026b1d47f30 changeset: 83037:1026b1d47f30 branch: 2.7 parent: 83034:e044d22d2f61 user: Raymond Hettinger <python@rcn.com> date: Sat Mar 30 23:37:57 2013 -0700 summary: Add an itertools recipe showing how to use t.__copy__(). files: Doc/library/itertools.rst | 12 ++++++++++++ 1 files changed, 12 insertions(+), 0 deletions(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -828,6 +828,18 @@ indices = sorted(random.randrange(n) for i in xrange(r)) return tuple(pool[i] for i in indices) + def tee_lookahead(t, i): + """Inspect the i-th upcomping value from a tee object + while leaving the tee object at its current position. + + Raise an IndexError if the underlying iterator doesn't + have enough values. + + """ + for value in islice(t.__copy__(), i, None): + return value + raise IndexError(i) + Note, many of the above recipes can be optimized by replacing global lookups with local variables defined as default values. For example, the *dotproduct* recipe can be written as:: -- Repository URL: http://hg.python.org/cpython

"Upcomping" -> "upcoming" On Mar 31, 2013 2:38 AM, "raymond.hettinger" <python-checkins@python.org> wrote:
http://hg.python.org/cpython/rev/1026b1d47f30 changeset: 83037:1026b1d47f30 branch: 2.7 parent: 83034:e044d22d2f61 user: Raymond Hettinger <python@rcn.com> date: Sat Mar 30 23:37:57 2013 -0700 summary: Add an itertools recipe showing how to use t.__copy__().
files: Doc/library/itertools.rst | 12 ++++++++++++ 1 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -828,6 +828,18 @@ indices = sorted(random.randrange(n) for i in xrange(r)) return tuple(pool[i] for i in indices)
+ def tee_lookahead(t, i): + """Inspect the i-th upcomping value from a tee object + while leaving the tee object at its current position. + + Raise an IndexError if the underlying iterator doesn't + have enough values. + + """ + for value in islice(t.__copy__(), i, None): + return value + raise IndexError(i) + Note, many of the above recipes can be optimized by replacing global lookups with local variables defined as default values. For example, the *dotproduct* recipe can be written as::
-- Repository URL: http://hg.python.org/cpython
_______________________________________________ Python-checkins mailing list Python-checkins@python.org http://mail.python.org/mailman/listinfo/python-checkins
participants (2)
-
Brett Cannon
-
raymond.hettinger