Skip to main content

LEVELS [of depth] - a vertical view of nested Python datastructures and JSON.

Project description

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

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

levels-0.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

levels-0.0.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file levels-0.0.2.tar.gz.

File metadata

  • Download URL: levels-0.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.15.4 CPython/3.12.3 Linux/5.15.146.1-microsoft-standard-WSL2

File hashes

Hashes for levels-0.0.2.tar.gz
Algorithm Hash digest
SHA256 17dba6db7c7be11ac7405ca660509ee85cf2c976fbc0b5337274a7d30c2afaa1
MD5 f4447a9d51de9d999d155d5b74bb083e
BLAKE2b-256 e4fd6877fdc0be47ce6c6cd1639086e8da9b1bded3896e4b1a5e3d72b3c0b28b

See more details on using hashes here.

File details

Details for the file levels-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: levels-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.15.4 CPython/3.12.3 Linux/5.15.146.1-microsoft-standard-WSL2

File hashes

Hashes for levels-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ea6f8a6b28dd8276c5fbb0fdb91891769a3dddaed5d8bc4d2b72e4ceb0aab4e7
MD5 fd881602e9e79e4193ccbfd3867ab722
BLAKE2b-256 0ffed37f6eb27a6692ef11809e43b34235db26a712094c4989a4474cc89db4a7

See more details on using hashes here.

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