2007-08-15 11:28:22 -03:00
|
|
|
|
:mod:`itertools` --- Functions creating iterators for efficient looping
|
|
|
|
|
=======================================================================
|
|
|
|
|
|
|
|
|
|
.. module:: itertools
|
2010-12-01 19:45:20 -04:00
|
|
|
|
:synopsis: Functions creating iterators for efficient looping.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. moduleauthor:: Raymond Hettinger <python@rcn.com>
|
|
|
|
|
.. sectionauthor:: Raymond Hettinger <python@rcn.com>
|
|
|
|
|
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
|
.. testsetup::
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
from itertools import *
|
2022-10-18 16:09:34 -03:00
|
|
|
|
import collections
|
|
|
|
|
import math
|
|
|
|
|
import operator
|
|
|
|
|
import random
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
|
--------------
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
|
|
2009-02-17 16:00:59 -04:00
|
|
|
|
This module implements a number of :term:`iterator` building blocks inspired
|
|
|
|
|
by constructs from APL, Haskell, and SML. Each has been recast in a form
|
|
|
|
|
suitable for Python.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
The module standardizes a core set of fast, memory efficient tools that are
|
2009-02-17 16:00:59 -04:00
|
|
|
|
useful by themselves or in combination. Together, they form an "iterator
|
|
|
|
|
algebra" making it possible to construct specialized tools succinctly and
|
|
|
|
|
efficiently in pure Python.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a
|
2010-01-21 16:57:24 -04:00
|
|
|
|
sequence ``f(0), f(1), ...``. The same effect can be achieved in Python
|
2008-03-12 22:26:19 -03:00
|
|
|
|
by combining :func:`map` and :func:`count` to form ``map(f, count())``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-03-11 21:29:44 -03:00
|
|
|
|
These tools and their built-in counterparts also work well with the high-speed
|
|
|
|
|
functions in the :mod:`operator` module. For example, the multiplication
|
|
|
|
|
operator can be mapped across two vectors to form an efficient dot-product:
|
2023-01-07 14:46:35 -04:00
|
|
|
|
``sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))``.
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
|
|
|
|
|
2017-12-15 17:17:55 -04:00
|
|
|
|
**Infinite iterators:**
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
2009-04-10 16:02:36 -03:00
|
|
|
|
================== ================= ================================================= =========================================
|
|
|
|
|
Iterator Arguments Results Example
|
|
|
|
|
================== ================= ================================================= =========================================
|
2023-10-27 01:30:13 -03:00
|
|
|
|
:func:`count` [start[, step]] start, start+step, start+2*step, ... ``count(10) --> 10 11 12 13 14 ...``
|
2009-04-10 16:02:36 -03:00
|
|
|
|
:func:`cycle` p p0, p1, ... plast, p0, p1, ... ``cycle('ABCD') --> A B C D A B C D ...``
|
|
|
|
|
:func:`repeat` elem [,n] elem, elem, elem, ... endlessly or up to n times ``repeat(10, 3) --> 10 10 10``
|
|
|
|
|
================== ================= ================================================= =========================================
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
|
|
|
|
**Iterators terminating on the shortest input sequence:**
|
|
|
|
|
|
2014-01-16 11:10:13 -04:00
|
|
|
|
============================ ============================ ================================================= =============================================================
|
|
|
|
|
Iterator Arguments Results Example
|
|
|
|
|
============================ ============================ ================================================= =============================================================
|
|
|
|
|
:func:`accumulate` p [,func] p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) --> 1 3 6 10 15``
|
2022-12-08 17:08:16 -04:00
|
|
|
|
:func:`batched` p, n (p0, p1, ..., p_n-1), ... ``batched('ABCDEFG', n=3) --> ABC DEF G``
|
2014-01-16 11:10:13 -04:00
|
|
|
|
:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F``
|
|
|
|
|
:func:`chain.from_iterable` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) --> A B C D E F``
|
|
|
|
|
:func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F``
|
|
|
|
|
:func:`dropwhile` pred, seq seq[n], seq[n+1], starting when pred fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1``
|
|
|
|
|
:func:`filterfalse` pred, seq elements of seq where pred(elem) is false ``filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8``
|
2017-09-25 05:21:06 -03:00
|
|
|
|
:func:`groupby` iterable[, key] sub-iterators grouped by value of key(v)
|
2014-01-16 11:10:13 -04:00
|
|
|
|
:func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) --> C D E F G``
|
2020-12-01 00:42:54 -04:00
|
|
|
|
:func:`pairwise` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') --> AB BC CD DE EF FG``
|
2014-01-16 11:10:13 -04:00
|
|
|
|
:func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000``
|
|
|
|
|
:func:`takewhile` pred, seq seq[0], seq[1], until pred fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4``
|
|
|
|
|
:func:`tee` it, n it1, it2, ... itn splits one iterator into n
|
|
|
|
|
:func:`zip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-``
|
|
|
|
|
============================ ============================ ================================================= =============================================================
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
2017-12-15 17:17:55 -04:00
|
|
|
|
**Combinatoric iterators:**
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
2009-04-10 16:43:50 -03:00
|
|
|
|
============================================== ==================== =============================================================
|
|
|
|
|
Iterator Arguments Results
|
|
|
|
|
============================================== ==================== =============================================================
|
|
|
|
|
:func:`product` p, q, ... [repeat=1] cartesian product, equivalent to a nested for-loop
|
|
|
|
|
:func:`permutations` p[, r] r-length tuples, all possible orderings, no repeated elements
|
2009-11-18 21:20:07 -04:00
|
|
|
|
:func:`combinations` p, r r-length tuples, in sorted order, no repeated elements
|
|
|
|
|
:func:`combinations_with_replacement` p, r r-length tuples, in sorted order, with repeated elements
|
2009-04-10 16:43:50 -03:00
|
|
|
|
============================================== ==================== =============================================================
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2019-03-21 12:28:49 -03:00
|
|
|
|
============================================== =============================================================
|
|
|
|
|
Examples Results
|
|
|
|
|
============================================== =============================================================
|
|
|
|
|
``product('ABCD', repeat=2)`` ``AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD``
|
|
|
|
|
``permutations('ABCD', 2)`` ``AB AC AD BA BC BD CA CB CD DA DB DC``
|
|
|
|
|
``combinations('ABCD', 2)`` ``AB AC AD BC BD CD``
|
|
|
|
|
``combinations_with_replacement('ABCD', 2)`` ``AA AB AC AD BB BC BD CC CD DD``
|
|
|
|
|
============================================== =============================================================
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. _itertools-functions:
|
|
|
|
|
|
|
|
|
|
Itertool functions
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
The following module functions all construct and return iterators. Some provide
|
|
|
|
|
streams of infinite length, so they should only be accessed by functions or
|
|
|
|
|
loops that truncate the stream.
|
|
|
|
|
|
2018-09-23 21:34:59 -03:00
|
|
|
|
.. function:: accumulate(iterable[, func, *, initial=None])
|
2010-12-01 18:50:36 -04:00
|
|
|
|
|
2014-04-15 23:28:40 -03:00
|
|
|
|
Make an iterator that returns accumulated sums, or accumulated
|
|
|
|
|
results of other binary functions (specified via the optional
|
2018-09-23 21:34:59 -03:00
|
|
|
|
*func* argument).
|
|
|
|
|
|
|
|
|
|
If *func* is supplied, it should be a function
|
2014-04-15 23:28:40 -03:00
|
|
|
|
of two arguments. Elements of the input *iterable* may be any type
|
|
|
|
|
that can be accepted as arguments to *func*. (For example, with
|
|
|
|
|
the default operation of addition, elements may be any addable
|
|
|
|
|
type including :class:`~decimal.Decimal` or
|
2018-09-23 21:34:59 -03:00
|
|
|
|
:class:`~fractions.Fraction`.)
|
|
|
|
|
|
|
|
|
|
Usually, the number of elements output matches the input iterable.
|
|
|
|
|
However, if the keyword argument *initial* is provided, the
|
|
|
|
|
accumulation leads off with the *initial* value so that the output
|
|
|
|
|
has one more element than the input iterable.
|
2010-12-01 18:50:36 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
2011-03-27 22:52:10 -03:00
|
|
|
|
|
2018-09-23 21:34:59 -03:00
|
|
|
|
def accumulate(iterable, func=operator.add, *, initial=None):
|
2010-12-01 18:50:36 -04:00
|
|
|
|
'Return running totals'
|
2010-12-02 22:09:34 -04:00
|
|
|
|
# accumulate([1,2,3,4,5]) --> 1 3 6 10 15
|
2018-09-23 21:34:59 -03:00
|
|
|
|
# accumulate([1,2,3,4,5], initial=100) --> 100 101 103 106 110 115
|
2011-03-27 22:52:10 -03:00
|
|
|
|
# accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
|
2010-12-02 22:09:34 -04:00
|
|
|
|
it = iter(iterable)
|
2018-09-23 21:34:59 -03:00
|
|
|
|
total = initial
|
|
|
|
|
if initial is None:
|
|
|
|
|
try:
|
|
|
|
|
total = next(it)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
return
|
2010-12-02 22:09:34 -04:00
|
|
|
|
yield total
|
|
|
|
|
for element in it:
|
2011-03-27 22:52:10 -03:00
|
|
|
|
total = func(total, element)
|
2010-12-02 22:09:34 -04:00
|
|
|
|
yield total
|
2010-12-01 18:50:36 -04:00
|
|
|
|
|
2011-04-21 15:09:28 -03:00
|
|
|
|
There are a number of uses for the *func* argument. It can be set to
|
|
|
|
|
:func:`min` for a running minimum, :func:`max` for a running maximum, or
|
|
|
|
|
:func:`operator.mul` for a running product. Amortization tables can be
|
2022-10-18 16:09:34 -03:00
|
|
|
|
built by accumulating interest and applying payments:
|
|
|
|
|
|
|
|
|
|
.. doctest::
|
2011-03-27 22:52:10 -03:00
|
|
|
|
|
|
|
|
|
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
|
|
|
|
|
>>> list(accumulate(data, operator.mul)) # running product
|
|
|
|
|
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
|
|
|
|
|
>>> list(accumulate(data, max)) # running maximum
|
|
|
|
|
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
|
|
|
|
|
|
2023-05-25 00:31:02 -03:00
|
|
|
|
# Amortize a 5% loan of 1000 with 10 annual payments of 90
|
|
|
|
|
>>> account_update = lambda bal, pmt: round(bal * 1.05) + pmt
|
|
|
|
|
>>> list(accumulate(repeat(-90, 10), account_update, initial=1_000))
|
|
|
|
|
[1000, 960, 918, 874, 828, 779, 728, 674, 618, 559, 497]
|
2011-03-27 22:52:10 -03:00
|
|
|
|
|
2013-10-12 20:04:17 -03:00
|
|
|
|
See :func:`functools.reduce` for a similar function that returns only the
|
|
|
|
|
final accumulated value.
|
|
|
|
|
|
2010-12-01 18:50:36 -04:00
|
|
|
|
.. versionadded:: 3.2
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2011-03-27 22:52:10 -03:00
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
|
Added the optional *func* parameter.
|
|
|
|
|
|
2018-09-23 21:34:59 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
Added the optional *initial* parameter.
|
|
|
|
|
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
2023-12-16 11:13:50 -04:00
|
|
|
|
.. function:: batched(iterable, n, *, strict=False)
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
2022-12-08 17:08:16 -04:00
|
|
|
|
Batch data from the *iterable* into tuples of length *n*. The last
|
2022-10-17 20:53:45 -03:00
|
|
|
|
batch may be shorter than *n*.
|
|
|
|
|
|
2023-12-16 11:13:50 -04:00
|
|
|
|
If *strict* is true, will raise a :exc:`ValueError` if the final
|
|
|
|
|
batch is shorter than *n*.
|
|
|
|
|
|
2022-12-08 17:08:16 -04:00
|
|
|
|
Loops over the input iterable and accumulates data into tuples up to
|
|
|
|
|
size *n*. The input is consumed lazily, just enough to fill a batch.
|
2022-10-17 20:53:45 -03:00
|
|
|
|
The result is yielded as soon as the batch is full or when the input
|
|
|
|
|
iterable is exhausted:
|
|
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
|
|
>>> flattened_data = ['roses', 'red', 'violets', 'blue', 'sugar', 'sweet']
|
|
|
|
|
>>> unflattened = list(batched(flattened_data, 2))
|
|
|
|
|
>>> unflattened
|
2022-12-08 17:08:16 -04:00
|
|
|
|
[('roses', 'red'), ('violets', 'blue'), ('sugar', 'sweet')]
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
|
|
|
|
>>> for batch in batched('ABCDEFG', 3):
|
|
|
|
|
... print(batch)
|
|
|
|
|
...
|
2022-12-08 17:08:16 -04:00
|
|
|
|
('A', 'B', 'C')
|
|
|
|
|
('D', 'E', 'F')
|
|
|
|
|
('G',)
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
|
|
|
|
Roughly equivalent to::
|
|
|
|
|
|
2023-12-16 11:13:50 -04:00
|
|
|
|
def batched(iterable, n, *, strict=False):
|
2022-10-17 20:53:45 -03:00
|
|
|
|
# batched('ABCDEFG', 3) --> ABC DEF G
|
|
|
|
|
if n < 1:
|
|
|
|
|
raise ValueError('n must be at least one')
|
|
|
|
|
it = iter(iterable)
|
2023-03-21 14:06:18 -03:00
|
|
|
|
while batch := tuple(islice(it, n)):
|
2023-12-16 11:13:50 -04:00
|
|
|
|
if strict and len(batch) != n:
|
|
|
|
|
raise ValueError('batched(): incomplete batch')
|
2022-10-17 20:53:45 -03:00
|
|
|
|
yield batch
|
|
|
|
|
|
2022-10-18 22:14:41 -03:00
|
|
|
|
.. versionadded:: 3.12
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
2023-12-16 11:13:50 -04:00
|
|
|
|
.. versionchanged:: 3.13
|
|
|
|
|
Added the *strict* option.
|
|
|
|
|
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. function:: chain(*iterables)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns elements from the first iterable until it is
|
|
|
|
|
exhausted, then proceeds to the next iterable, until all of the iterables are
|
|
|
|
|
exhausted. Used for treating consecutive sequences as a single sequence.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def chain(*iterables):
|
|
|
|
|
# chain('ABC', 'DEF') --> A B C D E F
|
|
|
|
|
for it in iterables:
|
|
|
|
|
for element in it:
|
|
|
|
|
yield element
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
2010-07-29 11:36:11 -03:00
|
|
|
|
.. classmethod:: chain.from_iterable(iterable)
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Alternate constructor for :func:`chain`. Gets chained inputs from a
|
2013-09-09 03:54:27 -03:00
|
|
|
|
single iterable argument that is evaluated lazily. Roughly equivalent to::
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def from_iterable(iterables):
|
|
|
|
|
# chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
|
|
|
|
|
for it in iterables:
|
|
|
|
|
for element in it:
|
|
|
|
|
yield element
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61209 | georg.brandl | 2008-03-03 21:37:55 +0100 (Mon, 03 Mar 2008) | 2 lines
There are now sixteen isfoo functions.
........
r61210 | georg.brandl | 2008-03-03 21:39:00 +0100 (Mon, 03 Mar 2008) | 2 lines
15 -> 16, the 2nd
........
r61211 | georg.brandl | 2008-03-03 22:22:47 +0100 (Mon, 03 Mar 2008) | 2 lines
Actually import itertools.
........
r61212 | georg.brandl | 2008-03-03 22:31:50 +0100 (Mon, 03 Mar 2008) | 2 lines
Expand a bit on genexp scopes.
........
r61213 | raymond.hettinger | 2008-03-03 23:04:55 +0100 (Mon, 03 Mar 2008) | 1 line
Remove dependency on itertools -- a simple genexp suffices.
........
r61214 | raymond.hettinger | 2008-03-03 23:19:58 +0100 (Mon, 03 Mar 2008) | 1 line
Issue 2226: Callable checked for the wrong abstract method.
........
r61217 | andrew.kuchling | 2008-03-04 01:40:32 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61218 | andrew.kuchling | 2008-03-04 02:30:10 +0100 (Tue, 04 Mar 2008) | 1 line
Grammar fix; markup fix
........
r61219 | andrew.kuchling | 2008-03-04 02:47:38 +0100 (Tue, 04 Mar 2008) | 1 line
Fix sentence fragment
........
r61220 | andrew.kuchling | 2008-03-04 02:48:26 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61221 | andrew.kuchling | 2008-03-04 02:49:37 +0100 (Tue, 04 Mar 2008) | 1 line
Add versionadded tags
........
r61222 | andrew.kuchling | 2008-03-04 02:50:32 +0100 (Tue, 04 Mar 2008) | 1 line
Thesis night results: add various items
........
r61224 | raymond.hettinger | 2008-03-04 05:17:08 +0100 (Tue, 04 Mar 2008) | 1 line
Beef-up docs and tests for itertools. Fix-up end-case for product().
........
r61225 | georg.brandl | 2008-03-04 08:25:54 +0100 (Tue, 04 Mar 2008) | 2 lines
Fix some patch attributions.
........
r61226 | georg.brandl | 2008-03-04 08:33:30 +0100 (Tue, 04 Mar 2008) | 2 lines
#2230: document that PyArg_* leaves addresses alone on error.
........
r61233 | neal.norwitz | 2008-03-04 17:22:46 +0100 (Tue, 04 Mar 2008) | 3 lines
Close the file before trying to remove the directory so it works on Windows.
As reported by Trent Nelson on python-dev.
........
r61234 | thomas.heller | 2008-03-04 21:09:11 +0100 (Tue, 04 Mar 2008) | 9 lines
Merged changes from libffi3-branch.
The bundled libffi copy is now in sync with the recently released
libffi3.0.4 version, apart from some small changes to
Modules/_ctypes/libffi/configure.ac.
I gave up on using libffi3 files on os x.
Instead, static configuration with files from pyobjc is used.
........
r61235 | thomas.heller | 2008-03-04 21:21:42 +0100 (Tue, 04 Mar 2008) | 1 line
Try to fix the build for PY_LINUX.
........
r61236 | fred.drake | 2008-03-04 22:14:04 +0100 (Tue, 04 Mar 2008) | 2 lines
fix typo
........
r61237 | raymond.hettinger | 2008-03-04 23:29:44 +0100 (Tue, 04 Mar 2008) | 1 line
Fix refleak in chain().
........
2008-03-04 19:39:23 -04:00
|
|
|
|
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
.. function:: combinations(iterable, r)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Return *r* length subsequences of elements from the input *iterable*.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2020-05-28 16:56:43 -03:00
|
|
|
|
The combination tuples are emitted in lexicographic ordering according to
|
|
|
|
|
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
2022-10-18 16:09:34 -03:00
|
|
|
|
the output tuples will be produced in sorted order.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Elements are treated as unique based on their position, not on their
|
2022-10-18 16:09:34 -03:00
|
|
|
|
value. So if the input elements are unique, there will be no repeated
|
2010-12-01 19:45:20 -04:00
|
|
|
|
values in each combination.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
|
|
|
|
def combinations(iterable, r):
|
2008-03-12 23:39:40 -03:00
|
|
|
|
# combinations('ABCD', 2) --> AB AC AD BC BD CD
|
|
|
|
|
# combinations(range(4), 3) --> 012 013 023 123
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
pool = tuple(iterable)
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines
Speed up this test by about 99%. Remove sleeps and replace with events.
(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase. These tests were taking 3 seconds which
is what the timeout was set to.
........
r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line
The contains function raised a gcc warning. The new code is copied straight from py3k.
........
r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
........
r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines
Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg
My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too.
........
r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines
#2194: fix some typos.
........
r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line
Add itertools.combinations().
........
r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line
One too many decrefs.
........
r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line
Larger test range
........
r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line
Simply the sample code for combinations().
........
2008-02-28 07:19:05 -04:00
|
|
|
|
n = len(pool)
|
2009-01-08 17:01:54 -04:00
|
|
|
|
if r > n:
|
|
|
|
|
return
|
|
|
|
|
indices = list(range(r))
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
yield tuple(pool[i] for i in indices)
|
2009-02-18 16:56:51 -04:00
|
|
|
|
while True:
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines
Speed up this test by about 99%. Remove sleeps and replace with events.
(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase. These tests were taking 3 seconds which
is what the timeout was set to.
........
r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line
The contains function raised a gcc warning. The new code is copied straight from py3k.
........
r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
........
r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines
Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg
My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too.
........
r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines
#2194: fix some typos.
........
r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line
Add itertools.combinations().
........
r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line
One too many decrefs.
........
r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line
Larger test range
........
r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line
Simply the sample code for combinations().
........
2008-02-28 07:19:05 -04:00
|
|
|
|
for i in reversed(range(r)):
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
if indices[i] != i + n - r:
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
break
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines
Speed up this test by about 99%. Remove sleeps and replace with events.
(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase. These tests were taking 3 seconds which
is what the timeout was set to.
........
r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line
The contains function raised a gcc warning. The new code is copied straight from py3k.
........
r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
........
r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines
Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg
My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too.
........
r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines
#2194: fix some typos.
........
r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line
Add itertools.combinations().
........
r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line
One too many decrefs.
........
r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line
Larger test range
........
r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line
Simply the sample code for combinations().
........
2008-02-28 07:19:05 -04:00
|
|
|
|
else:
|
|
|
|
|
return
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
indices[i] += 1
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines
Speed up this test by about 99%. Remove sleeps and replace with events.
(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase. These tests were taking 3 seconds which
is what the timeout was set to.
........
r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line
The contains function raised a gcc warning. The new code is copied straight from py3k.
........
r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
........
r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines
Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg
My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too.
........
r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines
#2194: fix some typos.
........
r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line
Add itertools.combinations().
........
r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line
One too many decrefs.
........
r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line
Larger test range
........
r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line
Simply the sample code for combinations().
........
2008-02-28 07:19:05 -04:00
|
|
|
|
for j in range(i+1, r):
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
indices[j] = indices[j-1] + 1
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The code for :func:`combinations` can be also expressed as a subsequence
|
|
|
|
|
of :func:`permutations` after filtering entries where the elements are not
|
|
|
|
|
in sorted order (according to their position in the input pool)::
|
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61209 | georg.brandl | 2008-03-03 21:37:55 +0100 (Mon, 03 Mar 2008) | 2 lines
There are now sixteen isfoo functions.
........
r61210 | georg.brandl | 2008-03-03 21:39:00 +0100 (Mon, 03 Mar 2008) | 2 lines
15 -> 16, the 2nd
........
r61211 | georg.brandl | 2008-03-03 22:22:47 +0100 (Mon, 03 Mar 2008) | 2 lines
Actually import itertools.
........
r61212 | georg.brandl | 2008-03-03 22:31:50 +0100 (Mon, 03 Mar 2008) | 2 lines
Expand a bit on genexp scopes.
........
r61213 | raymond.hettinger | 2008-03-03 23:04:55 +0100 (Mon, 03 Mar 2008) | 1 line
Remove dependency on itertools -- a simple genexp suffices.
........
r61214 | raymond.hettinger | 2008-03-03 23:19:58 +0100 (Mon, 03 Mar 2008) | 1 line
Issue 2226: Callable checked for the wrong abstract method.
........
r61217 | andrew.kuchling | 2008-03-04 01:40:32 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61218 | andrew.kuchling | 2008-03-04 02:30:10 +0100 (Tue, 04 Mar 2008) | 1 line
Grammar fix; markup fix
........
r61219 | andrew.kuchling | 2008-03-04 02:47:38 +0100 (Tue, 04 Mar 2008) | 1 line
Fix sentence fragment
........
r61220 | andrew.kuchling | 2008-03-04 02:48:26 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61221 | andrew.kuchling | 2008-03-04 02:49:37 +0100 (Tue, 04 Mar 2008) | 1 line
Add versionadded tags
........
r61222 | andrew.kuchling | 2008-03-04 02:50:32 +0100 (Tue, 04 Mar 2008) | 1 line
Thesis night results: add various items
........
r61224 | raymond.hettinger | 2008-03-04 05:17:08 +0100 (Tue, 04 Mar 2008) | 1 line
Beef-up docs and tests for itertools. Fix-up end-case for product().
........
r61225 | georg.brandl | 2008-03-04 08:25:54 +0100 (Tue, 04 Mar 2008) | 2 lines
Fix some patch attributions.
........
r61226 | georg.brandl | 2008-03-04 08:33:30 +0100 (Tue, 04 Mar 2008) | 2 lines
#2230: document that PyArg_* leaves addresses alone on error.
........
r61233 | neal.norwitz | 2008-03-04 17:22:46 +0100 (Tue, 04 Mar 2008) | 3 lines
Close the file before trying to remove the directory so it works on Windows.
As reported by Trent Nelson on python-dev.
........
r61234 | thomas.heller | 2008-03-04 21:09:11 +0100 (Tue, 04 Mar 2008) | 9 lines
Merged changes from libffi3-branch.
The bundled libffi copy is now in sync with the recently released
libffi3.0.4 version, apart from some small changes to
Modules/_ctypes/libffi/configure.ac.
I gave up on using libffi3 files on os x.
Instead, static configuration with files from pyobjc is used.
........
r61235 | thomas.heller | 2008-03-04 21:21:42 +0100 (Tue, 04 Mar 2008) | 1 line
Try to fix the build for PY_LINUX.
........
r61236 | fred.drake | 2008-03-04 22:14:04 +0100 (Tue, 04 Mar 2008) | 2 lines
fix typo
........
r61237 | raymond.hettinger | 2008-03-04 23:29:44 +0100 (Tue, 04 Mar 2008) | 1 line
Fix refleak in chain().
........
2008-03-04 19:39:23 -04:00
|
|
|
|
|
|
|
|
|
def combinations(iterable, r):
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
for indices in permutations(range(n), r):
|
|
|
|
|
if sorted(indices) == list(indices):
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n``
|
|
|
|
|
or zero when ``r > n``.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2009-01-27 00:20:44 -04:00
|
|
|
|
.. function:: combinations_with_replacement(iterable, r)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Return *r* length subsequences of elements from the input *iterable*
|
|
|
|
|
allowing individual elements to be repeated more than once.
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2020-05-28 16:56:43 -03:00
|
|
|
|
The combination tuples are emitted in lexicographic ordering according to
|
|
|
|
|
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
2022-10-18 16:09:34 -03:00
|
|
|
|
the output tuples will be produced in sorted order.
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Elements are treated as unique based on their position, not on their
|
|
|
|
|
value. So if the input elements are unique, the generated combinations
|
|
|
|
|
will also be unique.
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
|
|
|
|
def combinations_with_replacement(iterable, r):
|
|
|
|
|
# combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
if not n and r:
|
|
|
|
|
return
|
|
|
|
|
indices = [0] * r
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
2009-02-18 16:56:51 -04:00
|
|
|
|
while True:
|
2009-01-27 00:20:44 -04:00
|
|
|
|
for i in reversed(range(r)):
|
|
|
|
|
if indices[i] != n - 1:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
return
|
|
|
|
|
indices[i:] = [indices[i] + 1] * (r - i)
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The code for :func:`combinations_with_replacement` can be also expressed as
|
|
|
|
|
a subsequence of :func:`product` after filtering entries where the elements
|
|
|
|
|
are not in sorted order (according to their position in the input pool)::
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
|
|
|
|
def combinations_with_replacement(iterable, r):
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
for indices in product(range(n), repeat=r):
|
|
|
|
|
if sorted(indices) == list(indices):
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The number of items returned is ``(n+r-1)! / r! / (n-1)!`` when ``n > 0``.
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
.. versionadded:: 3.1
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2010-08-17 12:07:14 -03:00
|
|
|
|
|
2009-01-25 22:56:58 -04:00
|
|
|
|
.. function:: compress(data, selectors)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that filters elements from *data* returning only those that
|
|
|
|
|
have a corresponding element in *selectors* that evaluates to ``True``.
|
|
|
|
|
Stops when either the *data* or *selectors* iterables has been exhausted.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
2009-01-25 22:56:58 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def compress(data, selectors):
|
|
|
|
|
# compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
|
|
|
|
|
return (d for d, s in zip(data, selectors) if s)
|
2009-01-25 22:56:58 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
.. versionadded:: 3.1
|
2009-01-25 22:56:58 -04:00
|
|
|
|
|
|
|
|
|
|
2009-02-14 00:21:49 -04:00
|
|
|
|
.. function:: count(start=0, step=1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2013-06-21 09:00:58 -03:00
|
|
|
|
Make an iterator that returns evenly spaced values starting with number *start*. Often
|
2010-12-01 19:45:20 -04:00
|
|
|
|
used as an argument to :func:`map` to generate consecutive data points.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Also, used with :func:`zip` to add sequence numbers. Roughly equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def count(start=0, step=1):
|
|
|
|
|
# count(10) --> 10 11 12 13 14 ...
|
2022-09-13 23:10:47 -03:00
|
|
|
|
# count(2.5, 0.5) --> 2.5 3.0 3.5 ...
|
2010-12-01 19:45:20 -04:00
|
|
|
|
n = start
|
|
|
|
|
while True:
|
|
|
|
|
yield n
|
|
|
|
|
n += step
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
When counting with floating point numbers, better accuracy can sometimes be
|
|
|
|
|
achieved by substituting multiplicative code such as: ``(start + step * i
|
|
|
|
|
for i in count())``.
|
2009-06-16 22:40:52 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
.. versionchanged:: 3.1
|
|
|
|
|
Added *step* argument and allowed non-integer arguments.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. function:: cycle(iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator returning elements from the iterable and saving a copy of each.
|
|
|
|
|
When the iterable is exhausted, return elements from the saved copy. Repeats
|
2016-05-28 04:17:54 -03:00
|
|
|
|
indefinitely. Roughly equivalent to::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def cycle(iterable):
|
|
|
|
|
# cycle('ABCD') --> A B C D A B C D A B C D ...
|
|
|
|
|
saved = []
|
|
|
|
|
for element in iterable:
|
|
|
|
|
yield element
|
|
|
|
|
saved.append(element)
|
|
|
|
|
while saved:
|
|
|
|
|
for element in saved:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
yield element
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Note, this member of the toolkit may require significant auxiliary storage
|
|
|
|
|
(depending on the length of the iterable).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: dropwhile(predicate, iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that drops elements from the iterable as long as the predicate
|
|
|
|
|
is true; afterwards, returns every element. Note, the iterator does not produce
|
|
|
|
|
*any* output until the predicate first becomes false, so it may have a lengthy
|
2016-05-28 04:17:54 -03:00
|
|
|
|
start-up time. Roughly equivalent to::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def dropwhile(predicate, iterable):
|
|
|
|
|
# dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
|
|
|
|
|
iterable = iter(iterable)
|
|
|
|
|
for x in iterable:
|
|
|
|
|
if not predicate(x):
|
|
|
|
|
yield x
|
|
|
|
|
break
|
|
|
|
|
for x in iterable:
|
|
|
|
|
yield x
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-01-27 00:42:48 -04:00
|
|
|
|
.. function:: filterfalse(predicate, iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that filters elements from iterable returning only those for
|
2023-02-24 12:13:05 -04:00
|
|
|
|
which the predicate is false. If *predicate* is ``None``, return the items
|
2016-05-28 04:17:54 -03:00
|
|
|
|
that are false. Roughly equivalent to::
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def filterfalse(predicate, iterable):
|
|
|
|
|
# filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
|
|
|
|
|
if predicate is None:
|
|
|
|
|
predicate = bool
|
|
|
|
|
for x in iterable:
|
|
|
|
|
if not predicate(x):
|
|
|
|
|
yield x
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: groupby(iterable, key=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns consecutive keys and groups from the *iterable*.
|
|
|
|
|
The *key* is a function computing a key value for each element. If not
|
|
|
|
|
specified or is ``None``, *key* defaults to an identity function and returns
|
|
|
|
|
the element unchanged. Generally, the iterable needs to already be sorted on
|
|
|
|
|
the same key function.
|
|
|
|
|
|
|
|
|
|
The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It
|
|
|
|
|
generates a break or new group every time the value of the key function changes
|
|
|
|
|
(which is why it is usually necessary to have sorted the data using the same key
|
|
|
|
|
function). That behavior differs from SQL's GROUP BY which aggregates common
|
|
|
|
|
elements regardless of their input order.
|
|
|
|
|
|
|
|
|
|
The returned group is itself an iterator that shares the underlying iterable
|
|
|
|
|
with :func:`groupby`. Because the source is shared, when the :func:`groupby`
|
|
|
|
|
object is advanced, the previous group is no longer visible. So, if that data
|
|
|
|
|
is needed later, it should be stored as a list::
|
|
|
|
|
|
|
|
|
|
groups = []
|
|
|
|
|
uniquekeys = []
|
|
|
|
|
data = sorted(data, key=keyfunc)
|
|
|
|
|
for k, g in groupby(data, keyfunc):
|
|
|
|
|
groups.append(list(g)) # Store group iterator as a list
|
|
|
|
|
uniquekeys.append(k)
|
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
:func:`groupby` is roughly equivalent to::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
class groupby:
|
|
|
|
|
# [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
|
|
|
|
|
# [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def __init__(self, iterable, key=None):
|
|
|
|
|
if key is None:
|
|
|
|
|
key = lambda x: x
|
|
|
|
|
self.keyfunc = key
|
|
|
|
|
self.it = iter(iterable)
|
|
|
|
|
self.tgtkey = self.currkey = self.currvalue = object()
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def __iter__(self):
|
|
|
|
|
return self
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def __next__(self):
|
2017-09-24 07:36:11 -03:00
|
|
|
|
self.id = object()
|
2010-12-01 19:45:20 -04:00
|
|
|
|
while self.currkey == self.tgtkey:
|
|
|
|
|
self.currvalue = next(self.it) # Exit on StopIteration
|
|
|
|
|
self.currkey = self.keyfunc(self.currvalue)
|
|
|
|
|
self.tgtkey = self.currkey
|
2017-09-24 07:36:11 -03:00
|
|
|
|
return (self.currkey, self._grouper(self.tgtkey, self.id))
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2017-09-24 07:36:11 -03:00
|
|
|
|
def _grouper(self, tgtkey, id):
|
|
|
|
|
while self.id is id and self.currkey == tgtkey:
|
2010-12-01 19:45:20 -04:00
|
|
|
|
yield self.currvalue
|
2014-11-23 01:56:23 -04:00
|
|
|
|
try:
|
|
|
|
|
self.currvalue = next(self.it)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
return
|
2010-12-01 19:45:20 -04:00
|
|
|
|
self.currkey = self.keyfunc(self.currvalue)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
2012-09-14 00:32:35 -03:00
|
|
|
|
.. function:: islice(iterable, stop)
|
|
|
|
|
islice(iterable, start, stop[, step])
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns selected elements from the iterable. If *start* is
|
|
|
|
|
non-zero, then elements from the iterable are skipped until start is reached.
|
|
|
|
|
Afterward, elements are returned consecutively unless *step* is set higher than
|
|
|
|
|
one which results in items being skipped. If *stop* is ``None``, then iteration
|
|
|
|
|
continues until the iterator is exhausted, if at all; otherwise, it stops at the
|
2022-10-18 16:09:34 -03:00
|
|
|
|
specified position.
|
|
|
|
|
|
|
|
|
|
If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
|
|
|
|
|
then the step defaults to one.
|
|
|
|
|
|
|
|
|
|
Unlike regular slicing, :func:`islice` does not support negative values for
|
|
|
|
|
*start*, *stop*, or *step*. Can be used to extract related fields from
|
|
|
|
|
data where the internal structure has been flattened (for example, a
|
|
|
|
|
multi-line report may list a name field on every third line).
|
|
|
|
|
|
|
|
|
|
Roughly equivalent to::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def islice(iterable, *args):
|
|
|
|
|
# islice('ABCDEFG', 2) --> A B
|
|
|
|
|
# islice('ABCDEFG', 2, 4) --> C D
|
|
|
|
|
# islice('ABCDEFG', 2, None) --> C D E F G
|
|
|
|
|
# islice('ABCDEFG', 0, None, 2) --> A C E G
|
|
|
|
|
s = slice(*args)
|
2018-03-26 22:29:33 -03:00
|
|
|
|
start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
|
|
|
|
|
it = iter(range(start, stop, step))
|
2014-11-23 01:56:23 -04:00
|
|
|
|
try:
|
|
|
|
|
nexti = next(it)
|
|
|
|
|
except StopIteration:
|
2018-03-26 22:29:33 -03:00
|
|
|
|
# Consume *iterable* up to the *start* position.
|
|
|
|
|
for i, element in zip(range(start), iterable):
|
|
|
|
|
pass
|
2014-11-23 01:56:23 -04:00
|
|
|
|
return
|
2018-03-26 22:29:33 -03:00
|
|
|
|
try:
|
|
|
|
|
for i, element in enumerate(iterable):
|
|
|
|
|
if i == nexti:
|
|
|
|
|
yield element
|
|
|
|
|
nexti = next(it)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
# Consume to *stop*.
|
|
|
|
|
for i, element in zip(range(i + 1, stop), iterable):
|
|
|
|
|
pass
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2020-12-01 00:42:54 -04:00
|
|
|
|
.. function:: pairwise(iterable)
|
|
|
|
|
|
|
|
|
|
Return successive overlapping pairs taken from the input *iterable*.
|
|
|
|
|
|
|
|
|
|
The number of 2-tuples in the output iterator will be one fewer than the
|
|
|
|
|
number of inputs. It will be empty if the input iterable has fewer than
|
|
|
|
|
two values.
|
|
|
|
|
|
|
|
|
|
Roughly equivalent to::
|
|
|
|
|
|
|
|
|
|
def pairwise(iterable):
|
|
|
|
|
# pairwise('ABCDEFG') --> AB BC CD DE EF FG
|
|
|
|
|
a, b = tee(iterable)
|
|
|
|
|
next(b, None)
|
|
|
|
|
return zip(a, b)
|
|
|
|
|
|
2021-09-07 14:29:00 -03:00
|
|
|
|
.. versionadded:: 3.10
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: permutations(iterable, r=None)
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Return successive *r* length permutations of elements in the *iterable*.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
If *r* is not specified or is ``None``, then *r* defaults to the length
|
|
|
|
|
of the *iterable* and all possible full-length permutations
|
|
|
|
|
are generated.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2022-10-18 16:09:34 -03:00
|
|
|
|
The permutation tuples are emitted in lexicographic order according to
|
2020-05-28 16:56:43 -03:00
|
|
|
|
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
2022-10-18 16:09:34 -03:00
|
|
|
|
the output tuples will be produced in sorted order.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Elements are treated as unique based on their position, not on their
|
2022-10-18 16:09:34 -03:00
|
|
|
|
value. So if the input elements are unique, there will be no repeated
|
|
|
|
|
values within a permutation.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
|
|
|
|
|
def permutations(iterable, r=None):
|
2008-03-12 23:39:40 -03:00
|
|
|
|
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
|
|
|
|
|
# permutations(range(3)) --> 012 021 102 120 201 210
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
r = n if r is None else r
|
2009-01-08 17:01:54 -04:00
|
|
|
|
if r > n:
|
|
|
|
|
return
|
|
|
|
|
indices = list(range(n))
|
2011-12-25 12:25:45 -04:00
|
|
|
|
cycles = list(range(n, n-r, -1))
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
yield tuple(pool[i] for i in indices[:r])
|
|
|
|
|
while n:
|
|
|
|
|
for i in reversed(range(r)):
|
|
|
|
|
cycles[i] -= 1
|
|
|
|
|
if cycles[i] == 0:
|
|
|
|
|
indices[i:] = indices[i+1:] + indices[i:i+1]
|
|
|
|
|
cycles[i] = n - i
|
|
|
|
|
else:
|
|
|
|
|
j = cycles[i]
|
|
|
|
|
indices[i], indices[-j] = indices[-j], indices[i]
|
|
|
|
|
yield tuple(pool[i] for i in indices[:r])
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
return
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The code for :func:`permutations` can be also expressed as a subsequence of
|
|
|
|
|
:func:`product`, filtered to exclude entries with repeated elements (those
|
|
|
|
|
from the same position in the input pool)::
|
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61209 | georg.brandl | 2008-03-03 21:37:55 +0100 (Mon, 03 Mar 2008) | 2 lines
There are now sixteen isfoo functions.
........
r61210 | georg.brandl | 2008-03-03 21:39:00 +0100 (Mon, 03 Mar 2008) | 2 lines
15 -> 16, the 2nd
........
r61211 | georg.brandl | 2008-03-03 22:22:47 +0100 (Mon, 03 Mar 2008) | 2 lines
Actually import itertools.
........
r61212 | georg.brandl | 2008-03-03 22:31:50 +0100 (Mon, 03 Mar 2008) | 2 lines
Expand a bit on genexp scopes.
........
r61213 | raymond.hettinger | 2008-03-03 23:04:55 +0100 (Mon, 03 Mar 2008) | 1 line
Remove dependency on itertools -- a simple genexp suffices.
........
r61214 | raymond.hettinger | 2008-03-03 23:19:58 +0100 (Mon, 03 Mar 2008) | 1 line
Issue 2226: Callable checked for the wrong abstract method.
........
r61217 | andrew.kuchling | 2008-03-04 01:40:32 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61218 | andrew.kuchling | 2008-03-04 02:30:10 +0100 (Tue, 04 Mar 2008) | 1 line
Grammar fix; markup fix
........
r61219 | andrew.kuchling | 2008-03-04 02:47:38 +0100 (Tue, 04 Mar 2008) | 1 line
Fix sentence fragment
........
r61220 | andrew.kuchling | 2008-03-04 02:48:26 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61221 | andrew.kuchling | 2008-03-04 02:49:37 +0100 (Tue, 04 Mar 2008) | 1 line
Add versionadded tags
........
r61222 | andrew.kuchling | 2008-03-04 02:50:32 +0100 (Tue, 04 Mar 2008) | 1 line
Thesis night results: add various items
........
r61224 | raymond.hettinger | 2008-03-04 05:17:08 +0100 (Tue, 04 Mar 2008) | 1 line
Beef-up docs and tests for itertools. Fix-up end-case for product().
........
r61225 | georg.brandl | 2008-03-04 08:25:54 +0100 (Tue, 04 Mar 2008) | 2 lines
Fix some patch attributions.
........
r61226 | georg.brandl | 2008-03-04 08:33:30 +0100 (Tue, 04 Mar 2008) | 2 lines
#2230: document that PyArg_* leaves addresses alone on error.
........
r61233 | neal.norwitz | 2008-03-04 17:22:46 +0100 (Tue, 04 Mar 2008) | 3 lines
Close the file before trying to remove the directory so it works on Windows.
As reported by Trent Nelson on python-dev.
........
r61234 | thomas.heller | 2008-03-04 21:09:11 +0100 (Tue, 04 Mar 2008) | 9 lines
Merged changes from libffi3-branch.
The bundled libffi copy is now in sync with the recently released
libffi3.0.4 version, apart from some small changes to
Modules/_ctypes/libffi/configure.ac.
I gave up on using libffi3 files on os x.
Instead, static configuration with files from pyobjc is used.
........
r61235 | thomas.heller | 2008-03-04 21:21:42 +0100 (Tue, 04 Mar 2008) | 1 line
Try to fix the build for PY_LINUX.
........
r61236 | fred.drake | 2008-03-04 22:14:04 +0100 (Tue, 04 Mar 2008) | 2 lines
fix typo
........
r61237 | raymond.hettinger | 2008-03-04 23:29:44 +0100 (Tue, 04 Mar 2008) | 1 line
Fix refleak in chain().
........
2008-03-04 19:39:23 -04:00
|
|
|
|
|
|
|
|
|
def permutations(iterable, r=None):
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
r = n if r is None else r
|
|
|
|
|
for indices in product(range(n), repeat=r):
|
|
|
|
|
if len(set(indices)) == r:
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n``
|
|
|
|
|
or zero when ``r > n``.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: product(*iterables, repeat=1)
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Cartesian product of input iterables.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to nested for-loops in a generator expression. For example,
|
2010-12-01 19:45:20 -04:00
|
|
|
|
``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The nested loops cycle like an odometer with the rightmost element advancing
|
|
|
|
|
on every iteration. This pattern creates a lexicographic ordering so that if
|
|
|
|
|
the input's iterables are sorted, the product tuples are emitted in sorted
|
|
|
|
|
order.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
To compute the product of an iterable with itself, specify the number of
|
|
|
|
|
repetitions with the optional *repeat* keyword argument. For example,
|
|
|
|
|
``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61098 | jeffrey.yasskin | 2008-02-28 05:45:36 +0100 (Thu, 28 Feb 2008) | 7 lines
Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
which forbids constructing types that have it set. The effect is to speed
./python.exe -m timeit -s 'import abc' -s 'class Foo(object): __metaclass__ = abc.ABCMeta' 'Foo()'
up from 2.5us to 0.201us. This fixes issue 1762.
........
r61099 | jeffrey.yasskin | 2008-02-28 06:53:18 +0100 (Thu, 28 Feb 2008) | 3 lines
Speed test_socketserver up from 28.739s to 0.226s, simplify the logic, and make
sure all tests run even if some fail.
........
r61100 | jeffrey.yasskin | 2008-02-28 07:09:19 +0100 (Thu, 28 Feb 2008) | 21 lines
Thread.start() used sleep(0.000001) to make sure it didn't return before the
new thread had started. At least on my MacBook Pro, that wound up sleeping for
a full 10ms (probably 1 jiffy). By using an Event instead, we can be absolutely
certain that the thread has started, and return more quickly (217us).
Before:
$ ./python.exe -m timeit -s 'from threading import Thread' 't = Thread(); t.start(); t.join()'
100 loops, best of 3: 10.3 msec per loop
$ ./python.exe -m timeit -s 'from threading import Thread; t = Thread()' 't.isAlive()'
1000000 loops, best of 3: 0.47 usec per loop
After:
$ ./python.exe -m timeit -s 'from threading import Thread' 't = Thread(); t.start(); t.join()'
1000 loops, best of 3: 217 usec per loop
$ ./python.exe -m timeit -s 'from threading import Thread; t = Thread()' 't.isAlive()'
1000000 loops, best of 3: 0.86 usec per loop
To be fair, the 10ms isn't CPU time, and other threads including the spawned
one get to run during it. There are also some slightly more complicated ways to
get back the .4us in isAlive() if we want.
........
r61101 | raymond.hettinger | 2008-02-28 10:23:48 +0100 (Thu, 28 Feb 2008) | 1 line
Add repeat keyword argument to itertools.product().
........
r61102 | christian.heimes | 2008-02-28 12:18:49 +0100 (Thu, 28 Feb 2008) | 1 line
The empty tuple is usually a singleton with a much higher refcnt than 1
........
2008-02-28 08:27:11 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
This function is roughly equivalent to the following code, except that the
|
2010-12-01 19:45:20 -04:00
|
|
|
|
actual implementation does not build up intermediate results in memory::
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def product(*args, repeat=1):
|
|
|
|
|
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
|
|
|
|
|
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
|
|
|
|
|
pools = [tuple(pool) for pool in args] * repeat
|
|
|
|
|
result = [[]]
|
|
|
|
|
for pool in pools:
|
|
|
|
|
result = [x+[y] for x in result for y in pool]
|
|
|
|
|
for prod in result:
|
|
|
|
|
yield tuple(prod)
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2020-05-28 13:46:22 -03:00
|
|
|
|
Before :func:`product` runs, it completely consumes the input iterables,
|
|
|
|
|
keeping pools of values in memory to generate the products. Accordingly,
|
2020-12-17 16:24:43 -04:00
|
|
|
|
it is only useful with finite inputs.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2009-06-01 16:16:52 -03:00
|
|
|
|
.. function:: repeat(object[, times])
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns *object* over and over again. Runs indefinitely
|
2022-10-18 16:09:34 -03:00
|
|
|
|
unless the *times* argument is specified.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
|
|
|
|
|
Roughly equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def repeat(object, times=None):
|
|
|
|
|
# repeat(10, 3) --> 10 10 10
|
|
|
|
|
if times is None:
|
|
|
|
|
while True:
|
|
|
|
|
yield object
|
|
|
|
|
else:
|
|
|
|
|
for i in range(times):
|
|
|
|
|
yield object
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-02-01 13:07:40 -04:00
|
|
|
|
A common use for *repeat* is to supply a stream of constant values to *map*
|
2022-10-18 16:09:34 -03:00
|
|
|
|
or *zip*:
|
|
|
|
|
|
|
|
|
|
.. doctest::
|
2012-02-01 13:07:40 -04:00
|
|
|
|
|
|
|
|
|
>>> list(map(pow, range(10), repeat(2)))
|
|
|
|
|
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. function:: starmap(function, iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that computes the function using arguments obtained from
|
|
|
|
|
the iterable. Used instead of :func:`map` when argument parameters are already
|
2022-10-18 16:09:34 -03:00
|
|
|
|
grouped in tuples from a single iterable (when the data has been
|
|
|
|
|
"pre-zipped").
|
|
|
|
|
|
|
|
|
|
The difference between :func:`map` and :func:`starmap` parallels the
|
|
|
|
|
distinction between ``function(a,b)`` and ``function(*c)``. Roughly
|
|
|
|
|
equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def starmap(function, iterable):
|
|
|
|
|
# starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
|
|
|
|
|
for args in iterable:
|
|
|
|
|
yield function(*args)
|
Merged revisions 59985-60000,60002,60005-60007,60009-60042 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59987 | raymond.hettinger | 2008-01-15 21:52:42 +0100 (Tue, 15 Jan 2008) | 1 line
Refactor if/elif chain for clarity and speed. Remove dependency on subclasses having to implement _empty and _full.
........
r59988 | raymond.hettinger | 2008-01-15 22:22:47 +0100 (Tue, 15 Jan 2008) | 1 line
Fix-up half-written paragraph in the docs
........
r59989 | amaury.forgeotdarc | 2008-01-15 22:25:11 +0100 (Tue, 15 Jan 2008) | 3 lines
test_doctest fails since r59984.
Not sure if these are the correct values, but save_stdout has to be set before its usage...
........
r59992 | andrew.kuchling | 2008-01-16 01:32:03 +0100 (Wed, 16 Jan 2008) | 1 line
Docstring typos
........
r59993 | andrew.kuchling | 2008-01-16 04:17:25 +0100 (Wed, 16 Jan 2008) | 1 line
Add PEP 3141 section
........
r59998 | andrew.kuchling | 2008-01-16 14:01:51 +0100 (Wed, 16 Jan 2008) | 1 line
Markup fix
........
r59999 | georg.brandl | 2008-01-16 17:56:29 +0100 (Wed, 16 Jan 2008) | 2 lines
Fix MSDN library URL. (#1854)
........
r60006 | georg.brandl | 2008-01-16 21:27:56 +0100 (Wed, 16 Jan 2008) | 3 lines
Add Python-specific content to Doc dir. Update configuration file
to work with the newest Sphinx.
........
r60007 | georg.brandl | 2008-01-16 21:29:00 +0100 (Wed, 16 Jan 2008) | 2 lines
Doc build should work with 2.4 now.
........
r60009 | raymond.hettinger | 2008-01-17 00:38:16 +0100 (Thu, 17 Jan 2008) | 1 line
Minor wordsmithing.
........
r60010 | raymond.hettinger | 2008-01-17 00:40:45 +0100 (Thu, 17 Jan 2008) | 1 line
Add queues will alternative fetch orders (priority based and stack based).
........
r60011 | raymond.hettinger | 2008-01-17 00:49:35 +0100 (Thu, 17 Jan 2008) | 1 line
Add news entry.
........
r60013 | raymond.hettinger | 2008-01-17 04:02:14 +0100 (Thu, 17 Jan 2008) | 1 line
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
........
r60015 | gregory.p.smith | 2008-01-17 08:43:20 +0100 (Thu, 17 Jan 2008) | 3 lines
Comply with RFC 3207.
Fixes issue 829951 - http://bugs.python.org/issue829951
........
r60018 | gregory.p.smith | 2008-01-17 09:03:17 +0100 (Thu, 17 Jan 2008) | 2 lines
entry for r60015
........
r60019 | raymond.hettinger | 2008-01-17 09:07:05 +0100 (Thu, 17 Jan 2008) | 1 line
Note versionadded.
........
r60020 | gregory.p.smith | 2008-01-17 09:35:49 +0100 (Thu, 17 Jan 2008) | 8 lines
Fixes (accepts patch) issue1339 - http://bugs.python.org/issue1339
- Factor out the duplication of EHLO/HELO in login() and sendmail() to
a new function, ehlo_or_helo_if_needed().
- Use ehlo_or_helo_if_needed() in starttls()
- Check for the starttls exception in starttls() in the same way as
login() checks for the auth extension.
Contributed by Bill Fenner.
........
r60021 | andrew.kuchling | 2008-01-17 13:00:15 +0100 (Thu, 17 Jan 2008) | 1 line
Revise 3141 section a bit; add some Windows items
........
r60022 | brett.cannon | 2008-01-17 19:45:10 +0100 (Thu, 17 Jan 2008) | 2 lines
Fix a function pointer declaration to silence the compiler.
........
r60024 | raymond.hettinger | 2008-01-17 20:31:38 +0100 (Thu, 17 Jan 2008) | 1 line
Issue #1861: Add read-only attribute listing upcoming events in the order they will be run.
........
r60025 | andrew.kuchling | 2008-01-17 20:49:24 +0100 (Thu, 17 Jan 2008) | 1 line
Correction from Jordan Lewis: halfdelay() uses tenths of a second, not milliseconds
........
r60026 | raymond.hettinger | 2008-01-17 23:27:49 +0100 (Thu, 17 Jan 2008) | 1 line
Add advice on choosing between scheduler and threading.Timer().
........
r60028 | christian.heimes | 2008-01-18 00:01:44 +0100 (Fri, 18 Jan 2008) | 2 lines
Updated new property syntax. An elaborate example for subclassing and the getter was missing.
Added comment about VS 2008 and PGO builds.
........
r60029 | raymond.hettinger | 2008-01-18 00:32:01 +0100 (Fri, 18 Jan 2008) | 1 line
Fix-up Timer() example.
........
r60030 | raymond.hettinger | 2008-01-18 00:56:56 +0100 (Fri, 18 Jan 2008) | 1 line
Fix markup
........
r60031 | raymond.hettinger | 2008-01-18 01:10:42 +0100 (Fri, 18 Jan 2008) | 1 line
clearcache() needs to remove the dict as well as clear it.
........
r60033 | andrew.kuchling | 2008-01-18 03:26:16 +0100 (Fri, 18 Jan 2008) | 1 line
Bump verson
........
r60034 | andrew.kuchling | 2008-01-18 03:42:52 +0100 (Fri, 18 Jan 2008) | 1 line
Typo fix
........
r60035 | christian.heimes | 2008-01-18 08:30:20 +0100 (Fri, 18 Jan 2008) | 3 lines
Coverity issue CID #197
var_decl: Declared variable "stm" without initializer
ninit_use_in_call: Using uninitialized value "stm" (field "stm".tm_zone uninitialized) in call to function "mktime"
........
r60036 | christian.heimes | 2008-01-18 08:45:30 +0100 (Fri, 18 Jan 2008) | 11 lines
Coverity issue CID #167
Event alloc_fn: Called allocation function "metacompile" [model]
Event var_assign: Assigned variable "gr" to storage returned from "metacompile"
gr = metacompile(n);
Event pass_arg: Variable "gr" not freed or pointed-to in function "maketables" [model]
g = maketables(gr);
translatelabels(g);
addfirstsets(g);
Event leaked_storage: Returned without freeing storage "gr"
return g;
........
r60038 | christian.heimes | 2008-01-18 09:04:57 +0100 (Fri, 18 Jan 2008) | 3 lines
Coverity issue CID #182
size_error: Allocating 1 bytes to pointer "children", which needs at least 4 bytes
........
r60041 | christian.heimes | 2008-01-18 09:47:59 +0100 (Fri, 18 Jan 2008) | 4 lines
Coverity issue CID #169
local_ptr_assign_local: Assigning address of stack variable "namebuf" to pointer "filename"
out_of_scope: Variable "namebuf" goes out of scope
use_invalid: Used "filename" pointing to out-of-scope variable "namebuf"
........
r60042 | christian.heimes | 2008-01-18 09:53:45 +0100 (Fri, 18 Jan 2008) | 2 lines
Coverity CID #168
leaked_storage: Returned without freeing storage "fp"
........
2008-01-18 05:56:22 -04:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. function:: takewhile(predicate, iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns elements from the iterable as long as the
|
2016-05-28 04:17:54 -03:00
|
|
|
|
predicate is true. Roughly equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def takewhile(predicate, iterable):
|
|
|
|
|
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
|
|
|
|
|
for x in iterable:
|
|
|
|
|
if predicate(x):
|
|
|
|
|
yield x
|
|
|
|
|
else:
|
|
|
|
|
break
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2024-02-25 08:32:14 -04:00
|
|
|
|
Note, the element that first fails the predicate condition is
|
|
|
|
|
consumed from the input iterator and there is no way to access it.
|
|
|
|
|
This could be an issue if an application wants to further consume the
|
|
|
|
|
input iterator after takewhile has been run to exhaustion. To work
|
|
|
|
|
around this problem, consider using `more-iterools before_and_after()
|
|
|
|
|
<https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.before_and_after>`_
|
|
|
|
|
instead.
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: tee(iterable, n=2)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2016-04-26 04:10:00 -03:00
|
|
|
|
Return *n* independent iterators from a single iterable.
|
|
|
|
|
|
|
|
|
|
The following Python code helps explain what *tee* does (although the actual
|
2016-05-16 03:31:54 -03:00
|
|
|
|
implementation is more complex and uses only a single underlying
|
2022-10-18 16:09:34 -03:00
|
|
|
|
:abbr:`FIFO (first-in, first-out)` queue)::
|
2009-02-18 16:56:51 -04:00
|
|
|
|
|
|
|
|
|
def tee(iterable, n=2):
|
|
|
|
|
it = iter(iterable)
|
|
|
|
|
deques = [collections.deque() for i in range(n)]
|
|
|
|
|
def gen(mydeque):
|
|
|
|
|
while True:
|
|
|
|
|
if not mydeque: # when the local deque is empty
|
2014-11-23 01:56:23 -04:00
|
|
|
|
try:
|
|
|
|
|
newval = next(it) # fetch a new value and
|
|
|
|
|
except StopIteration:
|
|
|
|
|
return
|
2009-02-18 16:56:51 -04:00
|
|
|
|
for d in deques: # load it to all the deques
|
|
|
|
|
d.append(newval)
|
|
|
|
|
yield mydeque.popleft()
|
|
|
|
|
return tuple(gen(d) for d in deques)
|
|
|
|
|
|
2022-10-18 16:09:34 -03:00
|
|
|
|
Once a :func:`tee` has been created, the original *iterable* should not be
|
2010-12-01 19:45:20 -04:00
|
|
|
|
used anywhere else; otherwise, the *iterable* could get advanced without
|
2019-09-09 05:18:16 -03:00
|
|
|
|
the tee objects being informed.
|
2009-02-18 16:56:51 -04:00
|
|
|
|
|
2019-09-09 05:47:14 -03:00
|
|
|
|
``tee`` iterators are not threadsafe. A :exc:`RuntimeError` may be
|
2022-08-19 03:20:45 -03:00
|
|
|
|
raised when simultaneously using iterators returned by the same :func:`tee`
|
2019-09-09 05:47:14 -03:00
|
|
|
|
call, even if the original *iterable* is threadsafe.
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
This itertool may require significant auxiliary storage (depending on how
|
|
|
|
|
much temporary data needs to be stored). In general, if one iterator uses
|
|
|
|
|
most or all of the data before another iterator starts, it is faster to use
|
|
|
|
|
:func:`list` instead of :func:`tee`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: zip_longest(*iterables, fillvalue=None)
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that aggregates elements from each of the iterables. If the
|
|
|
|
|
iterables are of uneven length, missing values are filled-in with *fillvalue*.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Iteration continues until the longest iterable is exhausted. Roughly equivalent to::
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2017-09-07 18:01:44 -03:00
|
|
|
|
def zip_longest(*args, fillvalue=None):
|
2010-12-01 19:45:20 -04:00
|
|
|
|
# zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
|
2017-09-07 18:01:44 -03:00
|
|
|
|
iterators = [iter(it) for it in args]
|
|
|
|
|
num_active = len(iterators)
|
|
|
|
|
if not num_active:
|
|
|
|
|
return
|
|
|
|
|
while True:
|
|
|
|
|
values = []
|
|
|
|
|
for i, it in enumerate(iterators):
|
|
|
|
|
try:
|
|
|
|
|
value = next(it)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
num_active -= 1
|
|
|
|
|
if not num_active:
|
|
|
|
|
return
|
|
|
|
|
iterators[i] = repeat(fillvalue)
|
|
|
|
|
value = fillvalue
|
|
|
|
|
values.append(value)
|
|
|
|
|
yield tuple(values)
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
If one of the iterables is potentially infinite, then the :func:`zip_longest`
|
|
|
|
|
function should be wrapped with something that limits the number of calls
|
|
|
|
|
(for example :func:`islice` or :func:`takewhile`). If not specified,
|
|
|
|
|
*fillvalue* defaults to ``None``.
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. _itertools-recipes:
|
|
|
|
|
|
2010-12-06 19:31:36 -04:00
|
|
|
|
Itertools Recipes
|
|
|
|
|
-----------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
This section shows recipes for creating an extended toolset using the existing
|
|
|
|
|
itertools as building blocks.
|
|
|
|
|
|
2022-10-18 16:09:34 -03:00
|
|
|
|
The primary purpose of the itertools recipes is educational. The recipes show
|
|
|
|
|
various ways of thinking about individual tools — for example, that
|
|
|
|
|
``chain.from_iterable`` is related to the concept of flattening. The recipes
|
|
|
|
|
also give ideas about ways that the tools can be combined — for example, how
|
2022-10-31 12:30:29 -03:00
|
|
|
|
``compress()`` and ``range()`` can work together. The recipes also show patterns
|
2022-10-18 16:09:34 -03:00
|
|
|
|
for using itertools with the :mod:`operator` and :mod:`collections` modules as
|
|
|
|
|
well as with the built-in itertools such as ``map()``, ``filter()``,
|
|
|
|
|
``reversed()``, and ``enumerate()``.
|
|
|
|
|
|
|
|
|
|
A secondary purpose of the recipes is to serve as an incubator. The
|
|
|
|
|
``accumulate()``, ``compress()``, and ``pairwise()`` itertools started out as
|
2023-04-09 16:17:37 -03:00
|
|
|
|
recipes. Currently, the ``sliding_window()`` and ``iter_index()`` recipes
|
|
|
|
|
are being tested to see whether they prove their worth.
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2019-08-04 17:35:58 -03:00
|
|
|
|
Substantially all of these recipes and many, many others can be installed from
|
|
|
|
|
the `more-itertools project <https://pypi.org/project/more-itertools/>`_ found
|
|
|
|
|
on the Python Package Index::
|
|
|
|
|
|
2022-04-09 15:37:01 -03:00
|
|
|
|
python -m pip install more-itertools
|
2019-08-04 17:35:58 -03:00
|
|
|
|
|
2022-10-18 16:09:34 -03:00
|
|
|
|
Many of the recipes offer the same high performance as the underlying toolset.
|
|
|
|
|
Superior memory performance is kept by processing elements one at a time
|
2007-08-15 11:28:22 -03:00
|
|
|
|
rather than bringing the whole iterable into memory all at once. Code volume is
|
|
|
|
|
kept small by linking the tools together in a functional style which helps
|
|
|
|
|
eliminate temporary variables. High speed is retained by preferring
|
#1370: Finish the merge r58749, log below, by resolving all conflicts in Doc/.
Merged revisions 58221-58741 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r58221 | georg.brandl | 2007-09-20 10:57:59 -0700 (Thu, 20 Sep 2007) | 2 lines
Patch #1181: add os.environ.clear() method.
........
r58225 | sean.reifschneider | 2007-09-20 23:33:28 -0700 (Thu, 20 Sep 2007) | 3 lines
Issue1704287: "make install" fails unless you do "make" first. Make
oldsharedmods and sharedmods in "libinstall".
........
r58232 | guido.van.rossum | 2007-09-22 13:18:03 -0700 (Sat, 22 Sep 2007) | 4 lines
Patch # 188 by Philip Jenvey.
Make tell() mark CRLF as a newline.
With unit test.
........
r58242 | georg.brandl | 2007-09-24 10:55:47 -0700 (Mon, 24 Sep 2007) | 2 lines
Fix typo and double word.
........
r58245 | georg.brandl | 2007-09-24 10:59:28 -0700 (Mon, 24 Sep 2007) | 2 lines
#1196: document default radix for int().
........
r58247 | georg.brandl | 2007-09-24 11:08:24 -0700 (Mon, 24 Sep 2007) | 2 lines
#1177: accept 2xx responses for https too, not only http.
........
r58249 | andrew.kuchling | 2007-09-24 16:45:51 -0700 (Mon, 24 Sep 2007) | 1 line
Remove stray odd character; grammar fix
........
r58250 | andrew.kuchling | 2007-09-24 16:46:28 -0700 (Mon, 24 Sep 2007) | 1 line
Typo fix
........
r58251 | andrew.kuchling | 2007-09-24 17:09:42 -0700 (Mon, 24 Sep 2007) | 1 line
Add various items
........
r58268 | vinay.sajip | 2007-09-26 22:34:45 -0700 (Wed, 26 Sep 2007) | 1 line
Change to flush and close logic to fix #1760556.
........
r58269 | vinay.sajip | 2007-09-26 22:38:51 -0700 (Wed, 26 Sep 2007) | 1 line
Change to basicConfig() to fix #1021.
........
r58270 | georg.brandl | 2007-09-26 23:26:58 -0700 (Wed, 26 Sep 2007) | 2 lines
#1208: document match object's boolean value.
........
r58271 | vinay.sajip | 2007-09-26 23:56:13 -0700 (Wed, 26 Sep 2007) | 1 line
Minor date change.
........
r58272 | vinay.sajip | 2007-09-27 00:35:10 -0700 (Thu, 27 Sep 2007) | 1 line
Change to LogRecord.__init__() to fix #1206. Note that archaic use of type(x) == types.DictType is because of keeping 1.5.2 compatibility. While this is much less relevant these days, there probably needs to be a separate commit for removing all archaic constructs at the same time.
........
r58288 | brett.cannon | 2007-09-30 12:45:10 -0700 (Sun, 30 Sep 2007) | 9 lines
tuple.__repr__ did not consider a reference loop as it is not possible from
Python code; but it is possible from C. object.__str__ had the issue of not
expecting a type to doing something within it's tp_str implementation that
could trigger an infinite recursion, but it could in C code.. Both found
thanks to BaseException and how it handles its repr.
Closes issue #1686386. Thanks to Thomas Herve for taking an initial stab at
coming up with a solution.
........
r58289 | brett.cannon | 2007-09-30 13:37:19 -0700 (Sun, 30 Sep 2007) | 3 lines
Fix error introduced by r58288; if a tuple is length 0 return its repr and
don't worry about any self-referring tuples.
........
r58294 | facundo.batista | 2007-10-02 10:01:24 -0700 (Tue, 02 Oct 2007) | 11 lines
Made the various is_* operations return booleans. This was discussed
with Cawlishaw by mail, and he basically confirmed that to these is_*
operations, there's no need to return Decimal(0) and Decimal(1) if
the language supports the False and True booleans.
Also added a few tests for the these functions in extra.decTest, since
they are mostly untested (apart from the doctests).
Thanks Mark Dickinson
........
r58295 | facundo.batista | 2007-10-02 11:21:18 -0700 (Tue, 02 Oct 2007) | 4 lines
Added a class to store the digits of log(10), so that they can be made
available when necessary without recomputing. Thanks Mark Dickinson
........
r58299 | mark.summerfield | 2007-10-03 01:53:21 -0700 (Wed, 03 Oct 2007) | 4 lines
Added note in footnote about string comparisons about
unicodedata.normalize().
........
r58304 | raymond.hettinger | 2007-10-03 14:18:11 -0700 (Wed, 03 Oct 2007) | 1 line
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
........
r58305 | raymond.hettinger | 2007-10-03 17:20:27 -0700 (Wed, 03 Oct 2007) | 1 line
itertools.count() no longer limited to sys.maxint.
........
r58306 | kurt.kaiser | 2007-10-03 18:49:54 -0700 (Wed, 03 Oct 2007) | 3 lines
Assume that the user knows when he wants to end the line; don't insert
something he didn't select or complete.
........
r58307 | kurt.kaiser | 2007-10-03 19:07:50 -0700 (Wed, 03 Oct 2007) | 2 lines
Remove unused theme that was causing a fault in p3k.
........
r58308 | kurt.kaiser | 2007-10-03 19:09:17 -0700 (Wed, 03 Oct 2007) | 2 lines
Clean up EditorWindow close.
........
r58309 | kurt.kaiser | 2007-10-03 19:53:07 -0700 (Wed, 03 Oct 2007) | 7 lines
textView cleanup. Patch 1718043 Tal Einat.
M idlelib/EditorWindow.py
M idlelib/aboutDialog.py
M idlelib/textView.py
M idlelib/NEWS.txt
........
r58310 | kurt.kaiser | 2007-10-03 20:11:12 -0700 (Wed, 03 Oct 2007) | 3 lines
configDialog cleanup. Patch 1730217 Tal Einat.
........
r58311 | neal.norwitz | 2007-10-03 23:00:48 -0700 (Wed, 03 Oct 2007) | 4 lines
Coverity #151: Remove deadcode.
All this code already exists above starting at line 653.
........
r58325 | fred.drake | 2007-10-04 19:46:12 -0700 (Thu, 04 Oct 2007) | 1 line
wrap lines to <80 characters before fixing errors
........
r58326 | raymond.hettinger | 2007-10-04 19:47:07 -0700 (Thu, 04 Oct 2007) | 6 lines
Add __asdict__() to NamedTuple and refine the docs.
Add maxlen support to deque() and fixup docs.
Partially fix __reduce__(). The None as a third arg was no longer supported.
Still needs work on __reduce__() to handle recursive inputs.
........
r58327 | fred.drake | 2007-10-04 19:48:32 -0700 (Thu, 04 Oct 2007) | 3 lines
move descriptions of ac_(in|out)_buffer_size to the right place
http://bugs.python.org/issue1053
........
r58329 | neal.norwitz | 2007-10-04 20:39:17 -0700 (Thu, 04 Oct 2007) | 3 lines
dict could be NULL, so we need to XDECREF.
Fix a compiler warning about passing a PyTypeObject* instead of PyObject*.
........
r58330 | neal.norwitz | 2007-10-04 20:41:19 -0700 (Thu, 04 Oct 2007) | 2 lines
Fix Coverity #158: Check the correct variable.
........
r58332 | neal.norwitz | 2007-10-04 22:01:38 -0700 (Thu, 04 Oct 2007) | 7 lines
Fix Coverity #159.
This code was broken if save() returned a negative number since i contained
a boolean value and then we compared i < 0 which should never be true.
Will backport (assuming it's necessary)
........
r58334 | neal.norwitz | 2007-10-04 22:29:17 -0700 (Thu, 04 Oct 2007) | 1 line
Add a note about fixing some more warnings found by Coverity.
........
r58338 | raymond.hettinger | 2007-10-05 12:07:31 -0700 (Fri, 05 Oct 2007) | 1 line
Restore BEGIN/END THREADS macros which were squashed in the previous checkin
........
r58343 | gregory.p.smith | 2007-10-06 00:48:10 -0700 (Sat, 06 Oct 2007) | 3 lines
Stab in the dark attempt to fix the test_bsddb3 failure on sparc and S-390
ubuntu buildbots.
........
r58344 | gregory.p.smith | 2007-10-06 00:51:59 -0700 (Sat, 06 Oct 2007) | 2 lines
Allows BerkeleyDB 4.6.x >= 4.6.21 for the bsddb module.
........
r58348 | gregory.p.smith | 2007-10-06 08:47:37 -0700 (Sat, 06 Oct 2007) | 3 lines
Use the host the author likely meant in the first place. pop.gmail.com is
reliable. gmail.org is someones personal domain.
........
r58351 | neal.norwitz | 2007-10-06 12:16:28 -0700 (Sat, 06 Oct 2007) | 3 lines
Ensure that this test will pass even if another test left an unwritable TESTFN.
Also use the safe unlink in test_support instead of rolling our own here.
........
r58368 | georg.brandl | 2007-10-08 00:50:24 -0700 (Mon, 08 Oct 2007) | 3 lines
#1123: fix the docs for the str.split(None, sep) case.
Also expand a few other methods' docs, which had more info in the deprecated string module docs.
........
r58369 | georg.brandl | 2007-10-08 01:06:05 -0700 (Mon, 08 Oct 2007) | 2 lines
Update docstring of sched, also remove an unused assignment.
........
r58370 | raymond.hettinger | 2007-10-08 02:14:28 -0700 (Mon, 08 Oct 2007) | 5 lines
Add comments to NamedTuple code.
Let the field spec be either a string or a non-string sequence (suggested by Martin Blais with use cases).
Improve the error message in the case of a SyntaxError (caused by a duplicate field name).
........
r58371 | raymond.hettinger | 2007-10-08 02:56:29 -0700 (Mon, 08 Oct 2007) | 1 line
Missed a line in the docs
........
r58372 | raymond.hettinger | 2007-10-08 03:11:51 -0700 (Mon, 08 Oct 2007) | 1 line
Better variable names
........
r58376 | georg.brandl | 2007-10-08 07:12:47 -0700 (Mon, 08 Oct 2007) | 3 lines
#1199: docs for tp_as_{number,sequence,mapping}, by Amaury Forgeot d'Arc.
No need to merge this to py3k!
........
r58380 | raymond.hettinger | 2007-10-08 14:26:58 -0700 (Mon, 08 Oct 2007) | 1 line
Eliminate camelcase function name
........
r58381 | andrew.kuchling | 2007-10-08 16:23:03 -0700 (Mon, 08 Oct 2007) | 1 line
Eliminate camelcase function name
........
r58382 | raymond.hettinger | 2007-10-08 18:36:23 -0700 (Mon, 08 Oct 2007) | 1 line
Make the error messages more specific
........
r58384 | gregory.p.smith | 2007-10-08 23:02:21 -0700 (Mon, 08 Oct 2007) | 10 lines
Splits Modules/_bsddb.c up into bsddb.h and _bsddb.c and adds a C API
object available as bsddb.db.api. This is based on the patch submitted
by Duncan Grisby here:
http://sourceforge.net/tracker/index.php?func=detail&aid=1551895&group_id=13900&atid=313900
See this thread for additional info:
http://sourceforge.net/mailarchive/forum.php?thread_name=E1GAVDK-0002rk-Iw%40apasphere.com&forum_name=pybsddb-users
It also cleans up the code a little by removing some ifdef/endifs for
python prior to 2.1 and for unsupported Berkeley DB <= 3.2.
........
r58385 | gregory.p.smith | 2007-10-08 23:50:43 -0700 (Mon, 08 Oct 2007) | 5 lines
Fix a double free when positioning a database cursor to a non-existant
string key (and probably a few other situations with string keys).
This was reported with a patch as pybsddb sourceforge bug 1708868 by
jjjhhhlll at gmail.
........
r58386 | gregory.p.smith | 2007-10-09 00:19:11 -0700 (Tue, 09 Oct 2007) | 3 lines
Use the highest cPickle protocol in bsddb.dbshelve. This comes from
sourceforge pybsddb patch 1551443 by w_barnes.
........
r58394 | gregory.p.smith | 2007-10-09 11:26:02 -0700 (Tue, 09 Oct 2007) | 2 lines
remove another sleepycat reference
........
r58396 | kurt.kaiser | 2007-10-09 12:31:30 -0700 (Tue, 09 Oct 2007) | 3 lines
Allow interrupt only when executing user code in subprocess
Patch 1225 Tal Einat modified from IDLE-Spoon.
........
r58399 | brett.cannon | 2007-10-09 17:07:50 -0700 (Tue, 09 Oct 2007) | 5 lines
Remove file-level typedefs that were inconsistently used throughout the file.
Just move over to the public API names.
Closes issue1238.
........
r58401 | raymond.hettinger | 2007-10-09 17:26:46 -0700 (Tue, 09 Oct 2007) | 1 line
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
........
r58403 | kurt.kaiser | 2007-10-09 17:55:40 -0700 (Tue, 09 Oct 2007) | 2 lines
Allow cursor color change w/o restart. Patch 1725576 Tal Einat.
........
r58404 | kurt.kaiser | 2007-10-09 18:06:47 -0700 (Tue, 09 Oct 2007) | 2 lines
show paste if > 80 columns. Patch 1659326 Tal Einat.
........
r58415 | thomas.heller | 2007-10-11 12:51:32 -0700 (Thu, 11 Oct 2007) | 5 lines
On OS X, use os.uname() instead of gestalt.sysv(...) to get the
operating system version. This allows to use ctypes when Python
was configured with --disable-toolbox-glue.
........
r58419 | neal.norwitz | 2007-10-11 20:01:01 -0700 (Thu, 11 Oct 2007) | 1 line
Get rid of warning about not being able to create an existing directory.
........
r58420 | neal.norwitz | 2007-10-11 20:01:30 -0700 (Thu, 11 Oct 2007) | 1 line
Get rid of warnings on a bunch of platforms by using a proper prototype.
........
r58421 | neal.norwitz | 2007-10-11 20:01:54 -0700 (Thu, 11 Oct 2007) | 4 lines
Get rid of compiler warning about retval being used (returned) without
being initialized. (gcc warning and Coverity 202)
........
r58422 | neal.norwitz | 2007-10-11 20:03:23 -0700 (Thu, 11 Oct 2007) | 1 line
Fix Coverity 168: Close the file before returning (exiting).
........
r58423 | neal.norwitz | 2007-10-11 20:04:18 -0700 (Thu, 11 Oct 2007) | 4 lines
Fix Coverity 180: Don't overallocate. We don't need structs, but pointers.
Also fix a memory leak.
........
r58424 | neal.norwitz | 2007-10-11 20:05:19 -0700 (Thu, 11 Oct 2007) | 5 lines
Fix Coverity 185-186: If the passed in FILE is NULL, uninitialized memory
would be accessed.
Will backport.
........
r58425 | neal.norwitz | 2007-10-11 20:52:34 -0700 (Thu, 11 Oct 2007) | 1 line
Get this module to compile with bsddb versions prior to 4.3
........
r58430 | martin.v.loewis | 2007-10-12 01:56:52 -0700 (Fri, 12 Oct 2007) | 3 lines
Bug #1216: Restore support for Visual Studio 2002.
Will backport to 2.5.
........
r58433 | raymond.hettinger | 2007-10-12 10:53:11 -0700 (Fri, 12 Oct 2007) | 1 line
Fix test of count.__repr__() to ignore the 'L' if the count is a long
........
r58434 | gregory.p.smith | 2007-10-12 11:44:06 -0700 (Fri, 12 Oct 2007) | 4 lines
Fixes http://bugs.python.org/issue1233 - bsddb.dbshelve.DBShelf.append
was useless due to inverted logic. Also adds a test case for RECNO dbs
to test_dbshelve.
........
r58445 | georg.brandl | 2007-10-13 06:20:03 -0700 (Sat, 13 Oct 2007) | 2 lines
Fix email example.
........
r58450 | gregory.p.smith | 2007-10-13 16:02:05 -0700 (Sat, 13 Oct 2007) | 2 lines
Fix an uncollectable reference leak in bsddb.db.DBShelf.append
........
r58453 | neal.norwitz | 2007-10-13 17:18:40 -0700 (Sat, 13 Oct 2007) | 8 lines
Let the O/S supply a port if none of the default ports can be used.
This should make the tests more robust at the expense of allowing
tests to be sloppier by not requiring them to cleanup after themselves.
(It will legitamitely help when running two test suites simultaneously
or if another process is already using one of the predefined ports.)
Also simplifies (slightLy) the exception handling elsewhere.
........
r58459 | neal.norwitz | 2007-10-14 11:30:21 -0700 (Sun, 14 Oct 2007) | 2 lines
Don't raise a string exception, they don't work anymore.
........
r58460 | neal.norwitz | 2007-10-14 11:40:37 -0700 (Sun, 14 Oct 2007) | 1 line
Use unittest for assertions
........
r58468 | armin.rigo | 2007-10-15 00:48:35 -0700 (Mon, 15 Oct 2007) | 2 lines
test_bigbits was not testing what it seemed to.
........
r58471 | guido.van.rossum | 2007-10-15 08:54:11 -0700 (Mon, 15 Oct 2007) | 3 lines
Change a PyErr_Print() into a PyErr_Clear(),
per discussion in issue 1031213.
........
r58500 | raymond.hettinger | 2007-10-16 12:18:30 -0700 (Tue, 16 Oct 2007) | 1 line
Improve error messages
........
r58506 | raymond.hettinger | 2007-10-16 14:28:32 -0700 (Tue, 16 Oct 2007) | 1 line
More docs, error messages, and tests
........
r58507 | andrew.kuchling | 2007-10-16 15:58:03 -0700 (Tue, 16 Oct 2007) | 1 line
Add items
........
r58508 | brett.cannon | 2007-10-16 16:24:06 -0700 (Tue, 16 Oct 2007) | 3 lines
Remove ``:const:`` notation on None in parameter list. Since the markup is not
rendered for parameters it just showed up as ``:const:`None` `` in the output.
........
r58509 | brett.cannon | 2007-10-16 16:26:45 -0700 (Tue, 16 Oct 2007) | 3 lines
Re-order some functions whose parameters differ between PyObject and const char
* so that they are next to each other.
........
r58522 | armin.rigo | 2007-10-17 11:46:37 -0700 (Wed, 17 Oct 2007) | 5 lines
Fix the overflow checking of list_repeat.
Introduce overflow checking into list_inplace_repeat.
Backport candidate, possibly.
........
r58530 | facundo.batista | 2007-10-17 20:16:03 -0700 (Wed, 17 Oct 2007) | 7 lines
Issue #1580738. When HTTPConnection reads the whole stream with read(),
it closes itself. When the stream is read in several calls to read(n),
it should behave in the same way if HTTPConnection knows where the end
of the stream is (through self.length). Added a test case for this
behaviour.
........
r58531 | facundo.batista | 2007-10-17 20:44:48 -0700 (Wed, 17 Oct 2007) | 3 lines
Issue 1289, just a typo.
........
r58532 | gregory.p.smith | 2007-10-18 00:56:54 -0700 (Thu, 18 Oct 2007) | 4 lines
cleanup test_dbtables to use mkdtemp. cleanup dbtables to pass txn as a
keyword argument whenever possible to avoid bugs and confusion. (dbtables.py
line 447 self.db.get using txn as a non-keyword was an actual bug due to this)
........
r58533 | gregory.p.smith | 2007-10-18 01:34:20 -0700 (Thu, 18 Oct 2007) | 4 lines
Fix a weird bug in dbtables: if it chose a random rowid string that contained
NULL bytes it would cause the database all sorts of problems in the future
leading to very strange random failures and corrupt dbtables.bsdTableDb dbs.
........
r58534 | gregory.p.smith | 2007-10-18 09:32:02 -0700 (Thu, 18 Oct 2007) | 3 lines
A cleaner fix than the one committed last night. Generate random rowids that
do not contain null bytes.
........
r58537 | gregory.p.smith | 2007-10-18 10:17:57 -0700 (Thu, 18 Oct 2007) | 2 lines
mention bsddb fixes.
........
r58538 | raymond.hettinger | 2007-10-18 14:13:06 -0700 (Thu, 18 Oct 2007) | 1 line
Remove useless warning
........
r58539 | gregory.p.smith | 2007-10-19 00:31:20 -0700 (Fri, 19 Oct 2007) | 2 lines
squelch the warning that this test is supposed to trigger.
........
r58542 | georg.brandl | 2007-10-19 05:32:39 -0700 (Fri, 19 Oct 2007) | 2 lines
Clarify wording for apply().
........
r58544 | mark.summerfield | 2007-10-19 05:48:17 -0700 (Fri, 19 Oct 2007) | 3 lines
Added a cross-ref to each other.
........
r58545 | georg.brandl | 2007-10-19 10:38:49 -0700 (Fri, 19 Oct 2007) | 2 lines
#1284: "S" means "seen", not unread.
........
r58548 | thomas.heller | 2007-10-19 11:11:41 -0700 (Fri, 19 Oct 2007) | 4 lines
Fix ctypes on 32-bit systems when Python is configured --with-system-ffi.
See also https://bugs.launchpad.net/bugs/72505.
Ported from release25-maint branch.
........
r58550 | facundo.batista | 2007-10-19 12:25:57 -0700 (Fri, 19 Oct 2007) | 8 lines
The constructor from tuple was way too permissive: it allowed bad
coefficient numbers, floats in the sign, and other details that
generated directly the wrong number in the best case, or triggered
misfunctionality in the alorithms.
Test cases added for these issues. Thanks Mark Dickinson.
........
r58559 | georg.brandl | 2007-10-20 06:22:53 -0700 (Sat, 20 Oct 2007) | 2 lines
Fix code being interpreted as a target.
........
r58561 | georg.brandl | 2007-10-20 06:36:24 -0700 (Sat, 20 Oct 2007) | 2 lines
Document new "cmdoption" directive.
........
r58562 | georg.brandl | 2007-10-20 08:21:22 -0700 (Sat, 20 Oct 2007) | 2 lines
Make a path more Unix-standardy.
........
r58564 | georg.brandl | 2007-10-20 10:51:39 -0700 (Sat, 20 Oct 2007) | 2 lines
Document new directive "envvar".
........
r58567 | georg.brandl | 2007-10-20 11:08:14 -0700 (Sat, 20 Oct 2007) | 6 lines
* Add new toplevel chapter, "Using Python." (how to install,
configure and setup python on different platforms -- at least
in theory.)
* Move the Python on Mac docs in that chapter.
* Add a new chapter about the command line invocation, by stargaming.
........
r58568 | georg.brandl | 2007-10-20 11:33:20 -0700 (Sat, 20 Oct 2007) | 2 lines
Change title, for now.
........
r58569 | georg.brandl | 2007-10-20 11:39:25 -0700 (Sat, 20 Oct 2007) | 2 lines
Add entry to ACKS.
........
r58570 | georg.brandl | 2007-10-20 12:05:45 -0700 (Sat, 20 Oct 2007) | 2 lines
Clarify -E docs.
........
r58571 | georg.brandl | 2007-10-20 12:08:36 -0700 (Sat, 20 Oct 2007) | 2 lines
Even more clarification.
........
r58572 | andrew.kuchling | 2007-10-20 12:25:37 -0700 (Sat, 20 Oct 2007) | 1 line
Fix protocol name
........
r58573 | andrew.kuchling | 2007-10-20 12:35:18 -0700 (Sat, 20 Oct 2007) | 1 line
Various items
........
r58574 | andrew.kuchling | 2007-10-20 12:39:35 -0700 (Sat, 20 Oct 2007) | 1 line
Use correct header line
........
r58576 | armin.rigo | 2007-10-21 02:14:15 -0700 (Sun, 21 Oct 2007) | 3 lines
Add a crasher for the long-standing issue with closing a file
while another thread uses it.
........
r58577 | georg.brandl | 2007-10-21 03:01:56 -0700 (Sun, 21 Oct 2007) | 2 lines
Remove duplicate crasher.
........
r58578 | georg.brandl | 2007-10-21 03:24:20 -0700 (Sun, 21 Oct 2007) | 2 lines
Unify "byte code" to "bytecode". Also sprinkle :term: markup for it.
........
r58579 | georg.brandl | 2007-10-21 03:32:54 -0700 (Sun, 21 Oct 2007) | 2 lines
Add markup to new function descriptions.
........
r58580 | georg.brandl | 2007-10-21 03:45:46 -0700 (Sun, 21 Oct 2007) | 2 lines
Add :term:s for descriptors.
........
r58581 | georg.brandl | 2007-10-21 03:46:24 -0700 (Sun, 21 Oct 2007) | 2 lines
Unify "file-descriptor" to "file descriptor".
........
r58582 | georg.brandl | 2007-10-21 03:52:38 -0700 (Sun, 21 Oct 2007) | 2 lines
Add :term: for generators.
........
r58583 | georg.brandl | 2007-10-21 05:10:28 -0700 (Sun, 21 Oct 2007) | 2 lines
Add :term:s for iterator.
........
r58584 | georg.brandl | 2007-10-21 05:15:05 -0700 (Sun, 21 Oct 2007) | 2 lines
Add :term:s for "new-style class".
........
r58588 | neal.norwitz | 2007-10-21 21:47:54 -0700 (Sun, 21 Oct 2007) | 1 line
Add Chris Monson so he can edit PEPs.
........
r58594 | guido.van.rossum | 2007-10-22 09:27:19 -0700 (Mon, 22 Oct 2007) | 4 lines
Issue #1307, patch by Derek Shockey.
When "MAIL" is received without args, an exception happens instead of
sending a 501 syntax error response.
........
r58598 | travis.oliphant | 2007-10-22 19:40:56 -0700 (Mon, 22 Oct 2007) | 1 line
Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
........
r58601 | neal.norwitz | 2007-10-22 22:44:27 -0700 (Mon, 22 Oct 2007) | 2 lines
Bug #1313, fix typo (wrong variable name) in example.
........
r58609 | georg.brandl | 2007-10-23 11:21:35 -0700 (Tue, 23 Oct 2007) | 2 lines
Update Pygments version from externals.
........
r58618 | guido.van.rossum | 2007-10-23 12:25:41 -0700 (Tue, 23 Oct 2007) | 3 lines
Issue 1307 by Derek Shockey, fox the same bug for RCPT.
Neal: please backport!
........
r58620 | raymond.hettinger | 2007-10-23 13:37:41 -0700 (Tue, 23 Oct 2007) | 1 line
Shorter name for namedtuple()
........
r58621 | andrew.kuchling | 2007-10-23 13:55:47 -0700 (Tue, 23 Oct 2007) | 1 line
Update name
........
r58622 | raymond.hettinger | 2007-10-23 14:23:07 -0700 (Tue, 23 Oct 2007) | 1 line
Fixup news entry
........
r58623 | raymond.hettinger | 2007-10-23 18:28:33 -0700 (Tue, 23 Oct 2007) | 1 line
Optimize sum() for integer and float inputs.
........
r58624 | raymond.hettinger | 2007-10-23 19:05:51 -0700 (Tue, 23 Oct 2007) | 1 line
Fixup error return and add support for intermixed ints and floats/
........
r58628 | vinay.sajip | 2007-10-24 03:47:06 -0700 (Wed, 24 Oct 2007) | 1 line
Bug #1321: Fixed logic error in TimedRotatingFileHandler.__init__()
........
r58641 | facundo.batista | 2007-10-24 12:11:08 -0700 (Wed, 24 Oct 2007) | 4 lines
Issue 1290. CharacterData.__repr__ was constructing a string
in response that keeped having a non-ascii character.
........
r58643 | thomas.heller | 2007-10-24 12:50:45 -0700 (Wed, 24 Oct 2007) | 1 line
Added unittest for calling a function with paramflags (backport from py3k branch).
........
r58645 | matthias.klose | 2007-10-24 13:00:44 -0700 (Wed, 24 Oct 2007) | 2 lines
- Build using system ffi library on arm*-linux*.
........
r58651 | georg.brandl | 2007-10-24 14:40:38 -0700 (Wed, 24 Oct 2007) | 2 lines
Bug #1287: make os.environ.pop() work as expected.
........
r58652 | raymond.hettinger | 2007-10-24 19:26:58 -0700 (Wed, 24 Oct 2007) | 1 line
Missing DECREFs
........
r58653 | matthias.klose | 2007-10-24 23:37:24 -0700 (Wed, 24 Oct 2007) | 2 lines
- Build using system ffi library on arm*-linux*, pass --with-system-ffi to CONFIG_ARGS
........
r58655 | thomas.heller | 2007-10-25 12:47:32 -0700 (Thu, 25 Oct 2007) | 2 lines
ffi_type_longdouble may be already #defined.
See issue 1324.
........
r58656 | kurt.kaiser | 2007-10-25 15:43:45 -0700 (Thu, 25 Oct 2007) | 3 lines
Correct an ancient bug in an unused path by removing that path: register() is
now idempotent.
........
r58660 | kurt.kaiser | 2007-10-25 17:10:09 -0700 (Thu, 25 Oct 2007) | 4 lines
1. Add comments to provide top-level documentation.
2. Refactor to use more descriptive names.
3. Enhance tests in main().
........
r58675 | georg.brandl | 2007-10-26 11:30:41 -0700 (Fri, 26 Oct 2007) | 2 lines
Fix new pop() method on os.environ on ignorecase-platforms.
........
r58696 | neal.norwitz | 2007-10-27 15:32:21 -0700 (Sat, 27 Oct 2007) | 1 line
Update URL for Pygments. 0.8.1 is no longer available
........
r58697 | hyeshik.chang | 2007-10-28 04:19:02 -0700 (Sun, 28 Oct 2007) | 3 lines
- Add support for FreeBSD 8 which is recently forked from FreeBSD 7.
- Regenerate IN module for most recent maintenance tree of FreeBSD 6 and 7.
........
r58698 | hyeshik.chang | 2007-10-28 05:38:09 -0700 (Sun, 28 Oct 2007) | 2 lines
Enable platform-specific tweaks for FreeBSD 8 (exactly same to FreeBSD 7's yet)
........
r58700 | kurt.kaiser | 2007-10-28 12:03:59 -0700 (Sun, 28 Oct 2007) | 2 lines
Add confirmation dialog before printing. Patch 1717170 Tal Einat.
........
r58706 | guido.van.rossum | 2007-10-29 13:52:45 -0700 (Mon, 29 Oct 2007) | 3 lines
Patch 1353 by Jacob Winther.
Add mp4 mapping to mimetypes.py.
........
r58709 | guido.van.rossum | 2007-10-29 15:15:05 -0700 (Mon, 29 Oct 2007) | 6 lines
Backport fixes for the code that decodes octal escapes (and for PyString
also hex escapes) -- this was reaching beyond the end of the input string
buffer, even though it is not supposed to be \0-terminated.
This has no visible effect but is clearly the correct thing to do.
(In 3.0 it had a visible effect after removing ob_sstate from PyString.)
........
r58710 | kurt.kaiser | 2007-10-29 19:38:54 -0700 (Mon, 29 Oct 2007) | 7 lines
check in Tal Einat's update to tabpage.py
Patch 1612746
M configDialog.py
M NEWS.txt
AM tabbedpages.py
........
r58715 | georg.brandl | 2007-10-30 10:51:18 -0700 (Tue, 30 Oct 2007) | 2 lines
Use correct markup.
........
r58716 | georg.brandl | 2007-10-30 10:57:12 -0700 (Tue, 30 Oct 2007) | 2 lines
Make example about hiding None return values at the prompt clearer.
........
r58728 | neal.norwitz | 2007-10-30 23:33:20 -0700 (Tue, 30 Oct 2007) | 1 line
Fix some compiler warnings for signed comparisons on Unix and Windows.
........
r58731 | martin.v.loewis | 2007-10-31 10:19:33 -0700 (Wed, 31 Oct 2007) | 2 lines
Adding Christian Heimes.
........
r58737 | raymond.hettinger | 2007-10-31 14:57:58 -0700 (Wed, 31 Oct 2007) | 1 line
Clarify the reasons why pickle is almost always better than marshal
........
r58739 | raymond.hettinger | 2007-10-31 15:15:49 -0700 (Wed, 31 Oct 2007) | 1 line
Sets are marshalable.
........
2007-11-01 17:32:30 -03:00
|
|
|
|
"vectorized" building blocks over the use of for-loops and :term:`generator`\s
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
|
which incur interpreter overhead.
|
|
|
|
|
|
|
|
|
|
.. testcode::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2022-12-24 04:21:30 -04:00
|
|
|
|
import collections
|
2023-04-28 14:25:50 -03:00
|
|
|
|
import functools
|
2022-12-24 04:21:30 -04:00
|
|
|
|
import math
|
|
|
|
|
import operator
|
|
|
|
|
import random
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def take(n, iterable):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Return first n items of the iterable as a list."
|
2010-12-01 19:45:20 -04:00
|
|
|
|
return list(islice(iterable, n))
|
|
|
|
|
|
2023-11-22 01:35:36 -04:00
|
|
|
|
def prepend(value, iterable):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Prepend a single value in front of an iterable."
|
2022-09-13 23:10:47 -03:00
|
|
|
|
# prepend(1, [2, 3, 4]) --> 1 2 3 4
|
2023-11-22 01:35:36 -04:00
|
|
|
|
return chain([value], iterable)
|
2018-04-08 12:44:20 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def tabulate(function, start=0):
|
|
|
|
|
"Return function(0), function(1), ..."
|
|
|
|
|
return map(function, count(start))
|
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def repeatfunc(func, times=None, *args):
|
|
|
|
|
"""Repeat calls to func with specified arguments.
|
|
|
|
|
|
|
|
|
|
Example: repeatfunc(random.random)
|
|
|
|
|
"""
|
|
|
|
|
if times is None:
|
|
|
|
|
return starmap(func, repeat(args))
|
|
|
|
|
return starmap(func, repeat(args, times))
|
|
|
|
|
|
|
|
|
|
def flatten(list_of_lists):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Flatten one level of nesting."
|
2023-04-09 16:17:37 -03:00
|
|
|
|
return chain.from_iterable(list_of_lists)
|
|
|
|
|
|
|
|
|
|
def ncycles(iterable, n):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Returns the sequence elements n times."
|
2023-04-09 16:17:37 -03:00
|
|
|
|
return chain.from_iterable(repeat(tuple(iterable), n))
|
|
|
|
|
|
2014-05-26 02:03:56 -03:00
|
|
|
|
def tail(n, iterable):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Return an iterator over the last n items."
|
2014-05-26 02:03:56 -03:00
|
|
|
|
# tail(3, 'ABCDEFG') --> E F G
|
|
|
|
|
return iter(collections.deque(iterable, maxlen=n))
|
|
|
|
|
|
2018-03-26 22:29:33 -03:00
|
|
|
|
def consume(iterator, n=None):
|
|
|
|
|
"Advance the iterator n-steps ahead. If n is None, consume entirely."
|
2010-12-01 19:45:20 -04:00
|
|
|
|
# Use functions that consume iterators at C speed.
|
|
|
|
|
if n is None:
|
|
|
|
|
# feed the entire iterator into a zero-length deque
|
|
|
|
|
collections.deque(iterator, maxlen=0)
|
|
|
|
|
else:
|
|
|
|
|
# advance to the empty slice starting at position n
|
|
|
|
|
next(islice(iterator, n, n), None)
|
|
|
|
|
|
|
|
|
|
def nth(iterable, n, default=None):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Returns the nth item or a default value."
|
2010-12-01 19:45:20 -04:00
|
|
|
|
return next(islice(iterable, n, None), default)
|
|
|
|
|
|
|
|
|
|
def quantify(iterable, pred=bool):
|
2023-09-19 09:39:44 -03:00
|
|
|
|
"Given a predicate that returns True or False, count the True results."
|
2010-12-01 19:45:20 -04:00
|
|
|
|
return sum(map(pred, iterable))
|
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def all_equal(iterable):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Returns True if all the elements are equal to each other."
|
2023-04-09 16:17:37 -03:00
|
|
|
|
g = groupby(iterable)
|
|
|
|
|
return next(g, True) and not next(g, False)
|
2020-12-25 20:43:20 -04:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def first_true(iterable, default=False, pred=None):
|
|
|
|
|
"""Returns the first true value in the iterable.
|
2023-03-21 15:21:57 -03:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
If no true value is found, returns *default*
|
2023-03-21 15:21:57 -03:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
If *pred* is not None, returns the first item
|
|
|
|
|
for which pred(item) is true.
|
2023-03-20 22:40:04 -03:00
|
|
|
|
|
|
|
|
|
"""
|
2023-04-09 16:17:37 -03:00
|
|
|
|
# first_true([a,b,c], x) --> a or b or c or x
|
|
|
|
|
# first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
|
|
|
|
|
return next(filter(pred, iterable), default)
|
2022-08-21 11:41:29 -03:00
|
|
|
|
|
2023-12-25 18:26:04 -04:00
|
|
|
|
def unique_everseen(iterable, key=None):
|
|
|
|
|
"List unique elements, preserving order. Remember all elements ever seen."
|
|
|
|
|
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
|
|
|
|
|
# unique_everseen('ABBcCAD', str.casefold) --> A B c D
|
|
|
|
|
seen = set()
|
|
|
|
|
if key is None:
|
|
|
|
|
for element in filterfalse(seen.__contains__, iterable):
|
|
|
|
|
seen.add(element)
|
|
|
|
|
yield element
|
|
|
|
|
else:
|
|
|
|
|
for element in iterable:
|
|
|
|
|
k = key(element)
|
|
|
|
|
if k not in seen:
|
|
|
|
|
seen.add(k)
|
|
|
|
|
yield element
|
|
|
|
|
|
|
|
|
|
def unique_justseen(iterable, key=None):
|
|
|
|
|
"List unique elements, preserving order. Remember only the element just seen."
|
|
|
|
|
# unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
|
|
|
|
|
# unique_justseen('ABBcCAD', str.casefold) --> A B c A D
|
|
|
|
|
if key is None:
|
|
|
|
|
return map(operator.itemgetter(0), groupby(iterable))
|
|
|
|
|
return map(next, map(operator.itemgetter(1), groupby(iterable, key)))
|
|
|
|
|
|
2023-09-03 00:25:13 -03:00
|
|
|
|
def iter_index(iterable, value, start=0, stop=None):
|
2022-10-18 16:09:34 -03:00
|
|
|
|
"Return indices where a value occurs in a sequence or iterable."
|
2022-10-15 14:43:58 -03:00
|
|
|
|
# iter_index('AABCADEAF', 'A') --> 0 1 4 7
|
2023-09-03 00:25:13 -03:00
|
|
|
|
seq_index = getattr(iterable, 'index', None)
|
|
|
|
|
if seq_index is None:
|
2022-10-18 16:09:34 -03:00
|
|
|
|
# Slow path for general iterables
|
2023-09-03 00:25:13 -03:00
|
|
|
|
it = islice(iterable, start, stop)
|
|
|
|
|
for i, element in enumerate(it, start):
|
|
|
|
|
if element is value or element == value:
|
|
|
|
|
yield i
|
2022-10-18 16:09:34 -03:00
|
|
|
|
else:
|
|
|
|
|
# Fast path for sequences
|
2023-09-11 23:04:28 -03:00
|
|
|
|
stop = len(iterable) if stop is None else stop
|
2022-10-18 16:09:34 -03:00
|
|
|
|
i = start - 1
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
2023-09-03 00:25:13 -03:00
|
|
|
|
yield (i := seq_index(value, i+1, stop))
|
2022-10-18 16:09:34 -03:00
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
2022-10-15 14:43:58 -03:00
|
|
|
|
|
2023-12-25 18:26:04 -04:00
|
|
|
|
def sliding_window(iterable, n):
|
|
|
|
|
"Collect data into overlapping fixed-length chunks or blocks."
|
|
|
|
|
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
|
|
|
|
|
it = iter(iterable)
|
|
|
|
|
window = collections.deque(islice(it, n-1), maxlen=n)
|
|
|
|
|
for x in it:
|
|
|
|
|
window.append(x)
|
|
|
|
|
yield tuple(window)
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2022-01-23 16:31:10 -04:00
|
|
|
|
def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Collect data into non-overlapping fixed-length chunks or blocks."
|
2022-01-23 16:31:10 -04:00
|
|
|
|
# grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
|
|
|
|
|
# grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
|
|
|
|
|
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
|
2010-12-01 19:45:20 -04:00
|
|
|
|
args = [iter(iterable)] * n
|
2023-12-13 14:11:52 -04:00
|
|
|
|
match incomplete:
|
|
|
|
|
case 'fill':
|
|
|
|
|
return zip_longest(*args, fillvalue=fillvalue)
|
|
|
|
|
case 'strict':
|
|
|
|
|
return zip(*args, strict=True)
|
|
|
|
|
case 'ignore':
|
|
|
|
|
return zip(*args)
|
|
|
|
|
case _:
|
|
|
|
|
raise ValueError('Expected fill, strict, or ignore')
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def roundrobin(*iterables):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Visit input iterables in a cycle until each is exhausted."
|
|
|
|
|
# roundrobin('ABC', 'D', 'EF') --> A D E B F C
|
2010-12-01 19:45:20 -04:00
|
|
|
|
# Recipe credited to George Sakkis
|
2017-11-21 04:23:34 -04:00
|
|
|
|
num_active = len(iterables)
|
2010-12-01 19:45:20 -04:00
|
|
|
|
nexts = cycle(iter(it).__next__ for it in iterables)
|
2017-11-21 04:23:34 -04:00
|
|
|
|
while num_active:
|
2010-12-01 19:45:20 -04:00
|
|
|
|
try:
|
|
|
|
|
for next in nexts:
|
|
|
|
|
yield next()
|
|
|
|
|
except StopIteration:
|
2017-11-21 04:23:34 -04:00
|
|
|
|
# Remove the iterator we just exhausted from the cycle.
|
|
|
|
|
num_active -= 1
|
|
|
|
|
nexts = cycle(islice(nexts, num_active))
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def partition(pred, iterable):
|
2023-05-25 00:31:02 -03:00
|
|
|
|
"""Partition entries into false entries and true entries.
|
|
|
|
|
|
|
|
|
|
If *pred* is slow, consider wrapping it with functools.lru_cache().
|
|
|
|
|
"""
|
2010-12-01 19:45:20 -04:00
|
|
|
|
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
|
|
|
|
|
t1, t2 = tee(iterable)
|
|
|
|
|
return filterfalse(pred, t1), filter(pred, t2)
|
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def subslices(seq):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Return all contiguous non-empty subslices of a sequence."
|
2023-04-09 16:17:37 -03:00
|
|
|
|
# subslices('ABCD') --> A AB ABC ABCD B BC BCD C CD D
|
|
|
|
|
slices = starmap(slice, combinations(range(len(seq) + 1), 2))
|
|
|
|
|
return map(operator.getitem, repeat(seq), slices)
|
|
|
|
|
|
2023-12-25 18:26:04 -04:00
|
|
|
|
def iter_except(func, exception, first=None):
|
|
|
|
|
""" Call a function repeatedly until an exception is raised.
|
|
|
|
|
|
|
|
|
|
Converts a call-until-exception interface to an iterator interface.
|
|
|
|
|
Like builtins.iter(func, sentinel) but uses an exception instead
|
|
|
|
|
of a sentinel to end the loop.
|
|
|
|
|
|
|
|
|
|
Priority queue iterator:
|
|
|
|
|
iter_except(functools.partial(heappop, h), IndexError)
|
|
|
|
|
|
|
|
|
|
Non-blocking dictionary iterator:
|
|
|
|
|
iter_except(d.popitem, KeyError)
|
|
|
|
|
|
|
|
|
|
Non-blocking deque iterator:
|
|
|
|
|
iter_except(d.popleft, IndexError)
|
|
|
|
|
|
|
|
|
|
Non-blocking iterator over a producer Queue:
|
|
|
|
|
iter_except(q.get_nowait, Queue.Empty)
|
|
|
|
|
|
|
|
|
|
Non-blocking set iterator:
|
|
|
|
|
iter_except(s.pop, KeyError)
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
if first is not None:
|
|
|
|
|
# For database APIs needing an initial call to db.first()
|
|
|
|
|
yield first()
|
|
|
|
|
while True:
|
|
|
|
|
yield func()
|
|
|
|
|
except exception:
|
|
|
|
|
pass
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
The following recipes have a more mathematical flavor:
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
.. testcode::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def powerset(iterable):
|
|
|
|
|
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
|
|
|
|
|
s = list(iterable)
|
|
|
|
|
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
|
|
|
|
|
|
|
|
|
|
def sum_of_squares(it):
|
|
|
|
|
"Add up the squares of the input values."
|
|
|
|
|
# sum_of_squares([10, 20, 30]) -> 1400
|
|
|
|
|
return math.sumprod(*tee(it))
|
2014-04-02 07:16:42 -03:00
|
|
|
|
|
2023-12-15 20:03:44 -04:00
|
|
|
|
def reshape(matrix, cols):
|
|
|
|
|
"Reshape a 2-D matrix to have a given number of columns."
|
|
|
|
|
# reshape([(0, 1), (2, 3), (4, 5)], 3) --> (0, 1, 2), (3, 4, 5)
|
2023-12-16 11:13:50 -04:00
|
|
|
|
return batched(chain.from_iterable(matrix), cols, strict=True)
|
2023-12-15 20:03:44 -04:00
|
|
|
|
|
|
|
|
|
def transpose(matrix):
|
|
|
|
|
"Swap the rows and columns of a 2-D matrix."
|
2023-04-09 16:17:37 -03:00
|
|
|
|
# transpose([(1, 2, 3), (11, 22, 33)]) --> (1, 11) (2, 22) (3, 33)
|
2023-12-15 20:03:44 -04:00
|
|
|
|
return zip(*matrix, strict=True)
|
2014-04-02 07:16:42 -03:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def matmul(m1, m2):
|
|
|
|
|
"Multiply two matrices."
|
2023-05-25 00:31:02 -03:00
|
|
|
|
# matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) --> (49, 80), (41, 60)
|
2023-04-09 16:17:37 -03:00
|
|
|
|
n = len(m2[0])
|
|
|
|
|
return batched(starmap(math.sumprod, product(m1, transpose(m2))), n)
|
|
|
|
|
|
|
|
|
|
def convolve(signal, kernel):
|
2023-09-19 09:39:44 -03:00
|
|
|
|
"""Discrete linear convolution of two iterables.
|
|
|
|
|
|
|
|
|
|
The kernel is fully consumed before the calculations begin.
|
|
|
|
|
The signal is consumed lazily and can be infinite.
|
|
|
|
|
|
|
|
|
|
Convolutions are mathematically commutative.
|
|
|
|
|
If the signal and kernel are swapped,
|
|
|
|
|
the output will be the same.
|
2014-04-02 07:16:42 -03:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
Article: https://betterexplained.com/articles/intuitive-convolution/
|
|
|
|
|
Video: https://www.youtube.com/watch?v=KuXjwB4LzSA
|
2014-04-02 07:16:42 -03:00
|
|
|
|
"""
|
2023-04-09 16:17:37 -03:00
|
|
|
|
# convolve(data, [0.25, 0.25, 0.25, 0.25]) --> Moving average (blur)
|
2023-09-19 09:39:44 -03:00
|
|
|
|
# convolve(data, [1/2, 0, -1/2]) --> 1st derivative estimate
|
|
|
|
|
# convolve(data, [1, -2, 1]) --> 2nd derivative estimate
|
2023-04-09 16:17:37 -03:00
|
|
|
|
kernel = tuple(kernel)[::-1]
|
|
|
|
|
n = len(kernel)
|
2023-04-28 14:25:50 -03:00
|
|
|
|
padded_signal = chain(repeat(0, n-1), signal, repeat(0, n-1))
|
2023-07-03 17:38:38 -03:00
|
|
|
|
windowed_signal = sliding_window(padded_signal, n)
|
|
|
|
|
return map(math.sumprod, repeat(kernel), windowed_signal)
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
|
|
|
|
def polynomial_from_roots(roots):
|
|
|
|
|
"""Compute a polynomial's coefficients from its roots.
|
|
|
|
|
|
|
|
|
|
(x - 5) (x + 4) (x - 3) expands to: x³ -4x² -17x + 60
|
|
|
|
|
"""
|
|
|
|
|
# polynomial_from_roots([5, -4, 3]) --> [1, -4, -17, 60]
|
2023-04-28 14:25:50 -03:00
|
|
|
|
factors = zip(repeat(1), map(operator.neg, roots))
|
|
|
|
|
return list(functools.reduce(convolve, factors, [1]))
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
|
|
|
|
def polynomial_eval(coefficients, x):
|
|
|
|
|
"""Evaluate a polynomial at a specific value.
|
|
|
|
|
|
|
|
|
|
Computes with better numeric stability than Horner's method.
|
|
|
|
|
"""
|
|
|
|
|
# Evaluate x³ -4x² -17x + 60 at x = 2.5
|
|
|
|
|
# polynomial_eval([1, -4, -17, 60], x=2.5) --> 8.125
|
|
|
|
|
n = len(coefficients)
|
2023-09-19 09:39:44 -03:00
|
|
|
|
if not n:
|
|
|
|
|
return type(x)(0)
|
2023-04-09 16:17:37 -03:00
|
|
|
|
powers = map(pow, repeat(x), reversed(range(n)))
|
|
|
|
|
return math.sumprod(coefficients, powers)
|
2014-04-02 07:16:42 -03:00
|
|
|
|
|
2023-05-25 00:31:02 -03:00
|
|
|
|
def polynomial_derivative(coefficients):
|
|
|
|
|
"""Compute the first derivative of a polynomial.
|
|
|
|
|
|
|
|
|
|
f(x) = x³ -4x² -17x + 60
|
|
|
|
|
f'(x) = 3x² -8x -17
|
|
|
|
|
"""
|
|
|
|
|
# polynomial_derivative([1, -4, -17, 60]) -> [3, -8, -17]
|
|
|
|
|
n = len(coefficients)
|
|
|
|
|
powers = reversed(range(1, n))
|
|
|
|
|
return list(map(operator.mul, coefficients, powers))
|
|
|
|
|
|
2023-09-19 09:39:44 -03:00
|
|
|
|
def sieve(n):
|
|
|
|
|
"Primes less than n."
|
|
|
|
|
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
|
|
|
|
|
if n > 2:
|
|
|
|
|
yield 2
|
|
|
|
|
start = 3
|
|
|
|
|
data = bytearray((0, 1)) * (n // 2)
|
|
|
|
|
limit = math.isqrt(n) + 1
|
|
|
|
|
for p in iter_index(data, 1, start, limit):
|
|
|
|
|
yield from iter_index(data, 1, start, p*p)
|
|
|
|
|
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
|
|
|
|
|
start = p*p
|
|
|
|
|
yield from iter_index(data, 1, start)
|
|
|
|
|
|
|
|
|
|
def factor(n):
|
|
|
|
|
"Prime factors of n."
|
|
|
|
|
# factor(99) --> 3 3 11
|
|
|
|
|
# factor(1_000_000_000_000_007) --> 47 59 360620266859
|
|
|
|
|
# factor(1_000_000_000_000_403) --> 1000000000000403
|
|
|
|
|
for prime in sieve(math.isqrt(n) + 1):
|
|
|
|
|
while not n % prime:
|
|
|
|
|
yield prime
|
|
|
|
|
n //= prime
|
|
|
|
|
if n == 1:
|
|
|
|
|
return
|
|
|
|
|
if n > 1:
|
|
|
|
|
yield n
|
|
|
|
|
|
2023-12-14 15:15:29 -04:00
|
|
|
|
def totient(n):
|
|
|
|
|
"Count of natural numbers up to n that are coprime to n."
|
|
|
|
|
# https://mathworld.wolfram.com/TotientFunction.html
|
|
|
|
|
# totient(12) --> 4 because len([1, 5, 7, 11]) == 4
|
|
|
|
|
for p in unique_justseen(factor(n)):
|
2024-01-08 15:16:22 -04:00
|
|
|
|
n -= n // p
|
2023-12-14 15:15:29 -04:00
|
|
|
|
return n
|
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
2022-01-25 08:56:53 -04:00
|
|
|
|
.. doctest::
|
|
|
|
|
:hide:
|
|
|
|
|
|
|
|
|
|
These examples no longer appear in the docs but are guaranteed
|
|
|
|
|
to keep working.
|
|
|
|
|
|
|
|
|
|
>>> amounts = [120.15, 764.05, 823.14]
|
|
|
|
|
>>> for checknum, amount in zip(count(1200), amounts):
|
|
|
|
|
... print('Check %d is for $%.2f' % (checknum, amount))
|
|
|
|
|
...
|
|
|
|
|
Check 1200 is for $120.15
|
|
|
|
|
Check 1201 is for $764.05
|
|
|
|
|
Check 1202 is for $823.14
|
|
|
|
|
|
|
|
|
|
>>> import operator
|
|
|
|
|
>>> for cube in map(operator.pow, range(1,4), repeat(3)):
|
|
|
|
|
... print(cube)
|
|
|
|
|
...
|
|
|
|
|
1
|
|
|
|
|
8
|
|
|
|
|
27
|
|
|
|
|
|
|
|
|
|
>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
|
|
|
|
|
>>> for name in islice(reportlines, 3, None, 2):
|
|
|
|
|
... print(name.title())
|
|
|
|
|
...
|
|
|
|
|
Alex
|
|
|
|
|
Laura
|
|
|
|
|
Martin
|
|
|
|
|
Walter
|
|
|
|
|
Samuele
|
|
|
|
|
|
|
|
|
|
>>> from operator import itemgetter
|
|
|
|
|
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
|
|
|
|
|
>>> di = sorted(sorted(d.items()), key=itemgetter(1))
|
|
|
|
|
>>> for k, g in groupby(di, itemgetter(1)):
|
|
|
|
|
... print(k, list(map(itemgetter(0), g)))
|
|
|
|
|
...
|
|
|
|
|
1 ['a', 'c', 'e']
|
|
|
|
|
2 ['b', 'd', 'f']
|
|
|
|
|
3 ['g']
|
|
|
|
|
|
|
|
|
|
# Find runs of consecutive numbers using groupby. The key to the solution
|
|
|
|
|
# is differencing with a range so that consecutive numbers all appear in
|
|
|
|
|
# same group.
|
|
|
|
|
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
|
|
|
|
|
>>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
|
|
|
|
|
... print(list(map(operator.itemgetter(1), g)))
|
|
|
|
|
...
|
|
|
|
|
[1]
|
|
|
|
|
[4, 5, 6]
|
|
|
|
|
[10]
|
|
|
|
|
[15, 16, 17, 18]
|
|
|
|
|
[22]
|
|
|
|
|
[25, 26, 27, 28]
|
|
|
|
|
|
|
|
|
|
Now, we test all of the itertool recipes
|
|
|
|
|
|
|
|
|
|
>>> take(10, count())
|
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
|
|
|
|
|
>>> list(prepend(1, [2, 3, 4]))
|
|
|
|
|
[1, 2, 3, 4]
|
|
|
|
|
|
|
|
|
|
>>> list(enumerate('abc'))
|
|
|
|
|
[(0, 'a'), (1, 'b'), (2, 'c')]
|
|
|
|
|
|
|
|
|
|
>>> list(islice(tabulate(lambda x: 2*x), 4))
|
|
|
|
|
[0, 2, 4, 6]
|
|
|
|
|
|
|
|
|
|
>>> list(tail(3, 'ABCDEFG'))
|
|
|
|
|
['E', 'F', 'G']
|
|
|
|
|
|
|
|
|
|
>>> it = iter(range(10))
|
|
|
|
|
>>> consume(it, 3)
|
|
|
|
|
>>> next(it)
|
|
|
|
|
3
|
|
|
|
|
>>> consume(it)
|
|
|
|
|
>>> next(it, 'Done')
|
|
|
|
|
'Done'
|
|
|
|
|
|
|
|
|
|
>>> nth('abcde', 3)
|
|
|
|
|
'd'
|
|
|
|
|
|
|
|
|
|
>>> nth('abcde', 9) is None
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
|
|
|
|
|
[True, True, True, False, False]
|
|
|
|
|
|
|
|
|
|
>>> quantify(range(99), lambda x: x%2==0)
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
|
>>> quantify([True, False, False, True, True])
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
|
>>> quantify(range(12), pred=lambda x: x%2==1)
|
|
|
|
|
6
|
|
|
|
|
|
|
|
|
|
>>> a = [[1, 2, 3], [4, 5, 6]]
|
|
|
|
|
>>> list(flatten(a))
|
|
|
|
|
[1, 2, 3, 4, 5, 6]
|
|
|
|
|
|
|
|
|
|
>>> list(repeatfunc(pow, 5, 2, 3))
|
|
|
|
|
[8, 8, 8, 8, 8]
|
|
|
|
|
|
|
|
|
|
>>> take(5, map(int, repeatfunc(random.random)))
|
|
|
|
|
[0, 0, 0, 0, 0]
|
|
|
|
|
|
|
|
|
|
>>> list(ncycles('abc', 3))
|
|
|
|
|
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
|
|
|
|
|
|
2023-01-13 21:01:22 -04:00
|
|
|
|
>>> sum_of_squares([10, 20, 30])
|
|
|
|
|
1400
|
|
|
|
|
|
2023-12-15 20:03:44 -04:00
|
|
|
|
>>> list(reshape([(0, 1), (2, 3), (4, 5)], 3))
|
|
|
|
|
[(0, 1, 2), (3, 4, 5)]
|
|
|
|
|
>>> M = [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)]
|
|
|
|
|
>>> list(reshape(M, 1))
|
|
|
|
|
[(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,), (10,), (11,)]
|
|
|
|
|
>>> list(reshape(M, 2))
|
|
|
|
|
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)]
|
|
|
|
|
>>> list(reshape(M, 3))
|
|
|
|
|
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)]
|
|
|
|
|
>>> list(reshape(M, 4))
|
|
|
|
|
[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)]
|
2023-12-16 11:13:50 -04:00
|
|
|
|
>>> list(reshape(M, 5))
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
...
|
|
|
|
|
ValueError: batched(): incomplete batch
|
2023-12-15 20:03:44 -04:00
|
|
|
|
>>> list(reshape(M, 6))
|
|
|
|
|
[(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)]
|
|
|
|
|
>>> list(reshape(M, 12))
|
|
|
|
|
[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)]
|
|
|
|
|
|
2023-01-13 21:01:22 -04:00
|
|
|
|
>>> list(transpose([(1, 2, 3), (11, 22, 33)]))
|
|
|
|
|
[(1, 11), (2, 22), (3, 33)]
|
|
|
|
|
|
|
|
|
|
>>> list(matmul([(7, 5), (3, 5)], [[2, 5], [7, 9]]))
|
|
|
|
|
[(49, 80), (41, 60)]
|
|
|
|
|
>>> list(matmul([[2, 5], [7, 9], [3, 4]], [[7, 11, 5, 4, 9], [3, 5, 2, 6, 3]]))
|
|
|
|
|
[(29, 47, 20, 38, 33), (76, 122, 53, 82, 90), (33, 53, 23, 36, 39)]
|
|
|
|
|
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> data = [20, 40, 24, 32, 20, 28, 16]
|
|
|
|
|
>>> list(convolve(data, [0.25, 0.25, 0.25, 0.25]))
|
|
|
|
|
[5.0, 15.0, 21.0, 29.0, 29.0, 26.0, 24.0, 16.0, 11.0, 4.0]
|
|
|
|
|
>>> list(convolve(data, [1, -1]))
|
|
|
|
|
[20, 20, -16, 8, -12, 8, -12, -16]
|
|
|
|
|
>>> list(convolve(data, [1, -2, 1]))
|
|
|
|
|
[20, 0, -36, 24, -20, 20, -20, -4, 16]
|
|
|
|
|
|
2023-03-20 19:14:29 -03:00
|
|
|
|
>>> from fractions import Fraction
|
|
|
|
|
>>> from decimal import Decimal
|
|
|
|
|
>>> polynomial_eval([1, -4, -17, 60], x=2)
|
|
|
|
|
18
|
|
|
|
|
>>> x = 2; x**3 - 4*x**2 -17*x + 60
|
|
|
|
|
18
|
|
|
|
|
>>> polynomial_eval([1, -4, -17, 60], x=2.5)
|
|
|
|
|
8.125
|
|
|
|
|
>>> x = 2.5; x**3 - 4*x**2 -17*x + 60
|
|
|
|
|
8.125
|
|
|
|
|
>>> polynomial_eval([1, -4, -17, 60], x=Fraction(2, 3))
|
|
|
|
|
Fraction(1274, 27)
|
|
|
|
|
>>> x = Fraction(2, 3); x**3 - 4*x**2 -17*x + 60
|
|
|
|
|
Fraction(1274, 27)
|
|
|
|
|
>>> polynomial_eval([1, -4, -17, 60], x=Decimal('1.75'))
|
|
|
|
|
Decimal('23.359375')
|
|
|
|
|
>>> x = Decimal('1.75'); x**3 - 4*x**2 -17*x + 60
|
|
|
|
|
Decimal('23.359375')
|
|
|
|
|
>>> polynomial_eval([], 2)
|
|
|
|
|
0
|
|
|
|
|
>>> polynomial_eval([], 2.5)
|
|
|
|
|
0.0
|
|
|
|
|
>>> polynomial_eval([], Fraction(2, 3))
|
|
|
|
|
Fraction(0, 1)
|
|
|
|
|
>>> polynomial_eval([], Decimal('1.75'))
|
2023-09-19 09:39:44 -03:00
|
|
|
|
Decimal('0')
|
2023-03-20 19:14:29 -03:00
|
|
|
|
>>> polynomial_eval([11], 7) == 11
|
|
|
|
|
True
|
|
|
|
|
>>> polynomial_eval([11, 2], 7) == 11 * 7 + 2
|
|
|
|
|
True
|
|
|
|
|
|
2022-08-21 11:41:29 -03:00
|
|
|
|
>>> polynomial_from_roots([5, -4, 3])
|
|
|
|
|
[1, -4, -17, 60]
|
|
|
|
|
>>> factored = lambda x: (x - 5) * (x + 4) * (x - 3)
|
|
|
|
|
>>> expanded = lambda x: x**3 -4*x**2 -17*x + 60
|
|
|
|
|
>>> all(factored(x) == expanded(x) for x in range(-10, 11))
|
|
|
|
|
True
|
|
|
|
|
|
2023-05-25 00:31:02 -03:00
|
|
|
|
>>> polynomial_derivative([1, -4, -17, 60])
|
|
|
|
|
[3, -8, -17]
|
|
|
|
|
|
2022-10-15 14:43:58 -03:00
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A'))
|
|
|
|
|
[0, 1, 4, 7]
|
|
|
|
|
>>> list(iter_index('AABCADEAF', 'B'))
|
|
|
|
|
[2]
|
|
|
|
|
>>> list(iter_index('AABCADEAF', 'X'))
|
|
|
|
|
[]
|
|
|
|
|
>>> list(iter_index('', 'X'))
|
|
|
|
|
[]
|
2022-10-18 16:09:34 -03:00
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A', 1))
|
|
|
|
|
[1, 4, 7]
|
|
|
|
|
>>> list(iter_index(iter('AABCADEAF'), 'A', 1))
|
|
|
|
|
[1, 4, 7]
|
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A', 2))
|
|
|
|
|
[4, 7]
|
|
|
|
|
>>> list(iter_index(iter('AABCADEAF'), 'A', 2))
|
|
|
|
|
[4, 7]
|
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A', 10))
|
|
|
|
|
[]
|
|
|
|
|
>>> list(iter_index(iter('AABCADEAF'), 'A', 10))
|
|
|
|
|
[]
|
2023-09-03 00:25:13 -03:00
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A', 1, 7))
|
|
|
|
|
[1, 4]
|
|
|
|
|
>>> list(iter_index(iter('AABCADEAF'), 'A', 1, 7))
|
|
|
|
|
[1, 4]
|
|
|
|
|
>>> # Verify that ValueErrors not swallowed (gh-107208)
|
|
|
|
|
>>> def assert_no_value(iterable, forbidden_value):
|
|
|
|
|
... for item in iterable:
|
|
|
|
|
... if item == forbidden_value:
|
|
|
|
|
... raise ValueError
|
|
|
|
|
... yield item
|
|
|
|
|
...
|
|
|
|
|
>>> list(iter_index(assert_no_value('AABCADEAF', 'B'), 'A'))
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
...
|
|
|
|
|
ValueError
|
2023-09-11 23:04:28 -03:00
|
|
|
|
>>> # Verify that both paths can find identical NaN values
|
|
|
|
|
>>> x = float('NaN')
|
|
|
|
|
>>> y = float('NaN')
|
|
|
|
|
>>> list(iter_index([0, x, x, y, 0], x))
|
|
|
|
|
[1, 2]
|
|
|
|
|
>>> list(iter_index(iter([0, x, x, y, 0]), x))
|
|
|
|
|
[1, 2]
|
|
|
|
|
>>> # Test list input. Lists do not support None for the stop argument
|
|
|
|
|
>>> list(iter_index(list('AABCADEAF'), 'A'))
|
|
|
|
|
[0, 1, 4, 7]
|
2022-10-15 14:43:58 -03:00
|
|
|
|
|
2022-09-13 23:10:47 -03:00
|
|
|
|
>>> list(sieve(30))
|
|
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
|
2022-10-07 05:37:21 -03:00
|
|
|
|
>>> small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
|
|
|
|
|
>>> all(list(sieve(n)) == [p for p in small_primes if p < n] for n in range(101))
|
2022-09-17 13:31:04 -03:00
|
|
|
|
True
|
2022-09-13 23:10:47 -03:00
|
|
|
|
>>> len(list(sieve(100)))
|
|
|
|
|
25
|
|
|
|
|
>>> len(list(sieve(1_000)))
|
|
|
|
|
168
|
|
|
|
|
>>> len(list(sieve(10_000)))
|
|
|
|
|
1229
|
|
|
|
|
>>> len(list(sieve(100_000)))
|
|
|
|
|
9592
|
|
|
|
|
>>> len(list(sieve(1_000_000)))
|
|
|
|
|
78498
|
2022-09-17 13:31:04 -03:00
|
|
|
|
>>> carmichael = {561, 1105, 1729, 2465, 2821, 6601, 8911} # https://oeis.org/A002997
|
|
|
|
|
>>> set(sieve(10_000)).isdisjoint(carmichael)
|
|
|
|
|
True
|
2022-09-13 23:10:47 -03:00
|
|
|
|
|
2023-07-16 23:37:07 -03:00
|
|
|
|
>>> list(factor(99)) # Code example 1
|
|
|
|
|
[3, 3, 11]
|
|
|
|
|
>>> list(factor(1_000_000_000_000_007)) # Code example 2
|
|
|
|
|
[47, 59, 360620266859]
|
|
|
|
|
>>> list(factor(1_000_000_000_000_403)) # Code example 3
|
|
|
|
|
[1000000000000403]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(0))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(1))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(2))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(3))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[3]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(4))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2, 2]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(5))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[5]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(6))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2, 3]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(7))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[7]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(8))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2, 2, 2]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(9))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[3, 3]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(10))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2, 5]
|
2022-12-28 16:29:27 -04:00
|
|
|
|
>>> list(factor(128_884_753_939)) # large prime
|
|
|
|
|
[128884753939]
|
|
|
|
|
>>> list(factor(999953 * 999983)) # large semiprime
|
2022-12-28 07:13:58 -04:00
|
|
|
|
[999953, 999983]
|
2022-12-28 16:29:27 -04:00
|
|
|
|
>>> list(factor(6 ** 20)) == [2] * 20 + [3] * 20 # large power
|
|
|
|
|
True
|
|
|
|
|
>>> list(factor(909_909_090_909)) # large multiterm composite
|
|
|
|
|
[3, 3, 7, 13, 13, 751, 113797]
|
|
|
|
|
>>> math.prod([3, 3, 7, 13, 13, 751, 113797])
|
|
|
|
|
909909090909
|
|
|
|
|
>>> all(math.prod(factor(n)) == n for n in range(1, 2_000))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
True
|
2022-12-28 16:29:27 -04:00
|
|
|
|
>>> all(set(factor(n)) <= set(sieve(n+1)) for n in range(2_000))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
True
|
2022-12-28 16:29:27 -04:00
|
|
|
|
>>> all(list(factor(n)) == sorted(factor(n)) for n in range(2_000))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
True
|
|
|
|
|
|
2023-12-14 15:15:29 -04:00
|
|
|
|
>>> totient(0) # https://www.wolframalpha.com/input?i=totient+0
|
|
|
|
|
0
|
|
|
|
|
>>> first_totients = [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6,
|
|
|
|
|
... 18, 8, 12, 10, 22, 8, 20, 12, 18, 12, 28, 8, 30, 16, 20, 16, 24, 12, 36, 18,
|
|
|
|
|
... 24, 16, 40, 12, 42, 20, 24, 22, 46, 16, 42, 20, 32, 24, 52, 18, 40, 24, 36,
|
|
|
|
|
... 28, 58, 16, 60, 30, 36, 32, 48, 20, 66, 32, 44] # https://oeis.org/A000010
|
|
|
|
|
...
|
|
|
|
|
>>> list(map(totient, range(1, 70))) == first_totients
|
|
|
|
|
True
|
|
|
|
|
>>> reference_totient = lambda n: sum(math.gcd(t, n) == 1 for t in range(1, n+1))
|
|
|
|
|
>>> all(totient(n) == reference_totient(n) for n in range(1000))
|
|
|
|
|
True
|
|
|
|
|
>>> totient(128_884_753_939) == 128_884_753_938 # large prime
|
|
|
|
|
True
|
|
|
|
|
>>> totient(999953 * 999983) == 999952 * 999982 # large semiprime
|
|
|
|
|
True
|
|
|
|
|
>>> totient(6 ** 20) == 1 * 2**19 * 2 * 3**19 # repeated primes
|
|
|
|
|
True
|
|
|
|
|
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')]))
|
|
|
|
|
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
|
|
|
|
|
|
|
|
|
|
>>> random.seed(85753098575309)
|
|
|
|
|
>>> list(repeatfunc(random.random, 3))
|
|
|
|
|
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]
|
|
|
|
|
>>> list(repeatfunc(chr, 3, 65))
|
|
|
|
|
['A', 'A', 'A']
|
|
|
|
|
>>> list(repeatfunc(pow, 3, 2, 5))
|
|
|
|
|
[32, 32, 32]
|
|
|
|
|
|
|
|
|
|
>>> list(grouper('abcdefg', 3, fillvalue='x'))
|
|
|
|
|
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
|
|
|
|
|
|
|
|
|
|
>>> it = grouper('abcdefg', 3, incomplete='strict')
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('a', 'b', 'c')
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('d', 'e', 'f')
|
|
|
|
|
>>> next(it)
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
...
|
|
|
|
|
ValueError: zip() argument 2 is shorter than argument 1
|
|
|
|
|
|
|
|
|
|
>>> list(grouper('abcdefg', n=3, incomplete='ignore'))
|
|
|
|
|
[('a', 'b', 'c'), ('d', 'e', 'f')]
|
|
|
|
|
|
2023-06-06 15:44:45 -03:00
|
|
|
|
>>> list(sliding_window('ABCDEFG', 1))
|
|
|
|
|
[('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 2))
|
|
|
|
|
[('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'F'), ('F', 'G')]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 3))
|
|
|
|
|
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> list(sliding_window('ABCDEFG', 4))
|
|
|
|
|
[('A', 'B', 'C', 'D'), ('B', 'C', 'D', 'E'), ('C', 'D', 'E', 'F'), ('D', 'E', 'F', 'G')]
|
2023-06-06 15:44:45 -03:00
|
|
|
|
>>> list(sliding_window('ABCDEFG', 5))
|
|
|
|
|
[('A', 'B', 'C', 'D', 'E'), ('B', 'C', 'D', 'E', 'F'), ('C', 'D', 'E', 'F', 'G')]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 6))
|
|
|
|
|
[('A', 'B', 'C', 'D', 'E', 'F'), ('B', 'C', 'D', 'E', 'F', 'G')]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 7))
|
|
|
|
|
[('A', 'B', 'C', 'D', 'E', 'F', 'G')]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 8))
|
|
|
|
|
[]
|
|
|
|
|
>>> try:
|
|
|
|
|
... list(sliding_window('ABCDEFG', -1))
|
|
|
|
|
... except ValueError:
|
|
|
|
|
... 'zero or negative n not supported'
|
|
|
|
|
...
|
|
|
|
|
'zero or negative n not supported'
|
|
|
|
|
>>> try:
|
|
|
|
|
... list(sliding_window('ABCDEFG', 0))
|
|
|
|
|
... except ValueError:
|
|
|
|
|
... 'zero or negative n not supported'
|
|
|
|
|
...
|
|
|
|
|
'zero or negative n not supported'
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> list(roundrobin('abc', 'd', 'ef'))
|
|
|
|
|
['a', 'd', 'e', 'b', 'f', 'c']
|
|
|
|
|
|
|
|
|
|
>>> def is_odd(x):
|
|
|
|
|
... return x % 2 == 1
|
|
|
|
|
|
|
|
|
|
>>> evens, odds = partition(is_odd, range(10))
|
|
|
|
|
>>> list(evens)
|
|
|
|
|
[0, 2, 4, 6, 8]
|
|
|
|
|
>>> list(odds)
|
|
|
|
|
[1, 3, 5, 7, 9]
|
|
|
|
|
|
2022-02-02 00:18:52 -04:00
|
|
|
|
>>> list(subslices('ABCD'))
|
|
|
|
|
['A', 'AB', 'ABC', 'ABCD', 'B', 'BC', 'BCD', 'C', 'CD', 'D']
|
|
|
|
|
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> list(powerset([1,2,3]))
|
|
|
|
|
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
|
|
|
|
|
|
|
|
|
|
>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
>>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len)
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
>>> list(unique_everseen('AAAABBBCCDAABBB'))
|
|
|
|
|
['A', 'B', 'C', 'D']
|
2023-12-25 18:26:04 -04:00
|
|
|
|
>>> list(unique_everseen('ABBCcAD', str.casefold))
|
2022-01-25 08:56:53 -04:00
|
|
|
|
['A', 'B', 'C', 'D']
|
2023-12-25 18:26:04 -04:00
|
|
|
|
>>> list(unique_everseen('ABBcCAD', str.casefold))
|
2022-12-30 22:36:38 -04:00
|
|
|
|
['A', 'B', 'c', 'D']
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> list(unique_justseen('AAAABBBCCDAABBB'))
|
|
|
|
|
['A', 'B', 'C', 'D', 'A', 'B']
|
2023-12-25 18:26:04 -04:00
|
|
|
|
>>> list(unique_justseen('ABBCcAD', str.casefold))
|
2022-01-25 08:56:53 -04:00
|
|
|
|
['A', 'B', 'C', 'A', 'D']
|
2023-12-25 18:26:04 -04:00
|
|
|
|
>>> list(unique_justseen('ABBcCAD', str.casefold))
|
2022-12-30 22:36:38 -04:00
|
|
|
|
['A', 'B', 'c', 'A', 'D']
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> d = dict(a=1, b=2, c=3)
|
|
|
|
|
>>> it = iter_except(d.popitem, KeyError)
|
|
|
|
|
>>> d['d'] = 4
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('d', 4)
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('c', 3)
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('b', 2)
|
|
|
|
|
>>> d['e'] = 5
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('e', 5)
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('a', 1)
|
|
|
|
|
>>> next(it, 'empty')
|
|
|
|
|
'empty'
|
|
|
|
|
|
|
|
|
|
>>> first_true('ABC0DEF1', '9', str.isdigit)
|
|
|
|
|
'0'
|
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
|
:hide:
|
|
|
|
|
|
|
|
|
|
# Old recipes and their tests which are guaranteed to continue to work.
|
|
|
|
|
|
|
|
|
|
def sumprod(vec1, vec2):
|
|
|
|
|
"Compute a sum of products."
|
|
|
|
|
return sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))
|
|
|
|
|
|
|
|
|
|
def dotproduct(vec1, vec2):
|
|
|
|
|
return sum(map(operator.mul, vec1, vec2))
|
|
|
|
|
|
|
|
|
|
def pad_none(iterable):
|
|
|
|
|
"""Returns the sequence elements and then returns None indefinitely.
|
|
|
|
|
|
|
|
|
|
Useful for emulating the behavior of the built-in map() function.
|
|
|
|
|
"""
|
|
|
|
|
return chain(iterable, repeat(None))
|
|
|
|
|
|
|
|
|
|
def triplewise(iterable):
|
|
|
|
|
"Return overlapping triplets from an iterable"
|
|
|
|
|
# triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG
|
|
|
|
|
for (a, _), (b, c) in pairwise(pairwise(iterable)):
|
|
|
|
|
yield a, b, c
|
|
|
|
|
|
2023-12-14 16:36:40 -04:00
|
|
|
|
def nth_combination(iterable, r, index):
|
|
|
|
|
"Equivalent to list(combinations(iterable, r))[index]"
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
c = math.comb(n, r)
|
|
|
|
|
if index < 0:
|
|
|
|
|
index += c
|
|
|
|
|
if index < 0 or index >= c:
|
|
|
|
|
raise IndexError
|
|
|
|
|
result = []
|
|
|
|
|
while r:
|
|
|
|
|
c, n, r = c*r//n, n-1, r-1
|
|
|
|
|
while index >= c:
|
|
|
|
|
index -= c
|
|
|
|
|
c, n = c*(n-r)//n, n-1
|
|
|
|
|
result.append(pool[-1-n])
|
|
|
|
|
return tuple(result)
|
|
|
|
|
|
2024-02-25 08:32:14 -04:00
|
|
|
|
def before_and_after(predicate, it):
|
|
|
|
|
""" Variant of takewhile() that allows complete
|
|
|
|
|
access to the remainder of the iterator.
|
|
|
|
|
|
|
|
|
|
>>> it = iter('ABCdEfGhI')
|
|
|
|
|
>>> all_upper, remainder = before_and_after(str.isupper, it)
|
|
|
|
|
>>> ''.join(all_upper)
|
|
|
|
|
'ABC'
|
|
|
|
|
>>> ''.join(remainder) # takewhile() would lose the 'd'
|
|
|
|
|
'dEfGhI'
|
|
|
|
|
|
|
|
|
|
Note that the true iterator must be fully consumed
|
|
|
|
|
before the remainder iterator can generate valid results.
|
|
|
|
|
"""
|
|
|
|
|
it = iter(it)
|
|
|
|
|
transition = []
|
|
|
|
|
|
|
|
|
|
def true_iterator():
|
|
|
|
|
for elem in it:
|
|
|
|
|
if predicate(elem):
|
|
|
|
|
yield elem
|
|
|
|
|
else:
|
|
|
|
|
transition.append(elem)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
return true_iterator(), chain(transition, it)
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
:hide:
|
|
|
|
|
|
|
|
|
|
>>> dotproduct([1,2,3], [4,5,6])
|
|
|
|
|
32
|
|
|
|
|
|
|
|
|
|
>>> sumprod([1,2,3], [4,5,6])
|
|
|
|
|
32
|
|
|
|
|
|
|
|
|
|
>>> list(islice(pad_none('abc'), 0, 6))
|
|
|
|
|
['a', 'b', 'c', None, None, None]
|
|
|
|
|
|
|
|
|
|
>>> list(triplewise('ABCDEFG'))
|
|
|
|
|
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
|
2023-12-14 16:36:40 -04:00
|
|
|
|
|
|
|
|
|
>>> population = 'ABCDEFGH'
|
|
|
|
|
>>> for r in range(len(population) + 1):
|
|
|
|
|
... seq = list(combinations(population, r))
|
|
|
|
|
... for i in range(len(seq)):
|
|
|
|
|
... assert nth_combination(population, r, i) == seq[i]
|
|
|
|
|
... for i in range(-len(seq), 0):
|
|
|
|
|
... assert nth_combination(population, r, i) == seq[i]
|
|
|
|
|
|
|
|
|
|
>>> iterable = 'abcde'
|
|
|
|
|
>>> r = 3
|
|
|
|
|
>>> combos = list(combinations(iterable, r))
|
|
|
|
|
>>> all(nth_combination(iterable, r, i) == comb for i, comb in enumerate(combos))
|
|
|
|
|
True
|
2024-02-25 08:32:14 -04:00
|
|
|
|
|
|
|
|
|
>>> it = iter('ABCdEfGhI')
|
|
|
|
|
>>> all_upper, remainder = before_and_after(str.isupper, it)
|
|
|
|
|
>>> ''.join(all_upper)
|
|
|
|
|
'ABC'
|
|
|
|
|
>>> ''.join(remainder)
|
|
|
|
|
'dEfGhI'
|