Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Nathaniel Smith
On Sun, Jan 6, 2019 at 11:06 PM Steven D'Aprano wrote: > I'm not wedded to the idea that the default ought to be the current > behaviour. If there is a strong argument for one of the others, I'm > listening. "Errors should never pass silently"? Silently returning nonsensical results is hard to de

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Steven D'Aprano
On Sun, Jan 06, 2019 at 07:40:32PM -0800, Stephan Hoyer wrote: > On Sun, Jan 6, 2019 at 4:27 PM Steven D'Aprano wrote: > > > I propose adding a "nan_policy" keyword-only parameter to the relevant > > statistics functions (mean, median, variance etc), and defining the > > following policies: > > >

Re: [Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks

2019-01-06 Thread Elazar
I think the main issue is this: exception handling is already problematic with its nonlocal transfer of control. Making it bidirectional makes code even more difficult to understand. State will change "under your feet" without any syntactic clue. In "The Design and Evolution of C++" Bjarne Stroust

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread David Mertz
On Mon, Jan 7, 2019 at 1:27 AM Steven D'Aprano wrote: > > In [4]: statistics.median([9, 9, 9, nan, 1, 2, 3, 4, 5]) > > Out[4]: 1 > > In [5]: statistics.median([9, 9, 9, nan, 1, 2, 3, 4]) > > Out[5]: nan > > The second is possibly correct if one thinks that the median of a list > containing NAN sh

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Steven D'Aprano
On Sun, Jan 06, 2019 at 10:52:47PM -0500, David Mertz wrote: > Playing with Tim's examples, this suggests that statistics.median() is > simply outright WRONG. I can think of absolutely no way to characterize > these as reasonable results: > > Python 3.7.1 | packaged by conda-forge | (default, No

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Tim Peters
[David Mertz ] > OK, let me be more precise. Obviously if the implementation in a class is: > > class Foo: > def __lt__(self, other): > return random.random() < 0.5 > > Then we aren't going to rely on much. > > * If comparison of any two items in a list (under __lt__) is deterministic,

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread David Mertz
This statement is certainly false: > > * If two items are equal, and pairwise inequality is deterministic, > exchanging the items does not affect the sorting of other items in the list. > Just to demonstrate this obviousness: >>> sorted([9, 9, 9, b, 1, 2, 3, a]) [1, 2, 3, A, B, 9, 9, 9] >>> sor

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Chris Angelico
On Mon, Jan 7, 2019 at 3:19 PM David Mertz wrote: > > OK, let me be more precise. Obviously if the implementation in a class is: > > class Foo: > def __lt__(self, other): > return random.random() < 0.5 > > > Then we aren't going to rely on much. > > * If comparison of any two items in

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread David Mertz
OK, let me be more precise. Obviously if the implementation in a class is: class Foo: def __lt__(self, other): return random.random() < 0.5 Then we aren't going to rely on much. * If comparison of any two items in a list (under __lt__) is deterministic, is the resulting sort order

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Tim Peters
[David Mertz ] > Thanks Tim for clarifying. Is it even the case that sorts are STABLE in > the face of non-total orderings under __lt__? A couple quick examples > don't refute that, but what I tried was not very thorough, nor did I > think much about TimSort itself. I'm not clear on what "stable

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread David Mertz
[... apologies if this is dup, got a bounce ...] > [David Mertz ] >> I have to say though that the existing behavior of `statistics.median[_low|_high|]` >> is SURPRISING if not outright wrong. It is the behavior in existing Python, >> but it is very strange. >> >> The implementation simply does w

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Tim Peters
[David Mertz ] > I have to say though that the existing behavior of > `statistics.median[_low|_high|]` > is SURPRISING if not outright wrong. It is the behavior in existing Python, > but it is very strange. > > The implementation simply does whatever `sorted()` does, which is an > implementation

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Stephan Hoyer
On Sun, Jan 6, 2019 at 4:27 PM Steven D'Aprano wrote: > I propose adding a "nan_policy" keyword-only parameter to the relevant > statistics functions (mean, median, variance etc), and defining the > following policies: > > IGNORE: quietly ignore all NANs > FAIL: raise an exception if an

Re: [Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks

2019-01-06 Thread Richard Damon
On 1/6/19 9:54 PM, simon.bordeyne wrote: > I knew that you can just chain try/except blocks, and it's how I do it > now, but the example I provided wasn't very realistic. > > Take for example the initialization of a class from a config file, > config file which may or may not have certain keys in i

Re: [Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks

2019-01-06 Thread simon.bordeyne
I knew that you can just chain try/except blocks, and it's how I do it now, but the example I provided wasn't very realistic. Take for example the initialization of a class from a config file, config file which may or may not have certain keys in it. With many keys, it is very inconvenient to ch

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread David Mertz
I have to say though that the existing behavior of `statistics.median[_low|_high|]` is SURPRISING if not outright wrong. It is the behavior in existing Python, but it is very strange. The implementation simply does whatever `sorted()` does, which is an implementation detail. In particular, NaN's

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Steven D'Aprano
On Sun, Jan 06, 2019 at 07:46:03PM -0500, David Mertz wrote: > Would these policies be named as strings or with an enum? Following Pandas, > we'd probably support both. Sure, I can support both. > I won't bikeshed the names, but they seem to > cover desired behaviors. Good to hear. -- Stev

Re: [Python-ideas] NAN handling in the statistics module

2019-01-06 Thread David Mertz
Would these policies be named as strings or with an enum? Following Pandas, we'd probably support both. I won't bikeshed the names, but they seem to cover desired behaviors. On Sun, Jan 6, 2019, 7:28 PM Steven D'Aprano Bug #33084 reports that the statistics library calculates median and > other s

[Python-ideas] NAN handling in the statistics module

2019-01-06 Thread Steven D'Aprano
Bug #33084 reports that the statistics library calculates median and other stats wrongly if the data contains NANs. Worse, the result depends on the initial placement of the NAN: py> from statistics import median py> NAN = float('nan') py> median([NAN, 1, 2, 3, 4]) 2 py> median([1, 2, 3, 4, NAN]

Re: [Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks

2019-01-06 Thread Chris Barker via Python-ideas
There is already a much simpler way of doing this: > try: > i = int("string") > except ValueError as e: > print(e) > print("continued on") > j = int(9.0) > > The point of the 'try' block is to encapsulate the code you want to *stop* > executing if an exception is ra

[Python-ideas] Fixed point format for numbers with locale based separators

2019-01-06 Thread Stefan Krah
Eric V. Smith wrote: > If the locale character is "*", use locale-aware formatting for the > given "type", with LC_NUMERIC. So, "*g" would be equivalent to the > existing "n", and "*f" would give you the current "f" formatting, except > using LC_NUMERIC for the decimal point. If the locale cha

Re: [Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks

2019-01-06 Thread Amber Yust
On Sat, Jan 5, 2019 at 4:39 PM Simon wrote: > I propose to be able to use the continue keyword to continue the execution > of the try block even when an error is handled. The above could then be > changed to : > > > try: > i = int("string") > print("continued on") > j