Re: [Python-ideas] Sorted lists

2019-04-11 Thread Christopher Barker
I are the appeal, but this really seems too special purpose for yet another dunder. A SortedList might be a good addition to the statistics package, however. I note that with regard to sorting, I suggested a __sort_key dunder so that the sorting routines could know how to efficiently sort

Re: [Python-ideas] Sorted lists

2019-04-10 Thread Brice Parent
It surely would be helpful. I still find it a bit too single-case oriented. Maybe having an equivalent __sorting_algo__ property with a value of the current sorting algorithm would be more general? There could be a SortingAlgo base class, which could be extended into classes like:  -

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Terry Reedy
On 4/8/2019 4:48 PM, Barry Scott wrote: On 8 Apr 2019, at 19:37, Terry Reedy wrote: On 4/8/2019 5:40 AM, Steven D'Aprano wrote: On Mon, Apr 08, 2019 at 07:44:41AM +0100, Alex Chamberlain wrote: I think a better abstraction for a sorted list is a new class, which implements the Sequence

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Alex Chamberlain
On Mon, 8 Apr 2019 at 22:07, Barry Scott wrote: > > > > > On 8 Apr 2019, at 19:37, Terry Reedy wrote: > > > > On 4/8/2019 5:40 AM, Steven D'Aprano wrote: > >> On Mon, Apr 08, 2019 at 07:44:41AM +0100, Alex Chamberlain wrote: > >>> I think a better abstraction for a sorted list is a new class,

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Barry Scott
> On 8 Apr 2019, at 19:37, Terry Reedy wrote: > > On 4/8/2019 5:40 AM, Steven D'Aprano wrote: >> On Mon, Apr 08, 2019 at 07:44:41AM +0100, Alex Chamberlain wrote: >>> I think a better abstraction for a sorted list is a new class, which >>> implements the Sequence protocol (and hence can be

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Terry Reedy
On 4/8/2019 5:40 AM, Steven D'Aprano wrote: On Mon, Apr 08, 2019 at 07:44:41AM +0100, Alex Chamberlain wrote: I think a better abstraction for a sorted list is a new class, which implements the Sequence protocol (and hence can be used in a lot of existing list contexts), but only exposed

Re: [Python-ideas] Sorted lists

2019-04-08 Thread David Mertz
On Mon, Apr 8, 2019, 6:26 AM Steven D'Aprano wrote: > Given all the points already raised, I think that an explicit SortedList > might be more appropriate. > This one looks cool. I've read about it, but haven't used it: http://www.grantjenks.com/docs/sortedcontainers/ I think a "sort hint"

Re: [Python-ideas] Sorted lists

2019-04-08 Thread David Mertz
On Mon, Apr 8, 2019, 5:46 AM Paul Moore wrote: > Still not convinced this is safe enough to be worth it ;-) > I'm convinced it's NOT safe enough to be worth it. On the other hand, a sortedlist subclass that maintained its invariant (probably remembering a key) sounds cool. I think there are

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Dan Sommers
On 4/8/19 6:25 AM, Steven D'Aprano wrote: > On Mon, Apr 08, 2019 at 02:55:54AM -0700, Nathaniel Smith wrote: >> If only we had some kind of API that could compute multiple quantiles at >> the same time... > > You mean something like this? > > quartiles, quintiles, deciles, percentiles =

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Paul Moore
On Mon, 8 Apr 2019 at 11:27, Steven D'Aprano wrote: > > Fair enough, but does it help you feel a bit better about the feature if > we called it a "sort hint", and emphasized that it should only be used > in the same sort of APIs where you might allow the caller to pass > > already_sorted=True

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Steven D'Aprano
On Mon, Apr 08, 2019 at 10:34:19AM +0100, Paul Moore wrote: > On Mon, 8 Apr 2019 at 10:10, Steven D'Aprano wrote: > > > Possibly the maintainer of bisect may decide that its not worth the > > change. But for the statistics module, I would certainly change the > > implementation of median() to

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Steven D'Aprano
On Mon, Apr 08, 2019 at 02:55:54AM -0700, Nathaniel Smith wrote: > Right, by "doesn't affect" I meant "cannot get any benefit, even if their > code is modified". Ah, sorry I misunderstood you. > > # This only makes sense if data is a sequence (list) > > # not an iterator. > >

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Nathaniel Smith
On Mon, Apr 8, 2019, 02:09 Steven D'Aprano wrote: > On Sun, Apr 07, 2019 at 08:26:24PM -0700, Nathaniel Smith wrote: > > On Sun, Apr 7, 2019 at 7:37 PM Steven D'Aprano > wrote: > > > There are quite a few important algorithms which require lists to be > > > sorted. For example, the bisect

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Paul Moore
On Mon, 8 Apr 2019 at 10:34, Steven D'Aprano wrote: > The median (and soon, quantiles) API says: > > I'm going to sort the list for you, whether you need it or > not, just in case you do. Hmm, I didn't see that mentioned in the docs. It makes a difference to my comment about behaviour

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Steven D'Aprano
On Mon, Apr 08, 2019 at 07:44:41AM +0100, Alex Chamberlain wrote: > I think a better abstraction for a sorted list is a new class, which > implements the Sequence protocol (and hence can be used in a lot of > existing list contexts), but only exposed mutation methods that can > guarantee that

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Paul Moore
On Mon, 8 Apr 2019 at 10:10, Steven D'Aprano wrote: > Possibly the maintainer of bisect may decide that its not worth the > change. But for the statistics module, I would certainly change the > implementation of median() to look something vaguely like this: > > # was > data =

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Steven D'Aprano
On Mon, Apr 08, 2019 at 03:18:53PM +1000, Cameron Simpson wrote: > If this is a publicly queriable value, is there any need to have a > dunder name at all? Why not just give lists a public is_sorted > attribute? I don't mind that. > I'm also not convinced the cost to every insert/append is

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Steven D'Aprano
On Mon, Apr 08, 2019 at 01:31:14PM +1000, Cameron Simpson wrote: > __setitem__ concerns me, along with other modification methods: what > about subclasses(*)? Every existing subclass which overrides __setitem__ > now needs to grow code to maintain __issorted__ if they do not > themselves call

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Steven D'Aprano
On Sun, Apr 07, 2019 at 08:26:24PM -0700, Nathaniel Smith wrote: > On Sun, Apr 7, 2019 at 7:37 PM Steven D'Aprano wrote: > > There are quite a few important algorithms which require lists to be > > sorted. For example, the bisect module, and for statistics median and > > other quantiles. > > But

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Xavier Combelle
looks for me that means that all subclass of list has to maintain the __isorted__ invariant, looks like not a backward compatible modification and quite problematic in case of the invariant broken. So if I'm correct the design is brittle. Moreover, that solve only the strict subset of sort case

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Kirill Balunov
Basically, I like this idea and of course theoretically it has use cases. But in the proposed form, it feels strongly curtailed. In practice, what does it mean to be sorted? [('a', 1),('b', 2),('bb', 4),('aaa', 3)] # Is this list sorted? [('a', 1),('aaa', 3),('b', 2),('bb', 4)] # or this?

Re: [Python-ideas] Sorted lists

2019-04-08 Thread Alex Chamberlain
On Mon, 8 Apr 2019 at 06:19, Cameron Simpson wrote: > > On 08Apr2019 00:17, Terry Reedy wrote: > >On 4/7/2019 10:32 PM, Steven D'Aprano wrote: > >>There are quite a few important algorithms which require lists to be > >>sorted. [...] > >>Proposal: let's give lists a dunder flag, __issorted__,

Re: [Python-ideas] Sorted lists

2019-04-07 Thread Cameron Simpson
On 08Apr2019 00:17, Terry Reedy wrote: On 4/7/2019 10:32 PM, Steven D'Aprano wrote: There are quite a few important algorithms which require lists to be sorted. [...] Proposal: let's give lists a dunder flag, __issorted__, that tracks whether the list is definitely sorted or not: [...]

Re: [Python-ideas] Sorted lists

2019-04-07 Thread Terry Reedy
On 4/7/2019 10:32 PM, Steven D'Aprano wrote: There are quite a few important algorithms which require lists to be sorted. For example, the bisect module, and for statistics median and other quantiles. Sorting a list is potentially expensive: while Timsort is very efficient, it is still

Re: [Python-ideas] Sorted lists

2019-04-07 Thread Cameron Simpson
On 08Apr2019 12:32, Steven D'Aprano wrote: There are quite a few important algorithms which require lists to be sorted. For example, the bisect module, and for statistics median and other quantiles. Sorting a list is potentially expensive: while Timsort is very efficient, it is still

Re: [Python-ideas] Sorted lists

2019-04-07 Thread Nathaniel Smith
On Sun, Apr 7, 2019 at 7:37 PM Steven D'Aprano wrote: > There are quite a few important algorithms which require lists to be > sorted. For example, the bisect module, and for statistics median and > other quantiles. But this flag doesn't affect those modules, right? 'bisect' already requires the