Skip to main content

lookup nested data structures

Project description

PyPI Versions Travis Codecov

lookuper makes it easy to lookup a target in nested data structures. A lookup can return the values matching a target as a string or, equivalently, as a list:

>>> from lookuper import lookup
>>> list(lookup('a.0.b', {'a': [{'b': 1}]}))
[1]
>>> list(lookup(['a', 0, 'b'], {'a': [{'b': 1}]}))
[1]

As a string, a target can contain stars (‘*’) or globstars (‘**’):

>>> list(lookup('a.*', {'a': {'b': 1, 'B': 2}}))
[1, 2]
>>> list(lookup('**.b', [{'b': 1}, {'a': {'b': 2}}]))
[1, 2]

As a list, it can contain regular expressions or functions:

>>> import re
>>> list(lookup(['a', re.compile(r'[a-z]')], {'a': {'b': 1, 'B': 2}}))
[1]
>>> from lookuper import match_key
>>> list(lookup(['a', match_key(str.islower)], {'a': {'b': 1, 'B': 2}}))
[1]

A lookup can also be useful to update all dictionaries that match the key b:

>>> from lookuper import GLOBSTAR, match
>>> data = {'a': {'b': 1}, 'c': [{'b': 2}]}
>>> for d in lookup([GLOBSTAR, match(key='b')], data):
...   d.update(b=d['b'] + 1)
>>> data
{'a': {'b': 2}, 'c': [{'b': 3}]}

Or to update all lists that contain 0:

>>> data = {'a': [0, 1], 'b': {'c': [0, 2]}}
>>> for l in lookup([GLOBSTAR, match(value=0)], data):
...   l.remove(0)
>>> data
{'a': [1], 'b': {'c': [2]}}

Project information

lookuper is released under the MIT license, the code on GitHub, and the latest release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lookuper-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

File details

Details for the file lookuper-0.1.0.tar.gz.

File metadata

  • Download URL: lookuper-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.4

File hashes

Hashes for lookuper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d9f06a0e24b949d25e7ec94e3d49642a6c602373b0002a612f2e4e32b9d389ba
MD5 44d8e466a36ac38fdd2b90c26ea1a7ac
BLAKE2b-256 3167627781c9e3de3fd94f5017406bdffd93a3cc405e4156c3878078f6892192

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page