[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Steven D'Aprano
On Fri, Jul 03, 2020 at 09:24:47PM -0300, Soni L. wrote: > how do you plan to clamp a numpy array or a string? I'm not saying it is meaningful, but it certainly works to clamp strings: py> s = "hello" py> min(max(s, "a"), "z") 'hello' Likewise other indexable types can be compared

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Soni L.
On 2020-07-03 9:37 p.m., Christopher Barker wrote: On Fri, Jul 3, 2020 at 5:25 PM > wrote: > I'd go for val[min:max] tbh. another reason this is Not Good: in slicing syntax, a:b means >=a and < b -- this asymmetry is not what we would want here. It doesn't

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Christopher Barker
FWIW, numpy calls it "clip": numpy.clip(a, a_min, a_max, out=None, **kwargs) Clip (limit) the values in an array. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Christopher Barker
On Fri, Jul 3, 2020 at 5:25 PM wrote: > > I'd go for val[min:max] tbh. > another reason this is Not Good: in slicing syntax, a:b means >=a and < b -- this asymmetry is not what we would want here. -CHB -- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Matthew Einhorn
On Fri, Jul 3, 2020 at 5:57 PM Soni L. wrote: > > > On 2020-07-03 6:05 p.m., Federico Salerno wrote: > > Even after years of coding it sometimes takes me a moment to correctly > parse expressions like `min(max(value, minimum), maximum)`, especially when > the parentheses enclose some local

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Soni L.
On 2020-07-03 7:53 p.m., tcphon...@gmail.com wrote: > I'd go for val[min:max] tbh. > benefits: > > - currently not allowed! > - replaces min and max! Is this a serious suggestion? No offence intended, but this seems ill-thought-out. val[min:max] is perfectly legal syntax and it will only

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread TCPhone93
> I'd go for val[min:max] tbh. > benefits: > > - currently not allowed! > - replaces min and max! Is this a serious suggestion? No offence intended, but this seems ill-thought-out. val[min:max] is perfectly legal syntax and it will only error if the variable val happens to not support

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Steven D'Aprano
This was proposed about four years ago, Here is a link to the first post in the thread: https://mail.python.org/pipermail/python-ideas/2016-July/041262.html Discussion spilled over into the following month, here's the first post following:

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread MRAB
On 2020-07-03 22:05, Federico Salerno wrote: Even after years of coding it sometimes takes me a moment to correctly parse expressions like `min(max(value, minimum), maximum)`, especially when the parentheses enclose some local computation instead of only references, and the fact that `min`

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread MRAB
On 2020-07-03 22:56, Soni L. wrote: On 2020-07-03 6:05 p.m., Federico Salerno wrote: Even after years of coding it sometimes takes me a moment to correctly parse expressions like `min(max(value, minimum), maximum)`, especially when the parentheses enclose some local computation instead of

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Soni L.
On 2020-07-03 6:05 p.m., Federico Salerno wrote: Even after years of coding it sometimes takes me a moment to correctly parse expressions like `min(max(value, minimum), maximum)`, especially when the parentheses enclose some local computation instead of only references, and the fact that

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Jonathan Goble
My vote: clamp(+1000, min=-1, max=+1) I've had to do this operation many times in a variety of languages (Python is my playtoy for personal stuff, but work and school rarely give me a choice of language). I always love it when the language has a readily available `clamp()` function for this, and

[Python-ideas] Add builtin function for min(max())

2020-07-03 Thread Federico Salerno
Even after years of coding it sometimes takes me a moment to correctly parse expressions like `min(max(value, minimum), maximum)`, especially when the parentheses enclose some local computation instead of only references, and the fact that `min` actually needs the *maximum* value as an

[Python-ideas] Re: Else assert statement

2020-07-03 Thread Rob Cliffe via Python-ideas
On 03/07/2020 00:39, Artemis wrote: Often, I know that a variable should have one of a set of values, and I want to determine which one, with an if/elif/else clause. This looks something like this: ``` if foo == 1: # do a elif foo == 2: # do b elif foo == 3: # do c else:

[Python-ideas] Re: What about having a .get(index, default) method for arrays like we have for dicts?

2020-07-03 Thread Daniel.
Em sex, 3 de jul de 2020 11:37, Kyle Lahnakoski escreveu: > > On 2020-06-27 09:34, Daniel. wrote: > > When I need to traverse nested dicts, is a common pattern to do > > > > somedict.get('foo', {}).get('bar', {}) > > > > But there is no such equivalent for arrays, wouldn't be nice if we can > >

[Python-ideas] Re: What about having a .get(index, default) method for arrays like we have for dicts?

2020-07-03 Thread Kyle Lahnakoski
On 2020-06-27 09:34, Daniel. wrote: When I need to traverse nested dicts, is a common pattern to do somedict.get('foo', {}).get('bar', {}) But there is no such equivalent for arrays, wouldn't be nice if we can follow somedict.get('foo', {}).get('bar', []).get(10) ... ? What do you think?

[Python-ideas] Re: What about having a .get(index, default) method for arrays like we have for dicts?

2020-07-03 Thread Alex Hall
On Fri, Jul 3, 2020 at 5:36 AM Stephen J. Turnbull < turnbull.stephen...@u.tsukuba.ac.jp> wrote: > Alex Hall writes: > > > `dct.get('foo')?.get(0)?.get('bar')`. > > > > I would quite like to have PEP 505, but I don't know how to revive it. > And > > I'm not surprised that it's deferred,

[Python-ideas] Re: Add __eq__ to colletions.abc.Sequence ?

2020-07-03 Thread Joao S. O. Bueno
On Fri, 3 Jul 2020 at 03:07, Random832 wrote: > On Wed, Jul 1, 2020, at 08:23, Joao S. O. Bueno wrote: > > collections.mixins.SlicedSequence that would override `__delitem__`, > > `__setitem__` and `__getitem__` and > > handle slices could pair up with the "ComparableSequence" - people > > could

[Python-ideas] Re: An alternative way of defining properties (warning: contains creative abuse of the class statement)

2020-07-03 Thread Greg Ewing
On 3/07/20 6:33 pm, Serhiy Storchaka wrote: it is possible to implement pickling support for property objects which will fail with your example (and I think third-party libraries do it). The difference is that full qualified names of getter and setter differ from the full qualified name of the

[Python-ideas] Re: An alternative way of defining properties (warning: contains creative abuse of the class statement)

2020-07-03 Thread Greg Ewing
On 3/07/20 5:42 am, Matthew Einhorn wrote: Similarly, if you wanted to overwrite a property by using this property approach in the sub-class, but also call super for the parent's class property getter from within the get/set this wouldn't work!? Realised after sending that you were talking

[Python-ideas] Re: Add __eq__ to colletions.abc.Sequence ?

2020-07-03 Thread Wes Turner
Can a Sequence be infinite? If so, an equality test of two nonterminating sequences would be a nonterminating operation. Do Sized and *Reversible* imply that a sequence terminates? Could __len__ return inf? Perhaps `Ordered` is a requisite condition for defining a comparator for Sequences.

[Python-ideas] Re: giving set.add a return value

2020-07-03 Thread Random832
On Mon, Jun 29, 2020, at 04:51, Stephen J. Turnbull wrote: > Of course, we may prefer a boolean return, despite the general rule > about returning elements. I'm single-threaded, and so agnostic on > that. :-) But if it turns out that somebody *wants* to check "2 is > 2.0", > this .add_unique

[Python-ideas] Re: An alternative way of defining properties (warning: contains creative abuse of the class statement)

2020-07-03 Thread Greg Ewing
On 3/07/20 5:42 am, Matthew Einhorn wrote: I think what he may have meant is that if you tried accessing a double-underscore property of the outer class from the get/set methods, it won't properly de-mangle. Ah, I see what you mean. I don't think that's a fatal problem; double-underscore

[Python-ideas] Re: An alternative way of defining properties (warning: contains creative abuse of the class statement)

2020-07-03 Thread Serhiy Storchaka
02.07.20 13:26, Greg Ewing пише: On 2/07/20 8:04 pm, Serhiy Storchaka wrote: It has a problem with pickling (it is solvable). Can you elaborate? The end result is a property object just the same as you would get from using @property or calling property directly. I don't see how it can have

[Python-ideas] Re: Add __eq__ to colletions.abc.Sequence ?

2020-07-03 Thread Random832
On Wed, Jul 1, 2020, at 08:23, Joao S. O. Bueno wrote: > collections.mixins.SlicedSequence that would override `__delitem__`, > `__setitem__` and `__getitem__` and > handle slices could pair up with the "ComparableSequence" - people > could use these "a la carte", and > no backwards