Skip to main content
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'))
[1]

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'\*'))
[1]

A target can also contain functions and regular expressions:

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

Recipes

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')
1
>>> lookup1({'a': 1, 'b': 2}, '*')
Traceback (most recent call last):
...
ValueError: Expected exactly one item in iterable, but got 1, 2, and perhaps more.

Extensions

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__'))
['type']

Project information

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

Release files for lookuper 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lookuper 0.5.1
File Size Uploaded
lookuper-0.5.1.tar.gz 7.2 kB Details

Release files / lookuper-0.5.1.tar.gz

Download URL lookuper-0.5.1.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e148d05532abbd3c5d3dc3d19f57df94bb4cffaa674965ecfaf3efe1e954ca74
BLAKE2b-256 checksum
How to use checksums
c3875faab9b5e0ffadf6c3c2ab54ef94ec087dd451769dc8118108e342a54816
Upload date
Uploaded using Trusted Publishing?
What is 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.8.0

Release history Release notifications | RSS feed

This release

0.5.1 This release

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page