Skip to main content

Nested dictionary with indexed field queries, merge and serialization

Project description

mod_dict

A Python extension (C++) that stores nested dictionaries by reference and provides indexed filtering, sorting, grouping and merging — without converting to a DataFrame or a database.

import mod_dict as md

mn = md.ModDict()
mn["alice"] = {"age": 30, "score": 9.5, "active": True,
               "meta": {"level": 5, "details": {"rank": 42}}}

# Field access via chaining on stored PyObject* — no copy
age  = mn["alice"]["age"]                        # 30
rank = mn["alice"]["meta"]["details"]["rank"]    # 42
mn["alice"]["age"] = 31                          # update in-place

# Indexed filter / sort / group — auto-builds index on first call
adults = mn.filter("age").gte(18)
active = mn.filter("active").eq(True)
rows   = mn.sort_by("age")                       # → [row, row, ...]
keys   = mn.sort_by("age", returns="parent_keys")# → [key, key, ...]
ages   = mn.sort_by("age", returns="values")     # → [18, 25, 30, ...]
groups = mn.group_by("age")                      # → {value: ModDict, ...}
slim   = mn.select(["age", "score"])             # → new ModDict
rows   = mn.select(["age", "score"], returns="values")  # → [{"age":..}, ..]

# Dot-notation paths in sort / group / select
mn.sort_by("meta.details.rank")
mn.group_by("meta.level")
mn.select(["meta.details.rank", "score"])

# Update from another collection
mn.update(other)                                 # bulk insert (like dict.update)
mn.update(other, "*", "*")                       # key-to-key merge
mn.update(other, "user_id", "id")               # field-to-field
mn.update(other, "*.geo.lat", "*.geo.lat")      # deep field only

# Aliases — transparent second key for the same row
mn.alias("alice", "al")
mn["al"]["age"] = 32          # same row — mn["alice"]["age"] == 32
mn["al"] = {"age": 33}        # replaces row via alias
del mn["al"]                  # removes both alias and original
print(mn.aliases())           # {"al": "alice"}

# Binary serialization (full Python type set — date, bytes, Decimal, Path, …)
data = mn.serialize()
mn2  = md.ModDict(); mn2.deserialize(data)

# Custom type converters — applied at insert time
md.register_converter(Temperature, lambda t: t.celsius)

Architecture

Rows are stored as PyObject* references — the same dict object the caller passed in, with Py_INCREF. No deep copy.

mn["alice"] = row_dict          # Py_INCREF(row_dict), store pointer
mn["alice"]["age"]              # outer hash → PyObject* → PyDict_GetItemString

On top of the outer hash table, a FieldIndex per field powers O(1) equality and O(log n) range queries. Indices are built automatically on the first filter() / sort_by() / group_by() call and reused after.

When it fits

  • Collection of records with a fixed (or semi-fixed) schema.
  • You need indexed filter, sort, group, or field-level merge without pandas/SQL.
  • Write once, query many times.
  • In-process cache shared across asyncio coroutines — zero-copy, no GC pressure on reads.

When it does not fit

  • Truly write-heavy, latency-sensitive workloads — mn[k] = row is slightly slower than dict (refcount + hash).
  • You need concurrent writes from multiple threads.
  • Schema is fully dynamic with no repeating field names.

API at a glance

# Write
mn[key] = value                          # scalar or nested dict
mn[key]["field"] = value                 # update field in-place
del mn[key]

# Read
mn[key]                                  # full row — O(1), returns stored dict ref
mn[key]["field"]                         # field via Python chaining
mn.get(key, default)

# Membership / size
key in mn
len(mn)                                  # aliases are not counted

# Iteration — aliases are hidden
for key in mn: ...
mn.keys() / mn.values() / mn.items()

# Filter (auto-builds index on first call, reused after)
mn.filter("age").gte(18)                         # → ModDict
mn.filter("age").between(30, 40)
mn.filter("active").eq(True)
mn.filter("orders.?.status").eq("shipped")       # wildcard path

# Sort / select / group — support dot-notation paths
mn.sort_by("age", reverse=False, returns="rows")        # default → [row, ...]
mn.sort_by("age", returns="parent_keys")                # → [key, ...]
mn.sort_by("meta.details.rank", returns="values")       # → [val, ...]

mn.select(["age", "name"])                              # → new ModDict
mn.select(["age", "meta.level"], returns="values")      # → [{"age":..}, ...]

mn.group_by("active")                                   # → {value: ModDict, ...}
mn.group_by("meta.level")

# Aliases
mn.alias(key, alias)                     # create alias (1 per key)
mn.aliases()                             # → {alias: original_key, ...}
del mn[alias]                            # removes alias and original

# Update from another collection
mn.update(other)                                      # bulk insert
mn.update(other, from_path, to_path, conflict="keep_right")

# Serialize
mn.serialize() / mn.deserialize(data)

# Index management (optional — auto-index handles most cases)
mn.create_index("field") / mn.drop_index("field") / mn.has_index("field")

Path syntax for update and filter

Token Meaning
* scan_key — match by outer key
? pass_key — wildcard one nesting level
mn.update(updates, "*", "*")                        # join by outer key
mn.update(prices, "*.meta.score", "*.meta.score")  # update only one deep field
mn.filter("orders.?.status").eq("shipped")          # status inside any order id

Custom type converters

Converters are applied at insert time — values are converted before storage, so they survive serialize(). MRO is walked: a converter for a base class also applies to subclasses.

md.register_converter(MyType, lambda obj: obj.to_dict())
mn["key"] = {"value": MyType(...)}   # → stored as dict, serializable

