Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Inada Naoki
On Wed, Apr 24, 2019 at 6:17 AM Mark Shannon wrote: > > Hi, > > On 12/04/2019 2:44 pm, Inada Naoki wrote: > > Hi, all. > > > > I propose adding new method: dict.with_values(iterable) > > You can already do something like this, if memory saving is the main > concern. This should work on all

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Mark Shannon
Hi, On 12/04/2019 2:44 pm, Inada Naoki wrote: Hi, all. I propose adding new method: dict.with_values(iterable) You can already do something like this, if memory saving is the main concern. This should work on all versions from 3.3. def shared_keys_dict_maker(keys): class C: pass

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Serhiy Storchaka
12.04.19 19:17, Inada Naoki пише: Maybe, collections.DictBuilder may be another option. e.g. from collections import DictBuilder builder = DictBuilder(tuple("abc")) builder.build(range(3)) {"a": 0, "b": 1, "c": 2} Nitpicking: this is rather a factory than a builder. The difference between

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Inada Naoki
On Wed, Apr 24, 2019 at 12:34 AM Steve Dower wrote: > > >> If it's a key-sharing dict, then all the keys are strings. We know that > >> when we go to do the update, so we can intern all the strings (going to > >> do that anyway) and then it's a quick check if it already exists. If > >> it's a

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Inada Naoki
On Wed, Apr 24, 2019 at 12:28 AM Steve Dower wrote: > > > > > But if the original dictionary wasn't created with shared keys... the > > copy can't share them either. Or are you suggesting adding new code to > > create a shared key dictionary from one that isn't? > > This is a good point. Maybe

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Steve Dower
On 23Apr2019 0008, Inada Naoki wrote: On Tue, Apr 23, 2019 at 2:54 PM Steve Dower wrote: On 22Apr2019 2143, Inada Naoki wrote: On Tue, Apr 23, 2019 at 11:30 AM Steve Dower wrote: Or possibly just "dict(existing_dict).update(new_items)". Do you mean .update accepts values tuple? I can't

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Steve Dower
On 23Apr2019 0034, Glenn Linderman wrote: On 4/22/2019 10:59 PM, Steve Dower wrote: On 22Apr2019 2119, Glenn Linderman wrote: While Inada's suggested DictBuilder interface was immediately obvious, I don't get how either copy or update would achieve the goal. Perhaps you could explain?

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Glenn Linderman
On 4/22/2019 10:59 PM, Steve Dower wrote: On 22Apr2019 2119, Glenn Linderman wrote: While Inada's suggested DictBuilder interface was immediately obvious, I don't get how either copy or update would achieve the goal. Perhaps you could explain? Particularly, what would be the trigger that

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Inada Naoki
On Tue, Apr 23, 2019 at 2:54 PM Steve Dower wrote: > > On 22Apr2019 2143, Inada Naoki wrote: > > On Tue, Apr 23, 2019 at 11:30 AM Steve Dower wrote: > >> > >> Or possibly just "dict(existing_dict).update(new_items)". > >> > > > > Do you mean .update accepts values tuple? > > I can't think it's >

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Steve Dower
On 22Apr2019 2119, Glenn Linderman wrote: While Inada's suggested DictBuilder interface was immediately obvious, I don't get how either copy or update would achieve the goal. Perhaps you could explain? Particularly, what would be the trigger that would make dict() choose to create a shared key

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-23 Thread Steve Dower
On 22Apr2019 2143, Inada Naoki wrote: On Tue, Apr 23, 2019 at 11:30 AM Steve Dower wrote: Or possibly just "dict(existing_dict).update(new_items)". Do you mean .update accepts values tuple? I can't think it's Not sure what you were going to go on to say here, but why not? If it's a

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Inada Naoki
On Tue, Apr 23, 2019 at 11:30 AM Steve Dower wrote: > > Or possibly just "dict(existing_dict).update(new_items)". > Do you mean .update accepts values tuple? I can't think it's > My primary concern is still to avoid making CPython performance > characteristics part of the Python language

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Glenn Linderman
On 4/22/2019 7:27 PM, Steve Dower wrote: On 22Apr2019 1921, Steve Dower wrote: On 22Apr2019 1822, Glenn Linderman wrote: Inada is now proposing a way to allow the coder to suggest a group of dictionaries that might benefit from the same gains, by preclassifying non-__dict__ slot dictionaries

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Glenn Linderman
On 4/22/2019 7:27 PM, Steve Dower wrote: On 22Apr2019 1921, Steve Dower wrote: On 22Apr2019 1822, Glenn Linderman wrote: Inada is now proposing a way to allow the coder to suggest a group of dictionaries that might benefit from the same gains, by preclassifying non-__dict__ slot dictionaries

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Steve Dower
On 22Apr2019 1921, Steve Dower wrote: On 22Apr2019 1822, Glenn Linderman wrote: Inada is now proposing a way to allow the coder to suggest a group of dictionaries that might benefit from the same gains, by preclassifying non-__dict__ slot dictionaries to do similar sharing. CSV reader is an

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Steve Dower
On 22Apr2019 1822, Glenn Linderman wrote: Inada is now proposing a way to allow the coder to suggest a group of dictionaries that might benefit from the same gains, by preclassifying non-__dict__ slot dictionaries to do similar sharing. CSV reader is an exemplary candidate, because it creates

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Glenn Linderman
On 4/22/2019 5:19 PM, Steven D'Aprano wrote: Oh, you mean just like regular dicts with shared keys already do:-) https://www.python.org/dev/peps/pep-0412/ Perhaps I've missed something in this discussion, but isn't this a matter of just making the existing shared-keys functionality explicitly

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Steven D'Aprano
On Mon, Apr 22, 2019 at 10:06:20AM -0700, Chris Barker via Python-Dev wrote: > maybe a new dict mapping type -- "shared_dict" -- it would be used in > places like the csv reader where it makes sense, but wouldn't impact the > regular dict at all. > > you could get really clever an have it

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Glenn Linderman
On 4/22/2019 4:03 PM, Inada Naoki wrote: On Tue, Apr 23, 2019 at 2:18 AM Chris Barker via Python-Dev wrote: On Fri, Apr 12, 2019 at 10:20 AM Brett Cannon wrote: This doesn't strike me as needing an optimization through a dedicated method. maybe a new dict mapping type -- "shared_dict" --

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Inada Naoki
On Tue, Apr 23, 2019 at 2:18 AM Chris Barker via Python-Dev wrote: > > On Fri, Apr 12, 2019 at 10:20 AM Brett Cannon wrote: >>> >>> >> This doesn't strike me as needing an optimization through a dedicated method. > > maybe a new dict mapping type -- "shared_dict" -- it would be used in places >

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-22 Thread Chris Barker via Python-Dev
On Fri, Apr 12, 2019 at 10:20 AM Brett Cannon wrote: > >> This doesn't strike me as needing an optimization through a dedicated > method. > maybe a new dict mapping type -- "shared_dict" -- it would be used in places like the csv reader where it makes sense, but wouldn't impact the regular dict

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-12 Thread Brett Cannon
On Fri, Apr 12, 2019 at 8:35 AM Serhiy Storchaka wrote: > 12.04.19 16:44, Inada Naoki пише: > > When creating many dicts with same keys, dict need to > > lookup internal hash table while inserting each keys. > > > > It is costful operation. If we can reuse existing keys of dict, > > we can skip

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-12 Thread Inada Naoki
On Sat, Apr 13, 2019 at 12:38 AM Serhiy Storchaka wrote: > > It looks contrary to simplification made in Python 3 when we get rid of > some more efficient lists in favor of more general iterators. > Yes. This is API for special use case creates many dict having same keys, like csv.DictReader.

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-12 Thread Inada Naoki
On Fri, Apr 12, 2019 at 11:31 PM Victor Stinner wrote: > > Nice optimization! I have questions on the proposed API. > > > with_values(self, iterable, /) > > Create a new dictionary with keys from this dict and values from > > iterable. > > > > When length of iterable is different from

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-12 Thread Serhiy Storchaka
12.04.19 16:44, Inada Naoki пише: When creating many dicts with same keys, dict need to lookup internal hash table while inserting each keys. It is costful operation. If we can reuse existing keys of dict, we can skip this inserting cost. Additionally, we have "Key-Sharing Dictionary (PEP

Re: [Python-Dev] Proposal: dict.with_values(iterable)

2019-04-12 Thread Victor Stinner
Nice optimization! I have questions on the proposed API. > with_values(self, iterable, /) > Create a new dictionary with keys from this dict and values from iterable. > > When length of iterable is different from len(self), ValueError is raised. > This method does not support dict

[Python-Dev] Proposal: dict.with_values(iterable)

2019-04-12 Thread Inada Naoki
Hi, all. I propose adding new method: dict.with_values(iterable) # Motivation Python is used to handle data. While dict is not efficient way to handle may records, it is still convenient way. When creating many dicts with same keys, dict need to lookup internal hash table while inserting each