Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-12-01 Thread Sjoerd Job Postmus
On Wed, Nov 30, 2016 at 02:32:54PM -0600, Nick Timkovich wrote: > a generator with known length that's not indexable (a rare beast?). Not as rare as you might think: >>> k = set(range(10)) >>> len(k) 10 >>> k[3] Traceback (most recent call last): File "", line 1, in TypeError: 'set' object

Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Steven D'Aprano
On Wed, Nov 30, 2016 at 11:57:46AM -0800, Chris Kaynor wrote: > Consider that this code will not produce the "correct" results (for a > reasonable definition of correct): > > a = (i for i in range(100)) # Pretend this does something more > interesting, and isn't a trivial generator - maybe a

Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Bernardo Sulzbach
On 2016-11-30 19:11, Chris Kaynor wrote: All that said, I would not be opposed to Python including a random.reservoir_choice (probably not the best name) function *in addition* to random.choice. The algorithm has its uses, but enough drawbacks and gotchas that it likely is not a good candidate

Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Chris Kaynor
On Wed, Nov 30, 2016 at 12:21 PM, Bernardo Sulzbach wrote: > On 2016-11-30 17:57, Chris Kaynor wrote: >> >> On Wed, Nov 30, 2016 at 11:52 AM, Chris Kaynor >> wrote: >>> >>> There are also issues with how it should behave on iterables that >>>

Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Nick Timkovich
Is the goal to allow them to consume a finite generator of *unknown* length (requires reservoir sampling https://en.wikipedia.org/wiki/Reservoir_sampling with N random calls, which seemed to be the rub before?) or just consume a generator with known length that's not indexable (a rare beast?).

Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Bernardo Sulzbach
On 2016-11-30 17:57, Chris Kaynor wrote: On Wed, Nov 30, 2016 at 11:52 AM, Chris Kaynor wrote: There are also issues with how it should behave on iterables that cannot be re-iterated (eg, random.choice will consume the iterator, and could only be called once safely).

Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Chris Kaynor
On Wed, Nov 30, 2016 at 11:52 AM, Chris Kaynor wrote: > There are also issues with how it should behave on iterables that > cannot be re-iterated (eg, random.choice will consume the iterator, > and could only be called once safely). I meant to include a sample in my

Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Chris Kaynor
This was also brought up back in April: https://mail.python.org/pipermail//python-ideas/2016-April/039707.html It got a few replies from Guido (https://mail.python.org/pipermail//python-ideas/2016-April/039713.html for one of them). It seems the idea got dropped due to problems with making it

Re: [Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Bernardo Sulzbach
On 2016-11-30 17:25, Random832 wrote: Currently these functions fail if the supplied object has no len(). There are algorithms for this task that can work on any finite iterator (for example, files as a stream of lines), and the functions could fall back to these if there is no len(). I like

[Python-ideas] Allow random.choice, random.sample to work on iterators

2016-11-30 Thread Random832
Currently these functions fail if the supplied object has no len(). There are algorithms for this task that can work on any finite iterator (for example, files as a stream of lines), and the functions could fall back to these if there is no len(). ___