Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"You can't make a program without broken egos."


devel / comp.lang.python / Re: dict.get_deep()

SubjectAuthor
o Re: dict.get_deep()Peter J. Holzer

1
Re: dict.get_deep()

<mailman.32.1649015120.20749.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=22115&group=comp.lang.python#22115

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-python@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: dict.get_deep()
Date: Sun, 3 Apr 2022 21:45:12 +0200
Lines: 106
Message-ID: <mailman.32.1649015120.20749.python-list@python.org>
References: <CABbU2U8tdtEW8M7WB0E3nB_9cAHrZNY6cZYWgyjUHrUzMk8yhw@mail.gmail.com>
<525b0547-0ed2-776d-e862-b1d9193bd70e@devoteam.com>
<20220403194512.kcqo6zn5yjryvxz5@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="42noyssbmxq5nwen"
X-Trace: news.uni-berlin.de PaV3G9jc+drVvV5dVw2+dQtDhPzu56t9KIJi0PnQ5NRg==
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; '(which':
0.04; 'def': 0.04; 'content-type:multipart/signed': 0.05;
'issue.': 0.05; 'explicitly': 0.07; 'loop': 0.07; 'matching':
0.07; 'content-type:application/pgp-signature': 0.09; 'example:':
0.09; 'filename:fname piece:asc': 0.09; 'filename:fname
piece:signature': 0.09; 'filename:fname:signature.asc': 0.09;
'moved': 0.09; 'utility': 0.09; '"creative': 0.16; '(so': 0.16;
'__/': 0.16; 'challenge!"': 0.16; 'dict': 0.16; 'from:addr:hjp-
python': 0.16; 'from:addr:hjp.at': 0.16; 'from:name:peter j.
holzer': 0.16; 'hjp@hjp.at': 0.16; 'holzer': 0.16; 'instead?':
0.16; 'neat.': 0.16; 'nested': 0.16; 'path:': 0.16; 'reality.':
0.16; 'stross,': 0.16; 'structural': 0.16; 'structure.': 0.16;
'url-ip:212.17.106.137/32': 0.16; 'url-ip:212.17.106/24': 0.16;
'url-ip:212.17/16': 0.16; 'url:hjp': 0.16; '|_|_)': 0.16;
'wrote:': 0.16; 'instead': 0.17; 'probably': 0.17; 'to:addr
:python-list': 0.20; 'code': 0.23; 'lines': 0.23; "i'd": 0.24;
'pattern': 0.26; 'solution.': 0.26; 'function': 0.27; 'expect':
0.28; 'sense': 0.28; 'recently': 0.29; 'it,': 0.29; 'takes': 0.31;
'default': 0.31; 'python-list': 0.32; 'returning': 0.32; 'but':
0.32; "i'm": 0.33; 'path': 0.33; 'same': 0.34; 'package': 0.34;
'header:In-Reply-To:1': 0.34; 'one.': 0.35; 'subject:skip:d 10':
0.35; 'final': 0.35; 'cases': 0.36; 'class': 0.37; 'quite': 0.39;
'least': 0.39; 'single': 0.39; 'list': 0.39; 'wrote': 0.39;
'advantage': 0.40; 'match': 0.40; 'something': 0.40; 'should':
0.40; 'method': 0.61; 'skip:\xc2 10': 0.62; 'gives': 0.62;
'proposal.': 0.62; 'received:212': 0.62; 'data,': 0.63; 'copy':
0.63; 'pass': 0.64; 'key': 0.64; 'service': 0.64; 'your': 0.64;
'received:userid': 0.66; 'that,': 0.67; 'exactly': 0.68; 'deeply':
0.69; 'latter': 0.69; 'url-ip:212/8': 0.69; 'too.': 0.70;
'little': 0.73; 'plus': 0.73; '8bit%:100': 0.76; 'documented':
0.76; 'implemented': 0.76; 'need.': 0.84; 'up,': 0.84; 'carrier':
0.84; 'lines,': 0.84; 'received:at': 0.84; 'sulla': 0.84; 'sure.':
0.84; 'skip:d 30': 0.86; 'badly': 0.91
Content-Disposition: inline
In-Reply-To: <525b0547-0ed2-776d-e862-b1d9193bd70e@devoteam.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <20220403194512.kcqo6zn5yjryvxz5@hjp.at>
X-Mailman-Original-References: <CABbU2U8tdtEW8M7WB0E3nB_9cAHrZNY6cZYWgyjUHrUzMk8yhw@mail.gmail.com>
<525b0547-0ed2-776d-e862-b1d9193bd70e@devoteam.com>
 by: Peter J. Holzer - Sun, 3 Apr 2022 19:45 UTC
Attachments: signature.asc (application/pgp-signature)

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

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).

[...]
> > 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'm not sure whether I considered this when I wrote it, but a function
has the advantage of working with every class which can be indexed. A
method must be implemented on any class (so at least dict and list to be
useful).

> 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 = {"users": [{"address": {"street": "Baker"}}]}
>
> match data:
>     case {"users": [{"address": {"street": street}}]}:
>         print(f"street: {street}")
>
>     case _:
>         print("unsupported message structure")

Neat. But that's 5 lines instead of one. I simple loop around try/except
also takes 5 lines, and the latter can be easily moved into a function,
like this:

def get_nested(coll, path, default=None, cast=None):
for i in path:
try:
coll = coll[i]
except (KeyError, IndexError, TypeError):
return default
if cast:
coll = cast(coll)
return coll

which can then be called in a single line.

> Structural matching gives you warranty you process exactly message you
> expect and explicitly discards messages with another structure.

True, and sometimes useful, I'm sure. Not sure whether it would have
helped me in the cases where I used the utility function above.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)

devel / comp.lang.python / Re: dict.get_deep()

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor