Re: [Python-ideas] Add "default" kw argument to operator.itemgetter and operator.attrgetter

2018-05-03 Thread Serhiy Storchaka
03.05.18 08:28, Raymond Hettinger пише: On May 2, 2018, at 1:08 AM, Vincent Maillol wrote: Our PEP idea would be to purpose to add a global default value for itemgeet and attrgetter method. My preference is to not grow that API further. It is creep well beyond its intended uses. At some po

[Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Robert Roskam
Hi Everyone, Never posted in here before, so I hope that I'm not violating any particular procedure for intros or something. Over time, there have been various switch or match statement proposal; some that have gotten as far as PEPs: 2001 Nov - https://www.python.org/dev/peps/pep-0275/ 2006 Jun

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Joao S. O. Bueno
What about instead of number = match x: 1 => "one" 2 => "two" 3 => "three" 10 => "ten" _ => "anything" number = { 1 => "one" 2 => "two" 3 => "three" 10 => "ten" }.get(x, "anything") No magic syntax with blocks starting inside an assignment, just to start

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Joao S. O. Bueno
(sorry - my aboe message is about using a dictionary - the "=>" weird tokens should j=be just plain ":" - the point is that Python açready has syntax to do what is asked) On 3 May 2018 at 10:15, Joao S. O. Bueno wrote: > What about instead of > > number = match x: > 1 => "one" > 2 => "two

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Stéfane Fermigier
On Thu, May 3, 2018 at 2:41 PM, Robert Roskam wrote: > > And for the sake of completeness, here are other languages with similar > syntax features and their associated documentation [...] > Still for the sake of completeness, and without any judgement from me at this point, a couple more, which ar

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Alberto Berti
> "Stéfane" == Stéfane Fermigier writes: Stéfane> On Thu, May 3, 2018 at 2:41 PM, Robert Roskam Stéfane> wrote: >> >> And for the sake of completeness, here are other languages with similar >> syntax features and their associated documentation [...] >> Stéfane>

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Jacco van Dorp
> # Pattern matching with guards > x = 'three' > > number = match x: >1 => "one" >y if y is str => f'The string is {y}' >_ => "anything" > > print(number) # The string is three I think you meant to use isinstance(y, str) ? This looks like an incomplete ternary as well, missing the els

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Robert Roskam
Hey Joao, Thanks for providing me feedback on this idea! For the simplistic example at that you select, yes, you absolutely can do this as it stands atm. However, the examples I provided further along aren't as easily accomplished, nor is something like this: x = -1 result = match x: x:int

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 12:29 AM, Robert Roskam wrote: > Hey Joao, > > Thanks for providing me feedback on this idea! > > For the simplistic example at that you select, yes, you absolutely can do > this as it stands atm. However, the examples I provided further along aren't > as easily accomplished

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Ed Kellett
On 2018-05-03 15:02, Jacco van Dorp wrote: >> # Pattern matching with guards >> x = 'three' >> >> number = match x: >>1 => "one" >>y if y is str => f'The string is {y}' >>_ => "anything" >> >> print(number) # The string is three > > I think you meant to use isinstance(y, str) ? > This

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Rhodri James
On 03/05/18 18:18, Ed Kellett wrote: It's a proposal for new syntax. I snipped the rest because fundamentally you have failed to explain your new syntax in any clear way. You've given examples of varying levels of complexity but failed to explain what any of them should actually do in words

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Ed Kellett
On 2018-05-03 18:53, Rhodri James wrote: > On 03/05/18 18:18, Ed Kellett wrote: >> It's a proposal for new syntax. > > I snipped the rest because fundamentally you have failed to explain your > new syntax in any clear way.  You've given examples of varying levels of > complexity but failed to expl

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 3:18 AM, Ed Kellett wrote: > I believe the intention in the example you quoted is syntax something like: > > ::= >| "if" > > where the expression is a guard expression evaluated in the context of > the matched pattern. > > IOW, it could be written

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread David Mertz
Calculating the Ackermann function as Knuth up-arrows really has little practical user. The first few values are well known, the rest won't be calculated before the heat death of the universe. On Thu, May 3, 2018, 2:02 PM Chris Angelico wrote: > On Fri, May 4, 2018 at 3:18 AM, Ed Kellett > wrot

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Robert Roskam
Hey Chris, So I started extremely generally with my syntax, but it seems like I should provide a lot more examples of real use. Examples are hard. Here's my hastily put together example from an existing piece of production code: # Existing Production Code from datetime import timedelta, date f

Re: [Python-ideas] Please consider adding partialclass to functools

2018-05-03 Thread Neil Girdhar
Done: https://bugs.python.org/issue33419 On Wed, May 2, 2018 at 10:42 PM Steven D'Aprano wrote: > On Wed, May 02, 2018 at 02:46:09PM -0700, Neil Girdhar wrote: > > Essentially, functools.partial is almost good enough for specifying some > of > > the parameters of an object's initializer, but the

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Steven D'Aprano
On Fri, May 04, 2018 at 04:01:55AM +1000, Chris Angelico wrote: > On Fri, May 4, 2018 at 3:18 AM, Ed Kellett wrote: > > def hyperop(n, a, b): > > return match (n, a, b): > > (0, _, b) => b + 1 > > (1, a, 0) => a > > (2, _, 0) => 0 > > (_, _, 0) => 1 > >

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 4:36 AM, Robert Roskam wrote: > Hey Chris, > > So I started extremely generally with my syntax, but it seems like I should > provide a lot more examples of real use. Examples are hard. Here's my > hastily put together example from an existing piece of production code: > > >

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 4:40 AM, Steven D'Aprano wrote: > On Fri, May 04, 2018 at 04:01:55AM +1000, Chris Angelico wrote: >> On Fri, May 4, 2018 at 3:18 AM, Ed Kellett wrote: > >> > def hyperop(n, a, b): >> > return match (n, a, b): >> > (0, _, b) => b + 1 >> > (1, a, 0) => a >

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Robert Roskam
Hey Chris, Thanks for bringing that up! Before submitting this, I actually had the syntax for multiple matches for one arm being separated by or. And frankly I just didn't like how that looked for more than 3 items: '1' or '2' or '3' or '4' or '5' vs '1', '2', '3', '4', '5' But you're right.

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 5:02 AM, Robert Roskam wrote: > Hey Chris, > > Thanks for bringing that up! Before submitting this, I actually had the > syntax for multiple matches for one arm being separated by or. And frankly I > just didn't like how that looked for more than 3 items: > > '1' or '2' or '

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Ed Kellett
On 2018-05-03 19:57, Chris Angelico wrote: > Got it. Well, I don't see why we can't use Python's existing primitives. > > def hyperop(n, a, b): > if n == 0: return 1 + b > if n == 1: return a + b > if n == 2: return a * b > if n == 3: return a ** b > if n == 4: return a *** b >

[Python-ideas] Fwd: Pattern Matching Syntax

2018-05-03 Thread David Mertz
That's buggy code in either version. A month is not necessarily 30 days, and a year is not necessarily 365 days. An example without such painful bugs might be more compelling... It also likely makes the use case less obvious for the bug-free version. On Thu, May 3, 2018, 2:37 PM Robert Roskam wro

[Python-ideas] [offlist] Re: Add "default" kw argument to operator.itemgetter and operator.attrgetter

2018-05-03 Thread Raymond Hettinger
> On May 2, 2018, at 11:32 PM, Steven D'Aprano wrote: > > Intended by whom? By me. I proposed itemgetter() in the first place. That rationale I gave convinced Guido and python-dev to accept it. I then wrote the code, docs, tests and have maintained it for over a decade. So, I have a prett

Re: [Python-ideas] Please consider adding partialclass to functools

2018-05-03 Thread Terry Reedy
On 5/3/2018 2:34 PM, Neil Girdhar wrote: Done: https://bugs.python.org/issue33419 I agree with Steven. Add the functools 'experts' as nosy. And add links to the SO items. On Wed, May 2, 2018 at 10:42 PM Steven D'Aprano > wrote: On Wed, May 02, 2018 at 02:4

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Terry Reedy
On 5/3/2018 9:16 AM, Joao S. O. Bueno wrote: (sorry - my aboe message is about using a dictionary - the "=>" weird tokens should j=be just plain ":" - the point is that Python açready has syntax to do what is asked) On 3 May 2018 at 10:15, Joao S. O. Bueno wrote: What about instead of number

Re: [Python-ideas] Please consider adding partialclass to functools

2018-05-03 Thread Neil Girdhar
Thank you. Done. On Thu, May 3, 2018 at 5:44 PM Terry Reedy wrote: > On 5/3/2018 2:34 PM, Neil Girdhar wrote: > > Done: https://bugs.python.org/issue33419 > > I agree with Steven. > Add the functools 'experts' as nosy. And add links to the SO items. > > > > > On Wed, May 2, 2018 at 10:42 PM St

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Terry Reedy
On 5/3/2018 8:41 AM, Robert Roskam wrote: However, I don't see that the conversation ever really resolved, so I'd like restart the conversation on some kind of pattern matching syntax in Python. For the cases not handled by dicts, I believe chained conditional expressions work. """ # Patte

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Ed Kellett
On 2018-05-03 20:17, Chris Angelico wrote: >> def convert_time_to_timedelta_with_match(unit:str, amount:int, now:date): >> return match unit: >> x if x in ('days', 'hours', 'weeks') => timedelta(**{unit: amount}) >> 'months' => timedelta(days=30 * amount) >> 'years' => timedelta(day