Skip to main content

🌲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.

Metadata

Release files for carve 0.2.1

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

Source distribution (sdist)

Source distribution for carve 0.2.1
File Size Uploaded
carve-0.2.1.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for carve 0.2.1
File Interpreter ABI Platform
carve-0.2.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 7.1 kB

Release files / carve-0.2.1.tar.gz

Download URL carve-0.2.1.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7fb83859cc1e6f71f132ac402d9ab607d2e3eeb5fb3d0784f4223dfee676dc31
BLAKE2b-256 checksum
How to use checksums
c7aeca9022c8fe24636026c4d50113a1aee71d42eaafef8e5fe66a4ad1e5f2e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.11.5 CPython/3.7.0 Darwin/17.5.0

Release files / carve-0.2.1-py2.py3-none-any.whl

Download URL carve-0.2.1-py2.py3-none-any.whl
Size 4.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2eb0302d6871c3ea620528d9325060a70382d4c4c2d1dc0d97e00dc6b655efc8
BLAKE2b-256 checksum
How to use checksums
67258882529fc892e181b038a257fc1c52dcd9d0e38cf8ae862a6ae691c8ce35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.11.5 CPython/3.7.0 Darwin/17.5.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

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