Re: itemgetter with default arguments

2018-05-08 Thread Antoon Pardon
On 07-05-18 17:45, Peter Otten wrote: > Antoon Pardon wrote: > >> On 05-05-18 09:33, Peter Otten wrote: >>> I think you have established that there is no straight-forward way to >>> write this as a lambda. But is adding a default to itemgetter the right >>> conclusion? >>> >>> If there were an exce

Re: itemgetter with default arguments

2018-05-07 Thread Peter Otten
Antoon Pardon wrote: > On 05-05-18 09:33, Peter Otten wrote: >> I think you have established that there is no straight-forward way to >> write this as a lambda. But is adding a default to itemgetter the right >> conclusion? >> >> If there were an exception-catching decorator you could write >> >>

Re: itemgetter with default arguments

2018-05-07 Thread Antoon Pardon
On 05-05-18 09:33, Peter Otten wrote: > I think you have established that there is no straight-forward way to write > this as a lambda. But is adding a default to itemgetter the right > conclusion? > > If there were an exception-catching decorator you could write > > f = catch(IndexError, "spam")

Re: itemgetter with default arguments

2018-05-05 Thread Ian Kelly
On Fri, May 4, 2018 at 5:34 PM, Thomas Jollans wrote: > On 04/05/18 22:38, Ian Kelly wrote: >> The real thing is written in C. >> > > Is it though? > > https://github.com/python/cpython/blob/a1fc949b5ab8911a803eee691e6eea55cec43eeb/Lib/operator.py#L265 It is. First, notice the docstring of that m

Re: itemgetter with default arguments

2018-05-05 Thread Serhiy Storchaka
05.05.18 12:59, Steven D'Aprano пише: On Sat, 05 May 2018 10:31:17 +0300, Serhiy Storchaka wrote: Consider a concrete example. You need to sort a list of 2- or 3- element tuples by the first and third items (third items are strings if presented). itemgetter(0, 2) doesn't work because some tuples

Re: itemgetter with default arguments

2018-05-05 Thread Steven D'Aprano
On Sat, 05 May 2018 10:31:17 +0300, Serhiy Storchaka wrote: > Consider a concrete example. You need to sort a list of 2- or 3- element > tuples by the first and third items (third items are strings if > presented). itemgetter(0, 2) doesn't work because some tuples has only 2 > items. But you can u

Re: itemgetter with default arguments

2018-05-05 Thread Steven D'Aprano
On Sat, 05 May 2018 09:33:37 +0200, Peter Otten wrote: > I think you have established that there is no straight-forward way to > write this as a lambda. What I *mostly* established was that I was having a "cannot brain, I have the dumb" day, because the solution with ternary if was obvious in h

Re: itemgetter with default arguments

2018-05-05 Thread Peter Otten
Steven D'Aprano wrote: > A re-occurring feature request is to add a default to itemgetter and > attrgetter. For example, we might say: > > from operator import itemgetter > f = itemgetter(1, 6, default="spam") # proposed feature > f("Hello World!") # returns ('e', 'W') > f("Hello") # re

Re: itemgetter with default arguments

2018-05-05 Thread Serhiy Storchaka
04.05.18 20:04, Steven D'Aprano пише: On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote: On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano wrote: Here are the specifications: * you must use lambda, not def; Why? This seems like an arbitrary constraint. You'll have to ask the two core devs

Re: itemgetter with default arguments

2018-05-04 Thread Steven D'Aprano
On Fri, 04 May 2018 14:38:54 -0600, Ian Kelly wrote: > On Fri, May 4, 2018 at 11:04 AM, Steven D'Aprano > wrote: [...] >> My guess is that they were thinking that there's no need to complicate >> itemgetter for this use-case when it is just as easy to write up a >> quick lambda to do the job. >

Re: itemgetter with default arguments

2018-05-04 Thread Steven D'Aprano
On Fri, 04 May 2018 15:27:02 +0200, Thomas Jollans wrote: spamgetter = (lambda seq, i=2, fallback="spam": > ... seq[i] if abs(i) < len(seq) or i == -len(seq) > ... else fallback) spamgetter("abcd", i=-4) > 'a' spamgetter("abcd") > 'c' spamgett

Re: itemgetter with default arguments

2018-05-04 Thread Steven D'Aprano
On Fri, 04 May 2018 15:27:16 +0200, Antoon Pardon wrote: >> I might be slow today, but I cannot see how to write a clear, obvious, >> efficient lambda that provides functionality equivalent to itemgetter >> with a default value. [...] > This seems to work: > > f = (lambda seq: (list(seq) + 3 * [

Re: itemgetter with default arguments

2018-05-04 Thread Thomas Jollans
On 04/05/18 22:38, Ian Kelly wrote: > On Fri, May 4, 2018 at 11:04 AM, Steven D'Aprano > wrote: >> On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote: >> >>> On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano >>> wrote: Here are the specifications: * you must use lambda, not def; >>>

Re: itemgetter with default arguments

2018-05-04 Thread Ian Kelly
On Fri, May 4, 2018 at 11:04 AM, Steven D'Aprano wrote: > On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote: > >> On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano >> wrote: >>> Here are the specifications: >>> >>> * you must use lambda, not def; >> >> Why? This seems like an arbitrary constraint

Re: itemgetter with default arguments

2018-05-04 Thread Steven D'Aprano
On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote: > On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano > wrote: >> Here are the specifications: >> >> * you must use lambda, not def; > > Why? This seems like an arbitrary constraint. You'll have to ask the two core devs. In my post, in the part y

Re: itemgetter with default arguments

2018-05-04 Thread Chris Angelico
On Sat, May 5, 2018 at 1:17 AM, Ian Kelly wrote: > On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano > wrote: >> Here are the specifications: >> >> * you must use lambda, not def; > > Why? This seems like an arbitrary constraint. > > def itemgetter2(*items, default): > return lambda seq: tuple(

Re: itemgetter with default arguments

2018-05-04 Thread Ian Kelly
On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano wrote: > Here are the specifications: > > * you must use lambda, not def; Why? This seems like an arbitrary constraint. > * the lambda must take a single function, the sequence you want to > extract an item from; > > * you can hard-code the index

Re: itemgetter with default arguments

2018-05-04 Thread Antoon Pardon
On 04-05-18 15:01, Steven D'Aprano wrote: > A re-occurring feature request is to add a default to itemgetter and > attrgetter. For example, we might say: > > from operator import itemgetter > f = itemgetter(1, 6, default="spam") # proposed feature > f("Hello World!") # returns ('e', 'W') > f("He

Re: itemgetter with default arguments

2018-05-04 Thread Thomas Jollans
On 2018-05-04 15:01, Steven D'Aprano wrote: > A re-occurring feature request is to add a default to itemgetter and > attrgetter. For example, we might say: > > from operator import itemgetter > f = itemgetter(1, 6, default="spam") # proposed feature > f("Hello World!") # returns ('e', 'W') > f(