[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Andrew Barnert via Python-ideas
On Feb 18, 2020, at 17:11, Ethan Furman wrote: > > On 02/18/2020 04:26 PM, Steven D'Aprano wrote: >> On Tue, Feb 18, 2020 at 08:44:07PM +, Rhodri James wrote: > >>> Language, sunshine. >> What about it? >> Just for the record, I had no issues with Soni's language, but I do >> object to

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Ethan Furman
On 02/18/2020 04:26 PM, Steven D'Aprano wrote: On Tue, Feb 18, 2020 at 08:44:07PM +, Rhodri James wrote: Language, sunshine. What about it? Just for the record, I had no issues with Soni's language, but I do object to attempts to shame him for it. Since when is simply drawing one's

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Steven D'Aprano
On Tue, Feb 18, 2020 at 08:44:07PM +, Rhodri James wrote: > Language, sunshine. What about it? Just for the record, I had no issues with Soni's language, but I do object to attempts to shame him for it. This isn't the Disney Chanel, it's been over 100 years since Victoria was Queen of

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread Steven D'Aprano
On Tue, Feb 18, 2020 at 11:47:16AM -0800, Ethan Furman wrote: > Whatever `dict.items()` returns, both for consistency and because > `dict.items()` can become a simple alias for `dict.__items__`. That means that every object wanting to make use of this protocol needs to create a complex

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Steven D'Aprano
On Tue, Feb 18, 2020 at 09:04:21PM +, Paul Moore wrote: > This looks to me like Lua's `items()` function. I can't find that function listed anywhere. https://www.lua.org/manual/5.3/ Are you maybe thinking of `pairs()`? https://www.lua.org/manual/5.3/manual.html#pdf-pairs > It's useful in

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Soni L.
On 2020-02-18 7:03 p.m., Guido van Rossum wrote: On Tue, Feb 18, 2020 at 1:45 PM Soni L. > wrote: [...] Iteration is done thusly: def _pairs(obj): if isinstance(obj, collections.abc.Mapping): return iter(obj.items())

[Python-ideas] Re: Add a way to execute code in the context of different frame

2020-02-18 Thread Guido van Rossum
On Tue, Feb 18, 2020 at 11:09 AM Serhiy Storchaka wrote: > 18.02.20 20:13, Guido van Rossum пише: > > I am a little confused how you get from "there are extra frames in the > > traceback" to "modify exec() to run code in another frame". > > Sorry for skipping several steps. I'll try to expand

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Guido van Rossum
On Tue, Feb 18, 2020 at 1:45 PM Soni L. wrote: > [...] > Iteration is done thusly: > > def _pairs(obj): > if isinstance(obj, collections.abc.Mapping): > return iter(obj.items()) > elif isinstance(obj, collections.abc.Sequence): > return iter(enumerate(obj, 0)) >

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Soni L.
On 2020-02-18 6:04 p.m., Chris Angelico wrote: Too short, didn't read. Specifically, I'm not going to go digging through your links to find code to justify why you want this feature. Post some code here, or at very least, link directly to the code that would be improved by this feature. Soni,

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Guido van Rossum
On Tue, Feb 18, 2020 at 1:06 PM Chris Angelico wrote: > On Wed, Feb 19, 2020 at 7:58 AM Soni L. wrote: > > [...] I have a whole library focused on this sort of > > "canonical keys"-based iteration: > > > https://ganarchy.autistic.space/project/0f74bd87a23b515b45da7e6f5d9cc82380443dab/ > > > >

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Paul Moore
On Tue, 18 Feb 2020 at 20:47, Rhodri James wrote: > > On 18/02/2020 20:33, Soni L. wrote: > > > > > > On 2020-02-18 5:08 p.m., Rhodri James wrote: > >> On 18/02/2020 19:43, Soni L. wrote: > >>> It'd be nice to have a __valid_getitem_requests__ protocol that, if > >>> present, yields pairs such

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Chris Angelico
On Wed, Feb 19, 2020 at 7:58 AM Soni L. wrote: > > > > On 2020-02-18 5:43 p.m., Chris Angelico wrote: > > On Wed, Feb 19, 2020 at 7:34 AM Soni L. wrote: > > > > > > On 2020-02-18 5:08 p.m., Rhodri James wrote: > > > > On 18/02/2020 19:43, Soni L. wrote: > > > >> It'd be nice to have a

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Guido van Rossum
Presumably this was inspired by the other thread, "Add __keys__ or __items__ protocol", where an __items__() method was proposed instead of recognizing mappings by the presence of a keys() method. On Tue, Feb 18, 2020 at 12:46 PM Rhodri James wrote: > On 18/02/2020 20:33, Soni L. wrote: > > > >

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Soni L.
On 2020-02-18 5:43 p.m., Chris Angelico wrote: On Wed, Feb 19, 2020 at 7:34 AM Soni L. wrote: > > On 2020-02-18 5:08 p.m., Rhodri James wrote: > > On 18/02/2020 19:43, Soni L. wrote: > >> It'd be nice to have a __valid_getitem_requests__ protocol that, if > >> present, yields pairs such

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Chris Angelico
On Wed, Feb 19, 2020 at 7:34 AM Soni L. wrote: > > On 2020-02-18 5:08 p.m., Rhodri James wrote: > > On 18/02/2020 19:43, Soni L. wrote: > >> It'd be nice to have a __valid_getitem_requests__ protocol that, if > >> present, yields pairs such that: > >> > >> for key, value in items(obj): > >>

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Rhodri James
On 18/02/2020 20:33, Soni L. wrote: On 2020-02-18 5:08 p.m., Rhodri James wrote: On 18/02/2020 19:43, Soni L. wrote: It'd be nice to have a __valid_getitem_requests__ protocol that, if present, yields pairs such that: for key, value in items(obj):    assert obj[key] == value for any obj.

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Soni L.
On 2020-02-18 5:08 p.m., Rhodri James wrote: On 18/02/2020 19:43, Soni L. wrote: It'd be nice to have a __valid_getitem_requests__ protocol that, if present, yields pairs such that: for key, value in items(obj):    assert obj[key] == value for any obj. OK, I'll bite.  What is this

[Python-ideas] Re: Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Rhodri James
On 18/02/2020 19:43, Soni L. wrote: It'd be nice to have a __valid_getitem_requests__ protocol that, if present, yields pairs such that: for key, value in items(obj):   assert obj[key] == value for any obj. OK, I'll bite. What is this "items()" function you apply to the arbitrary object?

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread waszka23
+1 on the proposed addition. I think that `__items__` would be a better choice for the method name. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread Ethan Furman
On 02/18/2020 12:24 AM, Serhiy Storchaka wrote: 1. What special method should be added, `__keys__` or `__items__`? The former returns keys, it needs to call `__getitem__` repeatedly to get values. The latter returns key-value pairs, it does not need to call `__getitem__`, but you should

[Python-ideas] Add a __valid_getitem_requests__ protocol

2020-02-18 Thread Soni L.
It'd be nice to have a __valid_getitem_requests__ protocol that, if present, yields pairs such that: for key, value in items(obj):   assert obj[key] == value for any obj. Note that this definition explicitly allows subsets, which makes it useful for sequences and defaultdicts, as they can

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread Serhiy Storchaka
18.02.20 19:00, Soni L. пише: I like __items__. additionally, lists should implement it to return enumerate(self), and sets should implement it to return (v, v for v in self), and as such there should be no requirement with regards to __items__ and __iter__, and whether __iter__ returns keys

[Python-ideas] Re: Add a way to execute code in the context of different frame

2020-02-18 Thread Serhiy Storchaka
18.02.20 20:13, Guido van Rossum пише: I am a little confused how you get from "there are extra frames in the traceback" to "modify exec() to run code in another frame". Sorry for skipping several steps. I'll try to expand them here. PEP 558 does not have relation to this, as frame is not the

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread Soni L.
On 2020-02-18 5:34 a.m., Chris Angelico wrote: On Tue, Feb 18, 2020 at 7:25 PM Serhiy Storchaka wrote: > * The keys() method is not called in the dict constructor. Just the > existence of the keys attribute is checked, its value is not used. Given that that's already been the case, my

[Python-ideas] Re: Add a way to execute code in the context of different frame

2020-02-18 Thread Guido van Rossum
I am a little confused how you get from "there are extra frames in the traceback" to "modify exec() to run code in another frame". Also, with PEP 558 (Defined semantics for locals(), by Nick Coghlan) we might be able to just pass the frame's globals and locals to exec() or eval() without further

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread Guido van Rossum
+1 On Tue, Feb 18, 2020 at 5:01 AM Andrew Svetlov wrote: > Sounds reasonable to me > > On Tue, Feb 18, 2020 at 12:32 PM Serhiy Storchaka > wrote: > > > > 18.02.20 10:34, Chris Angelico пише: > > > Given that that's already been the case, my preferred colour for the > > > bike shed is... > > >

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread Andrew Svetlov
Sounds reasonable to me On Tue, Feb 18, 2020 at 12:32 PM Serhiy Storchaka wrote: > > 18.02.20 10:34, Chris Angelico пише: > > Given that that's already been the case, my preferred colour for the > > bike shed is... > > Thank you for your feedback. This is my preferable color too, for both >

[Python-ideas] A new itertools.product variant that supports kwargs and returns dicts

2020-02-18 Thread Daniel Grießhaber
When doing a grid-search over some hyper-parameters of an algorithm I often need to specify the possible value space for each parameter and then evaluate on every combination of these values. In this case, the product function of the itertools module is handy: from itertools import product

[Python-ideas] a new itertools.product variant that supports kwargs and returns dicts

2020-02-18 Thread Daniel Grießhaber
When doing a grid-search over some hyper-parameters of an algorithm I often need to specify the possible value space for each parameter and then evaluate on every combination of these values. In this case, the product function of the itertools module is handy: from itertools import product def

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread Serhiy Storchaka
18.02.20 10:34, Chris Angelico пише: Given that that's already been the case, my preferred colour for the bike shed is... Thank you for your feedback. This is my preferable color too, for both parts. ___ Python-ideas mailing list --

[Python-ideas] Re: Add a way to execute code in the context of different frame

2020-02-18 Thread Serhiy Storchaka
18.02.20 11:28, M.-A. Lemburg пише: This sounds like a nice idea, but it may make sense to limit the frame to one already on the stack. Otherwise, the code you're executing may never return to your current stack... I do not think there will be such problem. exec() will temporary replace the

[Python-ideas] Re: Add a way to execute code in the context of different frame

2020-02-18 Thread M.-A. Lemburg
On 18.02.2020 08:33, Serhiy Storchaka wrote: > The idea is inspired by the following StackOverflow question: > https://stackoverflow.com/questions/40945752/inspect-who-imported-me and > the corresponding BPO issue: https://bugs.python.org/issue39643. > > In the older Python versions the f_back

[Python-ideas] Re: Add __keys__ or __items__ protocol

2020-02-18 Thread Chris Angelico
On Tue, Feb 18, 2020 at 7:25 PM Serhiy Storchaka wrote: > * The keys() method is not called in the dict constructor. Just the > existence of the keys attribute is checked, its value is not used. Given that that's already been the case, my preferred colour for the bike shed is... > 1. What

[Python-ideas] Add __keys__ or __items__ protocol

2020-02-18 Thread Serhiy Storchaka
It is hard to distinguish a sequence and a mapping in Python 3. Both have __getitem__, __len__ and __iter__ methods, but with different semantic. In Python code you can use isinstance() and issubclass() checks against collections.abc.Mapping, but it works only with types which was explicitly