levels


Namelevels JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
SummaryLEVELS [of depth] - a vertical view of nested Python datastructures and JSON.
upload_time2024-06-12 18:39:15
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords data nested view levels
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
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

```python
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

```python
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

```bash
python -m levels -h # prints help screen
```

### Examples

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

```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "levels",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "data, nested, view, levels",
    "author": null,
    "author_email": "Emil Bisgaard Commerou <emil@commerou.dk>",
    "download_url": "https://files.pythonhosted.org/packages/e4/fd/6877fdc0be47ce6c6cd1639086e8da9b1bded3896e4b1a5e3d72b3c0b28b/levels-0.0.2.tar.gz",
    "platform": null,
    "description": "# levels\n\nA library to give a vertical view of nested data structures of any depth in Python.  \nNested data structures are common and can be hard to navigate and debug by hand/eye.   \nCurrently only supports dictionaries and lists (JSON-like structures).  \n\nThe base function (levels) gives a path and value for each key at a defined level.  \n\nUseful when handling nested data structures in the REPL, iPython or Jupyter notebooks.  \nAlso useful for debugging and logging.  \n\nIf you are familiar with the glom library, this library plays a similar role but with a different approach.  \nHowever, levels is not as feature-rich as glom and is not intended to be a replacement.  \n\nlevels allows you to get glom-like \"recipes\" / slices to play nicely in conjunction with glom.  \nsimply pass `glom=True` to the levels function.  \n\n## Installation\n\n```bash\npip install levels\n```\n\n\n### Short introduction to features\n\nlevels range from 0 - n, where 0 is the top level of the data structure.  \n\nlevels.levels() - returns a slice path and value for each key at a defined level.  \nlevels.get_level() - returns the level of a key or value in a dictionary.  \nlevels.find_path() - returns the full slice path to a value.  \nlevels.search() - returns the level of a value in a nested data structure.  \n\n\n## Examples\n\n```python\nfrom levels import levels\n\nnested_dict = {\n    'a': {\n        'b': {\n            'c': 1,\n            'd': 2\n        },\n        'e': 3\n    },\n    'f': {\n        'g': {\n            'h': [4,5]\n        }\n    }\n}\n\nfor path, value in levels(nested_dict, 2):\n\u00a0 \u00a0 print(path, \":\", value)\n\n# ['a']['b']['c']: 1\n# ['a']['b']['d']: 2\n# ['f']['g']['h']: [...]\n```\n\nAs can be seen this conveniently returns a path / \"slice recipe\" for the nested data structure.\n\nEyes saved!\n\nlevels is based on generators and will only consume memory and processing time if you let it run wild on big structures... \n\n### Advanced usage\n\n```python\nfrom levels import levels, get_level, find_path, search\nimport re\n\nnested_dict = {\n    'a': {\n        'b': {\n            'c': 1,\n            'd': 2\n        },\n        'e': 3\n    },\n    'f': {\n        'g': {\n            'h': [4,5]\n        }\n    }\n}\n\n# Get the level of a key\nprint(get_level(nested_dict, key='a')) # 0\nprint(get_level(nested_dict, key='b')) # 1\n\n# Get the level of a value\nprint(get_level(nested_dict, value=3)) # 1\n\n# get path and value for a key\nfor path, value in levels(nested_dict, 2, key=\"h\"):\n    print(path, \":\", value)\n\n# get path and value for a key matching a regex\nregex = re.compile(r\"[d-f]\") # match any letter between d and f\n\nfor path, value in levels(nested_dict, 2, regex_k=regex, values=True):\n    print(path, \":\", value)\n\n# get path and value for a value matching a regex\nvalue_regex = re.compile(r\"[1-2]\")  # match any digit between 1 and 2\n\nfor path, value in levels(nested_dict, 2, regex_v=value_regex):\n    print(path, \":\", value)\n\n# search for a value and return the level\nwant = 4\nlevel = search(nested_dict, value=want)\n\n# get path and value for a value at a specific level (from search)\nfor path, value in levels(nested_dict, depth=level, value=want):\n    print(path, \":\", value)\n# ['f']['g']['h'][0]: 4\n\n# find_path: a convenience function, a combination of search and levels (returns only one path!)\nwant = 2\nprint(find_path(nested_dict, want)) # ['a']['b']['d']\n```\n\n## Using it from the command-line\n\n```bash\npython -m levels -h # prints help screen\n```\n\n### Examples\n\n```bash\n    #Print the values of the JSON file at level 2:\npython -m levels data.json 2\n\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "LEVELS [of depth] - a vertical view of nested Python datastructures and JSON.",
    "version": "0.0.2",
    "project_urls": null,
    "split_keywords": [
        "data",
        " nested",
        " view",
        " levels"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0ffed37f6eb27a6692ef11809e43b34235db26a712094c4989a4474cc89db4a7",
                "md5": "fd881602e9e79e4193ccbfd3867ab722",
                "sha256": "ea6f8a6b28dd8276c5fbb0fdb91891769a3dddaed5d8bc4d2b72e4ceb0aab4e7"
            },
            "downloads": -1,
            "filename": "levels-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fd881602e9e79e4193ccbfd3867ab722",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5307,
            "upload_time": "2024-06-12T18:39:13",
            "upload_time_iso_8601": "2024-06-12T18:39:13.920182Z",
            "url": "https://files.pythonhosted.org/packages/0f/fe/d37f6eb27a6692ef11809e43b34235db26a712094c4989a4474cc89db4a7/levels-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e4fd6877fdc0be47ce6c6cd1639086e8da9b1bded3896e4b1a5e3d72b3c0b28b",
                "md5": "f4447a9d51de9d999d155d5b74bb083e",
                "sha256": "17dba6db7c7be11ac7405ca660509ee85cf2c976fbc0b5337274a7d30c2afaa1"
            },
            "downloads": -1,
            "filename": "levels-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "f4447a9d51de9d999d155d5b74bb083e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 4857,
            "upload_time": "2024-06-12T18:39:15",
            "upload_time_iso_8601": "2024-06-12T18:39:15.099094Z",
            "url": "https://files.pythonhosted.org/packages/e4/fd/6877fdc0be47ce6c6cd1639086e8da9b1bded3896e4b1a5e3d72b3c0b28b/levels-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-12 18:39:15",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "levels"
}
        
Elapsed time: 0.31163s