Re: dict.get_deep()

2022-04-03 Thread Avi Gross via Python-list
I may have misunderstood something. The original post in this subject sounded to ME likethey had nested dictionaries and wanted to be ableto ask a method in the first dictionary totake an unspecified number of arguments thatwould be successive keys and return the results. I mean if A was a dicti

Re: Sharing part of a function

2022-04-03 Thread Dan Stromberg
On Sun, Apr 3, 2022 at 2:46 PM Cecil Westerhof via Python-list < python-list@python.org> wrote: > Betty Hollinshead writes: > > > "Memoising" is the answer -- see "Python Algorithms" by Magnus Lie > Hetland. > > In the mean time, here a simplified version of "memoising" using a dict. > > This ver

Re: dict.get_deep()

2022-04-03 Thread Dieter Maurer
Marco Sulla wrote at 2022-4-3 21:17 +0200: >On Sun, 3 Apr 2022 at 18:57, Dieter Maurer wrote: >> You know you can easily implement this yourself -- in your own >> `dict` subclass. > >Well, of course, but the question is if such a method is worth to be >builtin, in a world imbued with JSON. I suppo

Re: dict.get_deep()

2022-04-03 Thread Chris Angelico
On Mon, 4 Apr 2022 at 05:19, Marco Sulla wrote: > > On Sun, 3 Apr 2022 at 18:57, Dieter Maurer wrote: > > You know you can easily implement this yourself -- in your own > > `dict` subclass. > > Well, of course, but the question is if such a method is worth to be > builtin, in a world imbued with

Re: Sharing part of a function

2022-04-03 Thread Cecil Westerhof via Python-list
Betty Hollinshead writes: > "Memoising" is the answer -- see "Python Algorithms" by Magnus Lie Hetland. > In the mean time, here a simplified version of "memoising" using a dict. > This version handles pretty large fibonacci numbers! > > # fibonacci sequence > # memoised - but using a simple dict

Re: dict.get_deep()

2022-04-03 Thread Marco Sulla
On Sun, 3 Apr 2022 at 21:46, Peter J. Holzer wrote: > > > > data.get_deep("users", 0, "address", "street", default="second star") > > Yep. Did that, too. Plus pass the final result through a function before > returning it. I didn't understand. Have you added a func parameter? > I'm not sure whet

Re: Sharing part of a function

2022-04-03 Thread Betty Hollinshead
"Memoising" is the answer -- see "Python Algorithms" by Magnus Lie Hetland. In the mean time, here a simplified version of "memoising" using a dict. This version handles pretty large fibonacci numbers! # fibonacci sequence # memoised - but using a simple dictionary (see Python Algorithms, p177) m

Re: dict.get_deep()

2022-04-03 Thread Peter J. Holzer
On 2022-04-03 17:58:09 +0300, Kirill Ratkin via Python-list wrote: > 02.04.2022 23:44, Marco Sulla пишет: > > A proposal. Very often dict are used as a deeply nested carrier of > > data, usually decoded from JSON. Sometimes I needed to get some of > > this data, something like this: > > > > data["

Re: dict.get_deep()

2022-04-03 Thread Marco Sulla
On Sun, 3 Apr 2022 at 18:57, Dieter Maurer wrote: > You know you can easily implement this yourself -- in your own > `dict` subclass. Well, of course, but the question is if such a method is worth to be builtin, in a world imbued with JSON. I suppose your answer is no. -- https://mail.python.org

Re: dict.get_deep()

2022-04-03 Thread Marco Sulla
On Sun, 3 Apr 2022 at 16:59, Kirill Ratkin via Python-list wrote: > > Hi Marco. > > Recently I met same issue. A service I intergated with was documented > badly and sent ... unpredictable jsons. > > And pattern matching helped me in first solution. (later I switched to > Pydantic models) > > For

Re: dict.get_deep()

2022-04-03 Thread Dieter Maurer
Marco Sulla wrote at 2022-4-2 22:44 +0200: >A proposal. Very often dict are used as a deeply nested carrier of >data, usually decoded from JSON. Sometimes I needed to get some of >this data, something like this: > >data["users"][0]["address"]["street"] > >What about something like this instead? > >

Re: dict.get_deep()

2022-04-03 Thread 2QdxY4RzWzUUiLuE
On 2022-04-03 at 18:01:58 +0300, Kirill Ratkin via Python-list wrote: > It seems 'case if' should help with types: > > case {"users": [{"address": {"street": street}}]} if isinstance(street, > str): reduce(lambda x, y: x[y], ["users", 0, "address", "street"], data) Unless it's y[x] rather than

Re: dict.get_deep()

2022-04-03 Thread Chris Angelico
On Mon, 4 Apr 2022 at 00:59, Kirill Ratkin via Python-list wrote: > > Hi Marco. > > Recently I met same issue. A service I intergated with was documented > badly and sent ... unpredictable jsons. > > And pattern matching helped me in first solution. (later I switched to > Pydantic models) > > For

Re: dict.get_deep()

2022-04-03 Thread Kirill Ratkin via Python-list
To my previous post. It seems 'case if' should help with types: case {"users": [{"address": {"street": street}}]} if isinstance(street, str): :) // BR 02.04.2022 23:44, Marco Sulla пишет: A proposal. Very often dict are used as a deeply nested carrier of data, usually decoded from JSON.

Re: dict.get_deep()

2022-04-03 Thread Kirill Ratkin via Python-list
Hi Marco. Recently I met same issue. A service I intergated with was documented badly and sent ... unpredictable jsons. And pattern matching helped me in first solution. (later I switched to Pydantic models) For your example I'd make match rule for key path you need. For example: data = {