deeppath
Python module to easily manipulate complex nested structures
Free software: MIT license
Documentation: https://deeppath.readthedocs.io.
Features
deeppath reads, writes, deletes and searches json-like data (nested dictionaries and lists) using short /-separated paths:
from deeppath import ddelete, dget, dsearch, dset, dwalk, flatten, has
data = {
"users": [
{"name": "John", "surname": "Doe", "tags": ["a", "b"]},
{"name": "Jane", "surname": "Doe", "tags": ["c"]},
],
"location": {"city": "London", "country": "United Kingdom"},
}
dget(data, "location/city") # "London"
dget(data, "users[-1]/name") # "Jane"
dget(data, "users[*]/name") # ["John", "Jane"]
dget(data, "users[*]/tags[*]") # ["a", "b", "c"]
dget(data, "users[:1]/name") # ["John"]
dget(data, "location/*") # ["London", "United Kingdom"]
dget(data, "missing", default=0) # 0
has(data, "users[1]/name") # True
dset(data, "location/zip", "E1") # creates missing keys along the way
ddelete(data, "users[*]/surname") # True, removed from every user
list(dsearch(data, "^name$")) # [("users[0]/name", "John"), ("users[1]/name", "Jane")]
list(dwalk(data)) # every (path, leaf value) pair
flatten([1, [2, [3, [4]]]]) # [1, 2, 3, 4]
Summary of the API:
dget(data, path, default=None, strict=False): return the value at path, or a list of every match when the path contains a wildcard or a slice.
has(data, path): whether path matches anything. Stops at the first match, so it’s cheap even on large wildcard fan-outs.
dset(data, path, value): set a value, creating intermediate dicts and lists as needed.
ddelete(data, path): remove every match. List elements are really removed, so later indices shift down.
dsearch(data, pattern): find every key matching a regular expression, at any depth.
dwalk(data): yield (path, value) for every leaf.
flatten(nested_list): flatten arbitrarily nested lists.
Path syntax:
a/b/c |
nested dictionary keys |
items[2] |
list index, negative indices allowed |
items[*] |
every element of a list |
* |
every value of a dictionary |
items[1:3] |
slice ([start:stop:step], each part optional) |
Note that * only matches dictionary values and [*] only matches list elements: dget(data, "users/*/name") returns [] because users is a list. See the usage guide for details.
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Release files for deeppath 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| deeppath-1.1.0.tar.gz | 34.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| deeppath-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 45.5 kB
Release files / deeppath-1.1.0.tar.gz
| Download URL | deeppath-1.1.0.tar.gz |
|---|---|
| Size | 34.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
596aa63ed65a301be25ba1df7736b1bb4f9d8e7bfcec706718c6dc4e7e50b3d9
|
|
BLAKE2b-256 checksum How to use checksums |
75f8ad0e96d4e03eb1087ebd49341862c766b1e4601cc24248262df9cfcecd8b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / deeppath-1.1.0-py3-none-any.whl
| Download URL | deeppath-1.1.0-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
af783c5e37d8a3a6653c708aa36bf332f619fa6a1b5bb42b83c528434b8fc415
|
|
BLAKE2b-256 checksum How to use checksums |
16801eed63c0470eb0d5fb2559930bdc5d3c8b5c6e03dad5c5fb4faa8a050b51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|