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.2-cp314-cp314-win_amd64.whl (264.8 kB view details)

Uploaded CPython 3.14Windows x86-64

mod_dict-0.2.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (106.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

mod_dict-0.2.2-cp314-cp314-macosx_11_0_arm64.whl (73.5 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

mod_dict-0.2.2-cp314-cp314-macosx_10_15_x86_64.whl (78.0 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

mod_dict-0.2.2-cp313-cp313-win_amd64.whl (256.8 kB view details)

Uploaded CPython 3.13Windows x86-64

mod_dict-0.2.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (106.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

mod_dict-0.2.2-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.2-cp312-cp312-win_amd64.whl (256.8 kB view details)

Uploaded CPython 3.12Windows x86-64

mod_dict-0.2.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (106.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

mod_dict-0.2.2-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.2-cp311-cp311-win_amd64.whl (256.3 kB view details)

Uploaded CPython 3.11Windows x86-64

mod_dict-0.2.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (105.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

mod_dict-0.2.2-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.2-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.2.2-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 264.8 kB
  • Tags: CPython 3.14, 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.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 7cb3d0541e9ecbbf55e96ed7bb00a1b073b7e0be3256474f2818c1d8a56ae157
MD5 445dc8530fd6e4b4a97f9525242568eb
BLAKE2b-256 d703ca6011dba1c00f84ddc2a871008773c0c704d8d950ed26824efb8cc7e67f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-cp314-cp314-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.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 99ea0d640306e158c098de30b2062d1b4f8a297c323c2ff12321c8d57c40cf52
MD5 e9609aced3e4b8da718c8a012c481ec8
BLAKE2b-256 f49ae6bc853634ab1e23715de4ba0948e08690194dea126d8d6fd51d0aa178cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_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.2-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c6415c6f3543bce5f08948a13c3d013e2685eb11e1e3a15723dbf20d3d3d90f7
MD5 027ef2c2e16e8afd7cd92cda2aaad8ab
BLAKE2b-256 9c74f4bbbf4ac6c96f9efa468ce9ddb85b6a4156826644125e111d0419cd88f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-cp314-cp314-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.2-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 c8245736a9b98155adb86fd6c73982863d4894573d350899b8053d02f399c38b
MD5 6f27693089b7654be43dd6c60a03b68d
BLAKE2b-256 c71b10a50a7ea644af4dfc984cbfb4d42b1481033e5b75ea776d6f306c27b769

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-cp314-cp314-macosx_10_15_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.2-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.2.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 256.8 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.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a4278789573d80771292a3a9e6c647d0f72af73b682e95b99626b7f36738c8be
MD5 1b7d148f1ba15c909e7147ed3f2f5100
BLAKE2b-256 515d4661cd62923ac441de58b46e8a3ccf1b3154fe19e8d29596653f27a2b17b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 70c5d03350f3007d67b7242aab1c37559447ad892b28971568c6f4c2f25beb07
MD5 c1c9aadc639e52d58f9a62de63677275
BLAKE2b-256 cbef32ca9f075b684cc4220842ac099fe9a0278ed5958961721bedc40f8b4a8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_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.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bdc93de197d63ed33b8bc8d312d226a4bdb2cef677cd30f25a295cd853a2be87
MD5 b42dc9b085bb7477bbe7d59a7ad0720e
BLAKE2b-256 9be6e21d915f6a27b1df04e25752a4d3670c790fbaceeb8c9b07870dd2c53c16

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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.2-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 6e19a1c1291848a08f0044fb66e6757ef49df23868d956d8d210b3617ecf8631
MD5 83cc88918f47f55119ff0f4882e6d3bb
BLAKE2b-256 5ef38ed6d18e15b671ac94efaac708ee21ae96439fb36b82bb090152cb1ff134

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.2.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 256.8 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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e80fd55b65166c37aa9dd0d4632ced93a06fae99c9a12f8aedfff8bfc70d4f49
MD5 7b1974531daacc685bb5ed4e2ea3a24e
BLAKE2b-256 19e12907aed53f4853f3f2fef0bbfebaf9797382fd6dc1dc2777b65751d63747

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 670c5728b2de3a8a45daf53c15c3504b97ca9f2bfce1b1d4ca40b73d16ef0360
MD5 569561ca3dc051b988745448d4b47769
BLAKE2b-256 c1598d786c7c20599176d842de560e3ce6bdee638b46ec64e29e0926815c1ae4

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_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.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 27af5a2d400d20ff073b99a49d4b326815fae6fa14bb03571ef164e4afb0a07d
MD5 1188aa456569efd6740f09ffbbf443c8
BLAKE2b-256 f16528b7c3099974824286bb617898b88a877415057bfaaf069bd21e33a7fc5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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.2-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 036779692c547e5ce98944e1518f7c4a496fafe85732af7fa13622825c53f770
MD5 1318536d77b4e62dbc1405fda9e7932e
BLAKE2b-256 67a9eedc5c53049e87ba45b8d0614f3d8bf8ad6f5d2ad0c564a6fcc7c2c83db4

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.2.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 256.3 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 06b10ddb16d096e33df064f62f298dc96b4d8160832aef19f5d8c7c6aaef9063
MD5 fcfff6ef1b4f3b4f04cab6333a91a937
BLAKE2b-256 b5c3b5f2f7e67f8b8775a56abc2403664167d522d0ee8c8b61956785379da9e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 84bd400b860dfc26fe0f704377f2a4c87ddf2841198716a80d718f85d29ceb76
MD5 65436bc0c5cbeddae1351002b6320480
BLAKE2b-256 6d5803200e260930a6706c60a222e24d6b0419b7a184f3182fb4e960ae3de079

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_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.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c529d982541f17c1d0e80a642606ab9ee12a9db1fc70adf15acadb3b90ceaa53
MD5 3309847544b950c8f456b4105cde539d
BLAKE2b-256 16c549eb35d1b531db738126b0d862886cced8be8820956a847f245ef6a33079

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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.2-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mod_dict-0.2.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e8ddc51a0e8ef7d54414fe59ec3dcb069d38c4d0c46d1884d6b5cb2ddbb05c06
MD5 ae0877385dfff4cd5d993ae8e3ea9d69
BLAKE2b-256 b236985c33caa1150cfd7a3e0d0d997b57432a18be47f0fbf2e30fdec9bcb649

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.2.2-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