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

Re: [Python-ideas] Add optional defaults to namedtuple

2016-11-30 Thread Joao S. O. Bueno
On 30 November 2016 at 13:09, Ethan Furman wrote: > But even more readable than that is using the NamedTuple class from my aenum > [3] library (and on SO as [3]): > > --> from aenum import NamedTuple > --> class Node(NamedTuple): > --> val = 0 > --> left = 1, 'previous

[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(). ___

Re: [Python-ideas] Add optional defaults to namedtuple

2016-11-30 Thread Guido van Rossum
On Wed, Nov 30, 2016 at 7:09 AM, Ethan Furman wrote: > On 11/30/2016 02:32 AM, Jelte Fennema wrote: > > It would be nice to have a supported way to add defaults to namedtuple, >> so the slightly hacky solution here does not have to be used: >>

Re: [Python-ideas] Add optional defaults to namedtuple

2016-11-30 Thread Ethan Furman
On 11/30/2016 02:32 AM, Jelte Fennema wrote: It would be nice to have a supported way to add defaults to namedtuple, so the slightly hacky solution here does not have to be used: http://stackoverflow.com/a/18348004/2570866 Actually, the solution right below it is better [1]: --> from

Re: [Python-ideas] Decorator to avoid a mistake

2016-11-30 Thread Vince Vinet
Hello, While I think this should not be "on by default", I don't see the harm in being able to opt-in to this behavior. I also figured spending a few minutes attempting to write this would be fun: https://gist.github.com/veloutin/2ec3e5246651f5de78442516d8e24fc1 François: sorry about the

Re: [Python-ideas] Better error messages [was: (no subject)]

2016-11-30 Thread Nick Coghlan
On 30 November 2016 at 19:05, Paul Moore wrote: > On 30 November 2016 at 02:14, Stephen J. Turnbull > wrote: >> Wouldn't >> >> NameError: Python doesn't recognize the name "log". Perhaps >> you need to import the "math" module? >

Re: [Python-ideas] (no subject)

2016-11-30 Thread Rob Cliffe
On 29/11/2016 20:09, Terry Reedy wrote: On 11/29/2016 11:32 AM, Rob Cliffe wrote: On 29/11/2016 04:58, victor rajewski wrote: Traceback (most recent call last): File "foo.py", line 2, in l[10]=14 IndexError: list assignment index out of range A better message might be: You

[Python-ideas] Add optional defaults to namedtuple

2016-11-30 Thread Jelte Fennema
It would be nice to have a supported way to add defaults to namedtuple, so the slightly hacky solution here does not have to be used: http://stackoverflow.com/a/18348004/2570866 Jelte ___ Python-ideas mailing list Python-ideas@python.org

Re: [Python-ideas] Better error messages [was: (no subject)]

2016-11-30 Thread Paul Moore
On 30 November 2016 at 02:14, Stephen J. Turnbull wrote: > How about: > > class Blog: > pass > > blog = get_blog_for_date(someday) > > logn = log(blog.size) > > NameError: Python doesn't recognize the function "log". Did you > mean