Skip to main content

levels

A library to give a vertical view of nested data structures of any depth in Python.
Nested data structures are common and can be hard to navigate and debug by hand/eye.
Currently only supports dictionaries and lists (JSON-like structures).

The base function (levels) gives a path and value for each key at a defined level.

Useful when handling nested data structures in the REPL, iPython or Jupyter notebooks.
Also useful for debugging and logging.

If you are familiar with the glom library, this library plays a similar role but with a different approach.
However, levels is not as feature-rich as glom and is not intended to be a replacement.

levels allows you to get glom-like "recipes" / slices to play nicely in conjunction with glom.
simply pass glom=True to the levels function.

Installation

pip install levels

Short introduction to features

levels range from 0 - n, where 0 is the top level of the data structure.

levels.levels() - returns a slice path and value for each key at a defined level.
levels.get_level() - returns the level of a key or value in a dictionary.
levels.find_path() - returns the full slice path to a value.
levels.search() - returns the level of a value in a nested data structure.

Examples

from levels import levels

nested_dict = {
    'a': {
        'b': {
            'c': 1,
            'd': 2
        },
        'e': 3
    },
    'f': {
        'g': {
            'h': [4,5]
        }
    }
}

for path, value in levels(nested_dict, 2):
    print(path, ":", value)

# ['a']['b']['c']: 1
# ['a']['b']['d']: 2
# ['f']['g']['h']: [...]

As can be seen this conveniently returns a path / "slice recipe" for the nested data structure.

Eyes saved!

levels is based on generators and will only consume memory and processing time if you let it run wild on big structures...

Advanced usage

from levels import levels, get_level, find_path, search
import re

nested_dict = {
    'a': {
        'b': {
            'c': 1,
            'd': 2
        },
        'e': 3
    },
    'f': {
        'g': {
            'h': [4,5]
        }
    }
}

# Get the level of a key
print(get_level(nested_dict, key='a')) # 0
print(get_level(nested_dict, key='b')) # 1

# Get the level of a value
print(get_level(nested_dict, value=3)) # 1

# get path and value for a key
for path, value in levels(nested_dict, 2, key="h"):
    print(path, ":", value)

# get path and value for a key matching a regex
regex = re.compile(r"[d-f]") # match any letter between d and f

for path, value in levels(nested_dict, 2, regex_k=regex, values=True):
    print(path, ":", value)

# get path and value for a value matching a regex
value_regex = re.compile(r"[1-2]")  # match any digit between 1 and 2

for path, value in levels(nested_dict, 2, regex_v=value_regex):
    print(path, ":", value)

# search for a value and return the level
want = 4
level = search(nested_dict, value=want)

# get path and value for a value at a specific level (from search)
for path, value in levels(nested_dict, depth=level, value=want):
    print(path, ":", value)
# ['f']['g']['h'][0]: 4

# find_path: a convenience function, a combination of search and levels (returns only one path!)
want = 2
print(find_path(nested_dict, want)) # ['a']['b']['d']

Using it from the command-line

python -m levels -h # prints help screen

Examples

    #Print the values of the JSON file at level 2:
python -m levels data.json 2

Metadata

Release files for levels 0.0.2

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

Source distribution (sdist)

Source distribution for levels 0.0.2
File Size Uploaded Provenance Actions
levels-0.0.2.tar.gz 4.9 kB Without provenance Details for levels-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for levels 0.0.2
File Interpreter ABI Platform Actions
levels-0.0.2-py3-none-any.whl Python 3 none any Details for levels-0.0.2-py3-none-any.whl

Total release size: 10.2 kB

Release files / levels-0.0.2.tar.gz

Download URL levels-0.0.2.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
17dba6db7c7be11ac7405ca660509ee85cf2c976fbc0b5337274a7d30c2afaa1
BLAKE2b-256 checksum
How to use checksums
e4fd6877fdc0be47ce6c6cd1639086e8da9b1bded3896e4b1a5e3d72b3c0b28b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.15.4 CPython/3.12.3 Linux/5.15.146.1-microsoft-standard-WSL2

Release files / levels-0.0.2-py3-none-any.whl

Download URL levels-0.0.2-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea6f8a6b28dd8276c5fbb0fdb91891769a3dddaed5d8bc4d2b72e4ceb0aab4e7
BLAKE2b-256 checksum
How to use checksums
0ffed37f6eb27a6692ef11809e43b34235db26a712094c4989a4474cc89db4a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.15.4 CPython/3.12.3 Linux/5.15.146.1-microsoft-standard-WSL2

Security for levels 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

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