Re: dict.get_deep()

2022-04-07 Thread Peter J. Holzer
On 2022-04-03 23:17:04 +0200, Marco Sulla wrote: > 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 unde

Re: dict.get_deep()

2022-04-04 Thread Cameron Simpson
On 03Apr2022 21:45, Peter J. Holzer wrote: >Yup. I need something like this quite frequently, so I wrote a little >utility function (which I copy and paste into lots of code - I probably >should package that up, but a package with a single short function feels >weird). Start with one with a sligh

Fwd: dict.get_deep()

2022-04-04 Thread Albert-Jan Roskam
-- Forwarded message -- From: Marco Sulla Date: Apr 2, 2022 22:44 Subject: dict.get_deep() To: Python List <> Cc: A proposal. Very often dict are used as a deeply nested carrier of data, usually decoded from JSON.  data["users&qu

Re: dict.get_deep()

2022-04-04 Thread Avi Gross via Python-list
ns offered. -Original Message- From: Kirill Ratkin via Python-list To: python-list@python.org Sent: Mon, Apr 4, 2022 3:40 am Subject: Re: dict.get_deep() Hello, Yes, I misunderstood as well because started to think about pattern matching which is good but this is not subject the question

Re: dict.get_deep()

2022-04-04 Thread Kirill Ratkin via Python-list
asort of built-in data structure but could work for one of many variantsalready implemented in modules. -Original Message- From: Marco Sulla To: Peter J. Holzer Cc: python-list@python.org Sent: Sun, Apr 3, 2022 5:17 pm Subject: Re: dict.get_deep() On Sun, 3 Apr 2022 at 21:46, Peter

Re: dict.get_deep()

2022-04-03 Thread Avi Gross via Python-list
y implemented in modules. -Original Message- From: Marco Sulla To: Peter J. Holzer Cc: python-list@python.org Sent: Sun, Apr 3, 2022 5:17 pm Subject: Re: dict.get_deep() On Sun, 3 Apr 2022 at 21:46, Peter J. Holzer wrote: > > > > data.get_deep("users", 0, "add

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: 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: 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 = {

dict.get_deep()

2022-04-02 Thread 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["users"][0]["address"]["street"] What about something like this instead? data.get_deep("users", 0, "address", "street") and