Built-in converters for shapely geometry (WKB) and geoalchemy2 (WKBElement) activate automatically when the library is installed.

Full type stubs with docstrings are in src/mod_dict.pyi — visible in IDE on hover and via help().

Installation

pip install mod_dict

Requires Python ≥ 3.11. Pre-built wheels for Windows / Linux / macOS. To build from source: pip wheel . (requires CMake ≥ 3.15 and a C++17 compiler).

Asyncio

ModDict is safe for concurrent reads in a single-threaded event loop. Rows are stored as PyObject* references — no copy between coroutines, no GC pressure during reads.

cache = md.ModDict()

async def handler(request):
    row = cache[request.user_id]
    return Response(row["meta"]["details"]["rank"])

async def startup():
    for key, row in data:
        cache[key] = row

See BENCHMARK.md for detailed numbers.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

mod_dict-0.2.0-cp313-cp313-win_amd64.whl (70.5 kB view details)

Uploaded CPython 3.13Windows x86-64

mod_dict-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (120.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

mod_dict-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (73.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mod_dict-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl (78.0 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

mod_dict-0.2.0-cp312-cp312-win_amd64.whl (70.5 kB view details)

Uploaded CPython 3.12Windows x86-64

mod_dict-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (120.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

mod_dict-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (73.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mod_dict-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl (78.0 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

mod_dict-0.2.0-cp311-cp311-win_amd64.whl (70.0 kB view details)

Uploaded CPython 3.11Windows x86-64

mod_dict-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (120.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

mod_dict-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (73.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

mod_dict-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl (77.1 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

File details

Details for the file mod_dict-0.2.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 70.5 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mod_dict-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c7653e93a852efed4505932484ec3b80d03167cfc8aa36535b1c1bbf7044a463
MD5 175261c0f4af5159c2235879f16538e7
BLAKE2b-256 57a856257e0615135872a844d913bf73c0aa86bc775baa97343dea3a7854617b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp313-cp313-win_amd64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aa062eac0f91612cbde711006d3f5e7005b312b3a4d36a21fab4357627390f9a
MD5 affd4eeee3b497386d258aa23c8ac3db
BLAKE2b-256 6e4757ca7e84278d3e29b41e8f40c372f5441a0a7024a5902b602e38577c6404

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7db648e7768d718eaf2abb54f977d932dc30d8b35de12b5d5ab3424291fdea38
MD5 dca665c9e565b5e6bbba37acc7c9988b
BLAKE2b-256 730f595b35bc1cfae658af518d0b94aac5567eaca8e115b061ea8047bf0c7694

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 470c23970f97557f35cf12827cbfec52f3408f4b0c388e66f2a98b9fac77b594
MD5 bc2b10226e8b16b5d7a99c5fcc4fd6b5
BLAKE2b-256 e7d7513bdfcfb3ee5deb050a4db3adfa5d3561f412e379944c9b34f8793f7408

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 70.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mod_dict-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9178d2bea7ce1390187a8051ee6890d5f984844d418591f3ce0265340a353a0b
MD5 8d44b255a7844c8650462d235f366095
BLAKE2b-256 c542732f4b19d8a4e638e8d4c3be57647cc38b4215ed42f9c35f86f6740ec4f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp312-cp312-win_amd64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 71455185c7048d8e5d18ceb2b89f3d955be5d70738a72b743bc01a4ca809bb24
MD5 f585f06922bf245976e66f3e0a104761
BLAKE2b-256 4db99e5fa6009f051f03059fa1708fbe580dd97a6e1ab68a84366717095c94a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b41b567958277d1e1de94577a0a9a0d57ad102881d43b5908ed328c71ad39e7
MD5 991dfa7c46267d6efc89cd91536c3583
BLAKE2b-256 f88c34d2ad0d2fc66bddfdb46b0e428e7f926613182ce5ad89fc5d6a8a00727f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 afb764b4f3c7458f8feabf628189e15d7e8387d12358acf564413747ab5520e8
MD5 b97040c27360eeb3a260e491d2aecd3a
BLAKE2b-256 a15c6440119d18ed9e1ec70c2fd5edcc4c81a22bb9cc83b78098451ef31024c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 70.0 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mod_dict-0.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d76bd414da996dd4695714a8d9abf165c2bc4007ff8d0aa9f069e055e60f376b
MD5 5d7a858c0beeb71ab05ab0b5445df9ed
BLAKE2b-256 a88358b4e4fb3384512f5b7d8906fdfbffe842ae23d533b393bd435d90837bf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp311-cp311-win_amd64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 540f9fb3499b67c10c7a947b664ec3a7b2bb13c60e6f168cee8081ff699c44db
MD5 53b37fe37bdda38fa3d246a9b15a47e2
BLAKE2b-256 2de4dea18bc16003a13bbdf0856c2b2bcd353b7c5cfabf28f43fbbe9d4a201d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9e1f235fcbf5cf074677448b148ecafbedc26a7ad7ded08f757c1359eba46444
MD5 aa46e86d9445ec8aa9ef86caf7857d50
BLAKE2b-256 b901cd72d706fbc4a2176cdd625759b3aa4e515d08fe743fa170b3303d226605

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mod_dict-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c11aefa854658422dae4a79b64708fd2cc12191a90a6c69266a62f2778b46e84
MD5 db5da05e72ad4ab40f796250878c39a7
BLAKE2b-256 03ff4a710c338ceb1fe3c88ae07613d59666dff5d4a51eaee6a294be7919d252

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl:

Publisher: build.yml on grey-pre-server/mod_dict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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