Re: [Python-ideas] a sorting protocol dunder method?

2017-12-08 Thread Chris Barker - NOAA Federal
If by no brainer you mean the performance of __sort-key__ is always better of __lt__ No. By no-brainer I meant that IF there is a __sort_key__ defined, then it should be used for sorting, regardless of whether __lt__ is also defined. (min and max should probably prefer __lt__) I will wask for

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-06 Thread Barry Scott
> On 5 Dec 2017, at 01:06, Chris Barker wrote: > > wow! a few time zones (and a day job) really make a difference to taking part > in a discussion :-) > > This could be a good idea -- just putting it here for the record as it's > mentioned elsewhere. > > I can't

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Greg Ewing
On 12/04/2017 10:01 AM, brent bejot wrote: I think we can all agree that defining __key__ and calling "sorted(list_of_attrdicts)" has that syntactic sugar that is oh-so-sweet-and-tasty. Just remember that too much syntactic sugar can give you cancer of the semicolon. -- Greg

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Chris Barker
wow! a few time zones (and a day job) really make a difference to taking part in a discussion :-) Thanks for all the feedback. From what I can tell, we don't have a consensus, though It's looking pretty unlikely that this is going to be adopted (though maybe the decorator idea). But I'm going to

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Chris Angelico
On Tue, Dec 5, 2017 at 8:54 AM, Julien Salort wrote: > Le 04/12/2017 à 14:16, Steven D'Aprano a écrit : > >> We're taking something which belongs in the report generator or >> collection, the knowledge of how to sort a collection of unordered >> values, and baking it into the

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Julien Salort
Le 04/12/2017 à 14:16, Steven D'Aprano a écrit : We're taking something which belongs in the report generator or collection, the knowledge of how to sort a collection of unordered values, and baking it into the values themselves. (Effectively making them ordered!) It is also possible to use

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Chris Angelico
On Tue, Dec 5, 2017 at 8:22 AM, Barry wrote: > > >> On 4 Dec 2017, at 20:12, Antoine Pitrou wrote: >> >> On Mon, 4 Dec 2017 19:37:02 + >> Barry Scott wrote: >>> I wondered what the performance would be and tested the

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Barry
> On 4 Dec 2017, at 20:12, Antoine Pitrou wrote: > > On Mon, 4 Dec 2017 19:37:02 + > Barry Scott wrote: >> I wondered what the performance would be and tested the following code: >> > [...] >> >> it outputs this for my with python 3.6.0 >>

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Antoine Pitrou
On Mon, 4 Dec 2017 19:37:02 + Barry Scott wrote: > I wondered what the performance would be and tested the following code: > [...] > > it outputs this for my with python 3.6.0 > > 1 > key 0.010628s 1 calls > lt 0.053690s 119886 calls > > It seems that

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Barry Scott
I wondered what the performance would be and tested the following code: #!/usr/bin/env python3 import random import time random.seed( hash('Testing Keys') ) lt_calls = 0 key_calls = 0 class MyObject: def __init__( self, value1, value2 ): self.value1 = value1 self.value2 =

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Rhodri James
On 04/12/17 18:01, brent bejot wrote: I'm +1 on this idea for the most part. I agree particularly with the idea that it is better OOP for an object to access it's member variables to create the key than an external container to do so. This I'm absolutely fine with. Key methods are something

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Mark Lawrence
On 04/12/17 18:01, brent bejot wrote: I'm +1 on this idea for the most part. I agree particularly with the idea that it is better OOP for an object to access it's member variables to create the key than an external container to do so. > and then sort like this: > sorted(list_of_attrdicts,

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Ethan Furman
On 12/04/2017 10:01 AM, brent bejot wrote: This is certainly a good pattern to use in the current and older versions, but I think we can all agree that defining __key__ and calling "sorted(list_of_attrdicts)" has that syntactic sugar that is oh-so-sweet-and-tasty. Actually, no, we do not

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread brent bejot
I'm +1 on this idea for the most part. I agree particularly with the idea that it is better OOP for an object to access it's member variables to create the key than an external container to do so. > and then sort like this: > sorted(list_of_attrdicts, key=AttrDict._id_order) This is certainly a

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Steven D'Aprano
On Mon, Dec 04, 2017 at 01:52:19PM +0100, Antoine Pitrou wrote: > On Mon, 4 Dec 2017 23:16:11 +1100 > Steven D'Aprano wrote: > > On Mon, Dec 04, 2017 at 12:06:38PM +0100, Antoine Pitrou wrote: > > > > > There are definitely advantages. Sorting calls __lt__ for each > > >

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Antoine Pitrou
On Mon, 4 Dec 2017 15:50:31 +0200 Serhiy Storchaka wrote: > > >> Yes, it is too special-case. I don't see any advantages in comparison > >> with defining the __lt__ method. > > > > There are definitely advantages. Sorting calls __lt__ for each > > comparison (that is,

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Serhiy Storchaka
04.12.17 14:25, Steven D'Aprano пише: On Mon, Dec 04, 2017 at 08:45:55AM +0200, Serhiy Storchaka wrote: But the idea of the class decorator looks more sane to me. The purpose of __key__ is to define a key function (not a comparison operator) for classes that aren't orderable and don't have

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Serhiy Storchaka
04.12.17 13:06, Antoine Pitrou пише: On Mon, 4 Dec 2017 08:45:55 +0200 Serhiy Storchaka wrote: 04.12.17 01:06, Chris Barker пише: So: has this already been brought up and rejected? https://bugs.python.org/issue20632 Am I imagining the performance benefits? This will

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Steven D'Aprano
On Sun, Dec 03, 2017 at 06:53:45PM -0800, Bruce Leban wrote: > I think you're arguing against this for the wrong reason. Chris was talking > about custom classes having the *option* of making them sortable by > providing a key method in the class definition. I never imagined that it would be a

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Antoine Pitrou
On Mon, 4 Dec 2017 12:19:07 + Paul Moore wrote: > On 4 December 2017 at 11:41, Steven D'Aprano wrote: > > On Sun, Dec 03, 2017 at 10:48:18PM -0800, Carl Meyer wrote: > >> I think this is an interesting idea, and I don't believe that either > >>

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Antoine Pitrou
On Mon, 4 Dec 2017 23:16:11 +1100 Steven D'Aprano wrote: > On Mon, Dec 04, 2017 at 12:06:38PM +0100, Antoine Pitrou wrote: > > > There are definitely advantages. Sorting calls __lt__ for each > > comparison (that is, O(n log n) times) while __key__ would only be > > called

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Steven D'Aprano
On Mon, Dec 04, 2017 at 08:45:55AM +0200, Serhiy Storchaka wrote: > But the idea of the class decorator looks more sane to me. The purpose of __key__ is to define a key function (not a comparison operator) for classes that aren't orderable and don't have __lt__. If you're going to then go

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Paul Moore
On 4 December 2017 at 11:41, Steven D'Aprano wrote: > On Sun, Dec 03, 2017 at 10:48:18PM -0800, Carl Meyer wrote: >> I think this is an interesting idea, and I don't believe that either >> performance or "sortable vs comparable" are very relevant. > > Performance is always

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Steven D'Aprano
On Mon, Dec 04, 2017 at 12:06:38PM +0100, Antoine Pitrou wrote: > There are definitely advantages. Sorting calls __lt__ for each > comparison (that is, O(n log n) times) while __key__ would only be > called once per item at the start (that is, O(n) times). Passing a key function doesn't

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Steven D'Aprano
On Sun, Dec 03, 2017 at 10:48:18PM -0800, Carl Meyer wrote: > I think this is an interesting idea, and I don't believe that either > performance or "sortable vs comparable" are very relevant. Performance is always relevant -- while performance shouldn't be the sole deciding factor, it should be

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Antoine Pitrou
On Mon, 4 Dec 2017 08:45:55 +0200 Serhiy Storchaka wrote: > 04.12.17 01:06, Chris Barker пише: > > So: has this already been brought up and rejected? > > https://bugs.python.org/issue20632 > > > Am I imagining the performance benefits? > > This will add an additional

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Antoine Pitrou
On Sun, 3 Dec 2017 15:06:02 -0800 Chris Barker wrote: > I can't believe this hasn't been brought up before, but searching the web, > and python-ideas, and all the PEPs has found nothing (could be my lame > google-fu), so here goes: > > Recent python has moved toward a

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-04 Thread Nathaniel Smith
On Sun, Dec 3, 2017 at 10:48 PM, Carl Meyer wrote: > It'd be nice to be able to eliminate an import and have the lines of > code and instead write that as: > > class BankAccount: > def __init__(self, balance): > self.balance = balance > > def

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-03 Thread Carl Meyer
I think this is an interesting idea, and I don't believe that either performance or "sortable vs comparable" are very relevant. I doubt there is much performance to gain here, and I think the default sort order for a class must continue to match its comparison behavior. I think the case in favor

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-03 Thread Serhiy Storchaka
04.12.17 01:06, Chris Barker пише: So: has this already been brought up and rejected? https://bugs.python.org/issue20632 Am I imagining the performance benefits? This will add an additional overhead. This will be even slower than passing the key function, since you will need to look up

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-03 Thread David Mertz
And if this is a method on a custom *collection*, it can do whatever it wants in MyCollection.sort() already. On Dec 3, 2017 7:14 PM, "David Mertz" wrote: > I'm not sure I understand the motivation to make elements *sortable* but > not comparable. If an arbitrary order is still

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-03 Thread Bruce Leban
On Sun, Dec 3, 2017 at 3:06 PM, Chris Barker wrote: > > However, if you are writing a custom class ... > > But what if there was a sort key magic method: > > __key__ or __sort_key__ (or whatever) > > that would be called by the sorting functions > > It seems this would

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-03 Thread Steven D'Aprano
On Sun, Dec 03, 2017 at 06:46:45PM -0500, Nathan Schneider wrote: > On Sun, Dec 3, 2017 at 6:06 PM, Chris Barker wrote: > > > In fact, it's striking me that there may well be classes that are defining > > the comparison magic methods not because they want the objects to

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-03 Thread Steven D'Aprano
On Sun, Dec 03, 2017 at 03:06:02PM -0800, Chris Barker wrote: > Recent python has moved toward a "key" function for customized sorting: > > list.sort(key=key_fun) > > key is also used (according to > https://docs.python.org/3.6/library/functools.html#functools.cmp_to_key) in: > > min(), max(),

Re: [Python-ideas] a sorting protocol dunder method?

2017-12-03 Thread Nathan Schneider
On Sun, Dec 3, 2017 at 6:06 PM, Chris Barker wrote: > In fact, it's striking me that there may well be classes that are defining > the comparison magic methods not because they want the objects to "work" > with the comparison operators, but because that want them to work

[Python-ideas] a sorting protocol dunder method?

2017-12-03 Thread Chris Barker
I can't believe this hasn't been brought up before, but searching the web, and python-ideas, and all the PEPs has found nothing (could be my lame google-fu), so here goes: Recent python has moved toward a "key" function for customized sorting: list.sort(key=key_fun) key is also used (according