On Thu, Aug 27, 2020 at 2:27 PM Ricky Teachey <ri...@teachey.org> wrote:

>
> On Thu, Aug 27, 2020 at 2:14 PM Christopher Barker <python...@gmail.com>
> wrote:
>
>> On Thu, Aug 27, 2020 at 11:01 AM Ricky Teachey <ri...@teachey.org> wrote:
>>
>>> What about something like this:
>>>>
>>>>
>>>>> class Name(NamedTuple):
>>>>>     first: str
>>>>>     last: str
>>>>>
>>>>> d = NamedKeyDict(Named)
>>>>> d[first='david', last='mertz'] = 1_000_000  # dollars
>>>>>
>>>>
>> right -- that would be a new custom class that took advantage of this
>> feature.
>>
>> Are you suggesting that the built in dict be extended to support this?
>> I'm pretty sure Jonathan Fine did suggest that -- but I don't think that's
>> a good idea myself.
>>
>> -CHB
>>
>
> No not the built-in, but maybe as an addition to the collections module.
>

Actually it just occurred to me-- why limit the idea to namedtuples? The
collections module could have a dict that maps the *args and **kwargs in
the [ ] to any arbitrary callable:

class MapDict:
    def __init__(self, f, __dict=None, **kwargs):
        self.callable = f
        self._dict = {}
          __dict = {**__dict if __dict is not None else {}, **kwargs}

        self.update(__dict)
    def __getitem__(self, key, **kwargs):
        args = self.translate_key(key)
        return self._dict[f(*args, **kwargs)]
    def __getitem__(self, key, value, **kwargs):
        args = self.translate_key(key)
        self._dict[f(*args, **kwargs)] = value
    def __delitem__(self, key, **kwargs):
        args = self.translate_key(key)
        del self._dict[f(*args, **kwargs)]
    def update(self, d, **kwargs):
        d.update(kwargs)
        for k,v in d.items():
            self[k] = v
    def translate_key(self, key):
        try:
            ikey = iter(key)
        except TypeError:
            ikey = iter((key,))
        return (*ikey,)

>>> from main import MapDict
>>> resolved_paths = MapDict(lambda s, *, strict=True:
Path(s).resolve(strict=strict))
>>> resolved_paths[r"spam/eggs/cheese", strict=True] = None

---
Ricky.

"I've never met a Kentucky man who wasn't either thinking about going home
or actually going home." - Happy Chandler
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JWSKDP3N7BYGOK5DRBG2RC7SPSA4LYOX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to