Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Measure with a micrometer. Mark with chalk. Cut with an axe.


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

SubjectAuthor
o Re: dict.get_deep()2QdxY4RzWzUUiLuE

1
Re: dict.get_deep()

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: 2QdxY4Rz...@potatochowder.com
Newsgroups: comp.lang.python
Subject: Re: dict.get_deep()
Date: Sun, 3 Apr 2022 10:44:04 -0500
Lines: 34
Message-ID: <mailman.28.1649002370.20749.python-list@python.org>
References: <CABbU2U8tdtEW8M7WB0E3nB_9cAHrZNY6cZYWgyjUHrUzMk8yhw@mail.gmail.com>
<8c7d83a2-20fe-3697-08a1-2fe733247976@devoteam.com>
<YknAxLLhK1F7dudu@scrozzle>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de FFwJDsp4+j5LbqjeSnjpfgUYzL9cnqlSZGcq/SeIO4Gw==
Return-Path: <2QdxY4RzWzUUiLuE@potatochowder.com>
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.025
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'this:': 0.03; 'data)':
0.09; 'received:78': 0.09; 'dict': 0.16;
'from:addr:2qdxy4rzwzuuilue': 0.16; 'from:addr:potatochowder.com':
0.16; 'instead?': 0.16; 'message-id:@scrozzle': 0.16; 'nested':
0.16; 'received:136.243': 0.16; 'received:www458.your-server.de':
0.16; 'received:your-server.de': 0.16; 'wrote:': 0.16; 'instead':
0.17; 'to:addr:python-list': 0.20; 'received:de': 0.23; 'seems':
0.26; 'python-list': 0.32; 'received:136': 0.32; 'unless': 0.32;
'there': 0.33; 'header:In-Reply-To:1': 0.34; 'subject:skip:d 10':
0.35; 'also,': 0.36; "it's": 0.37; 'way': 0.38; 'something': 0.40;
'should': 0.40; 'proposal.': 0.62; 'data,': 0.63; 'deeply': 0.69;
'carrier': 0.84; 'received:70': 0.84; 'sulla': 0.84; 'skip:d 30':
0.86; 'relatives': 0.91
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <8c7d83a2-20fe-3697-08a1-2fe733247976@devoteam.com>
X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com
X-Virus-Scanned: Clear (ClamAV 0.103.5/26501/Sun Apr 3 10:27:13 2022)
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: <YknAxLLhK1F7dudu@scrozzle>
X-Mailman-Original-References: <CABbU2U8tdtEW8M7WB0E3nB_9cAHrZNY6cZYWgyjUHrUzMk8yhw@mail.gmail.com>
<8c7d83a2-20fe-3697-08a1-2fe733247976@devoteam.com>
 by: 2QdxY4Rz...@potatochowder.com - Sun, 3 Apr 2022 15:44 UTC

On 2022-04-03 at 18:01:58 +0300,
Kirill Ratkin via Python-list <python-list@python.org> 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 x[y], and there might be a More Cleverer™
way to do it with getattr or one of its relatives instead of lambda.

> 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")
> >
> > and also, instead of this
> >
> > try:
> > result = data["users"][0]["address"]["street"]
> > except KeyError, IndexError:
> > result = "second star"
> >
> > write this:
> >
> > data.get_deep("users", 0, "address", "street", default="second star")

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor