Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

How can you work when the system's so crowded?


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

SubjectAuthor
o Re: dict.get_deep()Avi Gross

1
Re: dict.get_deep()

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: avigr...@verizon.net (Avi Gross)
Newsgroups: comp.lang.python
Subject: Re: dict.get_deep()
Date: Mon, 4 Apr 2022 12:50:10 +0000 (UTC)
Lines: 194
Message-ID: <mailman.40.1649076617.20749.python-list@python.org>
References: <CABbU2U8tdtEW8M7WB0E3nB_9cAHrZNY6cZYWgyjUHrUzMk8yhw@mail.gmail.com>
<525b0547-0ed2-776d-e862-b1d9193bd70e@devoteam.com>
<20220403194512.kcqo6zn5yjryvxz5@hjp.at>
<CABbU2U_oXPYYznc0u2rA02RbC6KJUcMqFnBO7j7kvmgCK5amGA@mail.gmail.com>
<1617313993.737226.1649028275904@mail.yahoo.com>
<33bfa854-004f-30e2-1ebe-b964797d1e73@devoteam.com>
<1479491782.811817.1649076610483@mail.yahoo.com>
Reply-To: Avi Gross <avigross@verizon.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de ksdoD0tbLUdA4mC7mcqbJQQPeb9E9YgIaDqJrVPjpc4g==
Return-Path: <avigross@verizon.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=verizon.net header.i=@verizon.net header.b=c1/ikv5w;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'def':
0.04; '2022': 0.05; 'containing': 0.05; 'parameter': 0.05; 'pypi':
0.05; 'matching': 0.07; 'modules': 0.07; 'sun,': 0.07; '"""':
0.09; 'else:': 0.09; 'modules.': 0.09; 'parse': 0.09;
'represents': 0.09; 'request?': 0.09; 'url-ip:151.101.0.223/32':
0.09; 'url-ip:151.101.128.223/32': 0.09; 'url-
ip:151.101.192.223/32': 0.09; 'url-ip:151.101.64.223/32': 0.09;
'url:mailman': 0.15; '(so': 0.16; '>>>>': 0.16; '??.': 0.16;
'args[0]': 0.16; 'argument"': 0.16; 'arguments': 0.16; 'avi':
0.16; 'dict': 0.16; 'dictionary.': 0.16; 'empty"': 0.16;
'expects': 0.16; 'gross': 0.16; 'holzer': 0.16; 'key,': 0.16;
'key.': 0.16; 'missed': 0.16; 'mistake.': 0.16; 'n-1': 0.16;
'nested': 0.16; 'parameter?': 0.16; 'probe': 0.16; 'protocol.':
0.16; 'something.': 0.16; 'stdlib': 0.16; 'tries': 0.16; 'tuple':
0.16; 'understand.': 0.16; 'url:project': 0.16; 'url:pypi': 0.16;
'wrote:': 0.16; 'python': 0.16; 'values': 0.17; "can't": 0.17;
'it?': 0.19; 'to:addr:python-list': 0.20; 'language': 0.21;
'maybe': 0.22; 'skip:& 40': 0.22; 'skip:_ 10': 0.22; 'way.': 0.22;
'lines': 0.23; 'to:name:python-list@python.org': 0.24; '(and':
0.25; 'skip:- 10': 0.25; 'url-ip:188.166.95.178/32': 0.25; 'url-
ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'url-
ip:188.166/16': 0.25; 'seems': 0.26; 'again,': 0.26; 'object':
0.26; 'pattern': 0.26; 'task': 0.26; 'function': 0.27; 'expect':
0.28; 'email addr:python.org&gt;': 0.28; 'ideas': 0.28; 'it,':
0.29; 'approach': 0.31; 'deep': 0.31; 'default': 0.31; 'raise':
0.31; 'url-ip:188/8': 0.31; 'think': 0.32; 'question': 0.32;
'fine.': 0.32; 'objects': 0.32; 'point,': 0.32; 'python-list':
0.32; 'replies': 0.32; 'retrieve': 0.32; 'returning': 0.32;
'right,': 0.32; 'structure': 0.32; 'but': 0.32; "i'm": 0.33;
'there': 0.33; 'march': 0.33; "didn't": 0.34; 'mean': 0.34;
'package': 0.34; 'header:In-Reply-To:1': 0.34; 'one.': 0.35;
'subject:skip:d 10': 0.35; 'url:)': 0.35; 'yes,': 0.35; 'final':
0.35; 'request': 0.35; 're:': 0.64; 'his': 0.65; 'process.': 0.65;
'similar': 0.65; 'look': 0.65; 'well': 0.65; 'back': 0.67;
'that,': 0.67; 'cc:': 0.69; 'deeply': 0.69; 'generator': 0.69;
'it:': 0.69; 'obvious': 0.69; 'ps:': 0.69; 'remaining': 0.69;
'skip:w 20': 0.69; 'solutions': 0.70; 'too.': 0.70; 'street':
0.71; 'content': 0.72; 'deal': 0.73; 'plus': 0.73; 'operate':
0.75; 'yourself': 0.75; 'skip:f 20': 0.75; 'finds': 0.76;
'implemented': 0.76; 'sent:': 0.78; 'header:Reply-To:1': 0.79;
'8bit%:67': 0.81; 'returned': 0.81; 'email name:&lt;python-list':
0.84; 'eventually': 0.84; 'indexes': 0.84; 'method,': 0.84; 'obj':
0.84; 'sequence.': 0.84; 'sulla': 0.84; 'valid,': 0.84; 'behind':
0.88; 'skip:\xd0 10': 0.91; 'interested,': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=verizon.net; s=a2048;
t=1649076614; bh=VOsGL264Sgd+Yiy1823/RiPsQCrmGTsW4H/C4NYAiAo=;
h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject:Reply-To;
b=c1/ikv5wWcSg/QOGQTiVIewcjSoaFerWP/zTekZmIuJFfotMovY03hOe21FI+2aWZM8Rt4K6RtsYA1dgN4XbMT5sq+FtAWf094iv6TlVJKRh3vyfYXTjkKaTu92cl6WNXRm+EEcDLpZsTYsucUflsZ1TfrvrXS5TL3tH9B29necc+IOM3Z4vTDwg8c46j+XU7M1MSQYpQgsdiSQg8wKH899jkUIsSxegmqR9F0njvQHu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fYClhRRKArojbM7shWgqdpsrRTWncnnIiZKzkPIMd8HPwP3Dra0nyw
PHCh_pQYprF5CmYmRrcvDMxIX1km5S2pmBJrTIzqfM4HZnBtuHydJFJGeM3sBT_7vhx9TZVDR.Q8
Qk92buiP9oVtZBuejqpkXngzKDb3enoHojD1434HXBRzHOmNsAVSIX33StbprM5lMGyyr0GRw93q
VtqoIu2OQt2oofoUJnwzRFBUXS59Ip1HqBkZEKvqiGxauznovAOilYevm9SOieBqE9RU2j4UpdmO
FLRndMc6kB7rSu7DVt2gJ_hqlSYGaALuBtQ4VTFHNyYNtL3Y6DpfKTmSEXyIkzIPxWHhfph1cySx
9lfcMmhT5xz8n5y1mYCX8MgzOACoSYpkWojNeMLCmrU9Yb5wNRTdqSnE9G2pf4uEcjGwMWUSp7wI
iSG8eIaUlnHHcoAHnuYfqoN4hl2.WbPFFr3JSQGeCvPBdTlL49BahlTDYphTs8xX5wNgieGtPcrw
fjU1hPFoqGc9QOvgQhKcJMM.Ppdf7EhuCOd.QOvLpMR25QKL1.gzRyTyUI.pwF7XsgyPY.jWRjwF
1YKmgOKuOSanp0duOrBJ72IzqgKBiJLvDt.ihqo_BohWKS1qqFp9JradTESHC9EyK9fDcHrQ2Mt0
qvTtq5tnWT4sIcnpLX6q0Z1QaZLBkb0JIsY9EfZ71BS00tn72KeGLLFlCwc8hIE.DlRkWURZof98
TtS11EDDUxu6oULo1oZQynye4x0P2o0xZ
X-Sonic-MF: <avigross@verizon.net>
In-Reply-To: <33bfa854-004f-30e2-1ebe-b964797d1e73@devoteam.com>
X-Mailer: WebService/1.1.20001 aolwebmail
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <1479491782.811817.1649076610483@mail.yahoo.com>
X-Mailman-Original-References: <CABbU2U8tdtEW8M7WB0E3nB_9cAHrZNY6cZYWgyjUHrUzMk8yhw@mail.gmail.com>
<525b0547-0ed2-776d-e862-b1d9193bd70e@devoteam.com>
<20220403194512.kcqo6zn5yjryvxz5@hjp.at>
<CABbU2U_oXPYYznc0u2rA02RbC6KJUcMqFnBO7j7kvmgCK5amGA@mail.gmail.com>
<1617313993.737226.1649028275904@mail.yahoo.com>
<33bfa854-004f-30e2-1ebe-b964797d1e73@devoteam.com>
 by: Avi Gross - Mon, 4 Apr 2022 12:50 UTC

