Skip to main content

Lookup nested data structures

Project description

PyPI Versions Travis Codecov Black

lookuper makes it easy to lookup a target in nested data structures. A lookup yields the values matching a target passed as an arguments list:

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

A target can contain stars (*) to match anything and globstars (**) to match anything recursively:

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

Note that these special characters can be escaped:

>>> list(lookup({'*': 1}, r'\*'))

A target can also contain functions and regular expressions:

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


lookuper can be combined with other libraries like more-itertools to return only one value:

>>> from more_itertools import only
>>> def lookup1(data, *targets, **kw):
...     return only(lookup(data, *targets), **kw)
>>> lookup1({}, 'a')
>>> lookup1({'a': 1}, 'a')
>>> lookup1({'a': 1, 'b': 2}, '*')
Traceback (most recent call last):
ValueError: Expected exactly one item in iterable, but got 1, 2, and perhaps more.


By default, lookuper only supports nested data structures like mappings, sequences and sets. It can extended to support other types:

>>> from lookuper import lookup_data
>>> _ = lookup_data.register(object, lambda data: (
...     (name, getattr(data, name, None)) for name in dir(data)
... ))
>>> list(lookup(object(), '__class__', '__class__', '__name__'))

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.

Files for lookuper, version 0.5.1
Filename, size File type Python version Upload date Hashes
Filename, size lookuper-0.5.1.tar.gz (7.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page