Skip to main content

Powerful deep search for nested dict/list structures

Project description

nestfind

Powerful deep search for nested dict/list structures in Python.

Traverses arbitrarily nested dict/list data using a flexible path-based syntax, supporting fallback paths, multiple sources, wildcard matching, predicate filtering, and more.

Installation

pip install nestfind

Quick Start

from nestfind import deep_search

data = {
    "user": {
        "profile": {
            "name": "Alice",
            "email": "alice@example.com"
        }
    }
}

deep_search(data, "user", "profile", "name")   # → "Alice"
deep_search(data, "email")                      # → "alice@example.com"  (wide search)

Path Segment Types

Segment Description Example
str Wide search key — finds key anywhere in nested structure "name"
str + "!" Condition key — returns the parent dict containing this key "uri!"
str + "?" Optional key — exact match only, skips wide search if not found "nickname?"
"*" Wildcard — matches ALL keys/items at this level "*"
int List index — exact positional access, supports negative 0, -1
callable Predicate filter — include item only if callable returns truthy lambda u: u.get("active")

Modes

Single path

deep_search(data, "a", "b", "c")

Fallback mode — tries paths in order, returns first non-empty result

deep_search(data, ["uri"], ["browser_native_hd_url"])

Multi-source mode — each list is [source, *keys]

deep_search([source1, "key1"], [source2, "key2", "key3"])

Parameters

Parameter Type Default Description
return_first bool True Return first match or list of all matches
default Any None Value to return if nothing found
type_filter type or tuple None Only return results of this type
value_filter callable None Only return results where value_filter(v) is truthy
transform callable None Apply function to each result before returning
max_depth int None Maximum nesting depth for wide search
exclude_keys list[str] None Skip these keys during wide search
strict bool False Disable wide search — exact path traversal only
with_path bool False Return (value, path) tuples instead of bare values
debug bool False Enable debug logging

Examples

from nestfind import deep_search, DeepSearch

data = {
    "users": [
        {"id": 1, "name": "Alice", "active": True},
        {"id": 2, "name": "Bob",   "active": False},
    ]
}

# Get all emails using wildcard
deep_search(data, "users", "*", "name", return_first=False)
# → ["Alice", "Bob"]

# Filter with predicate
deep_search(data, "users", lambda u: u.get("active"), "name")
# → "Alice"

# Return with path
deep_search(data, "name", with_path=True)
# → ("Alice", ["users", 0, "name"])

# Type filter
deep_search(data, "id", type_filter=int)
# → 1

# Class wrapper — bind config once, reuse
ds = DeepSearch(exclude_keys=["metadata"], max_depth=5)
ds(data, "users", "*", "name", return_first=False)
# → ["Alice", "Bob"]

DeepSearch class

Bind configuration once and reuse across calls:

class FacebookMapper:
    deep_search = DeepSearch(exclude_keys=["metadata"])

    def map(self, raw):
        return self.deep_search(raw, "user", "name")

License

MIT

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

nestfind-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nestfind-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file nestfind-0.1.0.tar.gz.

File metadata

  • Download URL: nestfind-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for nestfind-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8bdca5ad683fed3105b61c1c5113e7c041f73d12c6da07a82718930345b4bf9b
MD5 b35b67c4c548721758feaa7c3501acb4
BLAKE2b-256 773b052bd7ca0f805161c931a6271649f41a1ad361d37b1e4bfd025f6c11f1e5

See more details on using hashes here.

File details

Details for the file nestfind-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nestfind-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for nestfind-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41a80c6392c9f561aa18c69e9dd27dcfbd232d9d9bbbf856012ee1c835b895e9
MD5 7b22ff9ea8d8c65bb564f331394abd56
BLAKE2b-256 a66a3079d60b0d1c16614f4595e7235c0e363d49492a4adc7dbfc0884013d720

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page