Kirill,
There are many related needs and issues and solutions such as how to parse XML content and do all kinds of tree searches for "patterns" that multiple modules have been created to deal with.
My impression here was of a simpler request to allow a list of keys to be applied in sequence. The example wanted the list to be successive arguments in a call to a method, but obvious variants would be for a single argument containing atuple or list or any interator that would probe a tree of possibilities while anchored to the top.
This is a much easier task with many solutions offered.

-----Original Message-----
From: Kirill Ratkin via Python-list <python-list@python.org>
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 was about.

Sorry for my mistake.

Because question was about 'builtin' function which means stdlib
function implemented in python itself or even in C.

It seems, maybe I miss again, but we are talking about similar ideas
behind 'xpath' or 'jsonpath' or even 'LINQ'. We want to find some 'dsl'
which give us simple and safe way to get deeply nested values from dict.

There are several similar solutions on pypi
(https://pypi.org/project/dpath/, https://pypi.org/project/path-dict/).

But maybe (and maybe I miss again) we talk about language embedded
solution like operator ? or ??.

For example deep dict extraction could look like: street =
data["users"]?[0]?["address"]?["street"]?.

// BR

04.04.2022 2:24, 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 dictionary containing saycities and it had an alphabetical index of lettersA to Z and those contained dictionaries ofsay last names as additional dictionaries andso on, then they wanted to perhaps say;
> A.getdeep("Boston", "S", "Smith", "Address", default="None")
> But the replies I am seeing look so different that I mayhave missed something as it seems more about usingpattern matching on the data used to make the dictionariesor something.
> So I was happy to see Marco suggesting a function alongthe lines of my thought process. But I have another  thought.A stand-alone function along his lines might be fine. Buta method built into a general Dictionary class is anotherthing as it asks a method in one dictionary to march aroundinto other dictionaries. So I wonder if a better methodis sort of recursive.
> If you had a class like dictionary that had a getdeep function,and it got called with N arguments, and perhaps a namedargument supplying a default, then would it make sensefor the function checking to see if the FIRST argument canbe found as a key to the current dictionary.
> If arguments remain then it should expect to finda result that is a dictionary (or perhaps some otherobject that supports the getdeep() protocol and ask thatobject to operate on the N-1 remaining arguments, passingthe default along too.
> If the request is valid, after some iterations an object willhave a method invoked with a single argument (plus default)and a value passed back up the chain. For any errors alongthe way, the default would be returned.
> Is this closer to the spirit of the request? I view this versionof nested dictionaries as a sort of tree structure with variablebranches along the way. So an approach like this could makesense and perhaps Python could be updated eventually to havesome objects support such a protocol.
> Of course you could sort of do it yourself by subclassing somethingand making changes but that may not work for what is already asort of built-in data structure but could work for one of many variantsalready implemented in modules.
>
>
>
> -----Original Message-----
> From: Marco Sulla <Marco.Sulla.Python@gmail.com>
> To: Peter J. Holzer <hjp-python@hjp.at>
> 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 <hjp-python@hjp.at> 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 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).
> You're right, but where to put it? I don't know if an iterableutil package
> exists. If included in the stdlib, I don't know where to put it. In
> collections maybe?
>
> PS: if you're interested, here is my implementation:
>
> def get_deep(self, *args, default=_sentinel):
>      r"""
>      Get a nested element of the dictionary.
>
>      The method accepts multiple arguments or a single one.. If a single
>      argument is passed, it must be an iterable. This represents the
>      keys or indexes of the nested element.
>
>      The method first tries to get the value v1 of the dict using the
>      first key. If it finds v1 and there's no other key, v1 is
>      returned. Otherwise, the method tries to retrieve the value from v1
>      associated with the second key/index, and so on.
>
>      If in any point, for any reason, the value can't be retrieved, the
>      `default` parameter is returned if specified. Otherwise, a
>      KeyError or an IndexError is raised.
>      """
>
>      if len(args) == 1:
>          single = True
>
>          it_tpm = args[0]
>
>          try:
>              len(it_tpm)
>              it = it_tpm
>          except Exception:
>              # maybe it's a generator
>              try:
>                  it = tuple(it_tpm)
>              except Exception:
>                  err = (
>                      f"`{self.get_deep.__name__}` called with a single " +
>                      "argument supports only iterables"
>                  )
>
>                  raise TypeError(err) from None
>      else:
>          it = args
>          single = False
>
>      if not it:
>          if single:
>              raise ValueError(
>                  f"`{self.get_deep.__name__}` argument is empty"
>              )
>          else:
>              raise TypeError(
>                  f"`{self.get_deep.__name__}` expects at least one argument"
>              )
>
>      obj = self
>
>      for k in it:
>          try:
>              obj = obj[k]
>          except (KeyError, IndexError) as e:
>              if default is _sentinel:
>                  raise e from None
>
>              return default
>
>      return obj
--
https://mail.python.org/mailman/listinfo/python-list

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor