A minimalist Python library for manipulating nested data structures with ease and performance.
Project description
🌲Carve
A minimalist Python library for manipulating nested data structures with ease and performance.
Take a look:
>>> from carve import treemap
>>> obj = {"john": {"doe": [{"puma": "yes", "adidas": None}]}}
>>> treemap(obj, remove_empty)
{"john": {"doe": [{"puma": "yes"}]}}
>>> treemap(obj, lambda k,v,p: ("PUMA", "puma") if k == "puma" else (k,v))
{"john": {"doe": [{"PUMA": "puma", "adidas": None}]}}
Quick Start
Install using pip/pipenv/etc. (we recommend poetry for sane dependency management):
$ poetry add carve
Transform your dictionary using a k,v,p
context for each operation:
- k - key.
- v - value.
- p - path, in the form of a tuple: ("john", "doe") means the nested key "john.doe".
And return a key-value tuple: (key, value)
. You can:
- Return a custom value to change both key and value
("foo", "bar")
- Just modify a key:
("foo", v)
- Just modify a value:
(k, "bar")
- Remove the current entry:
(None, None)
- Decide what to do based on your current path:
(None, None) if "secret" in p else (k,v)
Builtins
You can use the following builtins for shortcut operations:
from carve import treemap, mapkey, mapval, remove, on_key, remove_empty, flow
treemap(target, remove(lambda k, v, p: k == "adidas"))
treemap(target, mapval(lambda k, v, p: "X" if len(p) > 2 else v))
treemap(target, mapkey(lambda k, v, p: "X" + v if len(p) > 2 else k))
treemap(target, on_key("puma", lambda k, v, p: (k, "X")))
treemap(target, remove_empty)
# multiple builtins, left-to-right with 'flow'
assert treemap(target, flow(scream, remove_empty))
Thanks:
To all Contributors - you make this happen, thanks!
Copyright
Copyright (c) 2018 @jondot. See LICENSE for further details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
carve-0.2.0.tar.gz
(2.9 kB
view hashes)
Built Distribution
Close
Hashes for carve-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b98ca5a49e24edc6f69432e60bd8903fa548e30a055b3d3817289523ec50e895 |
|
MD5 | 53f31f59064d74fa9386c850b1dfdcf1 |
|
BLAKE2b-256 | 09c225b11623906e5e46f8722729275f6fcf47116e705f2c5b433b030897e810 |