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

Uploaded Source

Supported by

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