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

Uploaded CPython 3.14Windows x86-64

mod_dict-0.3.0-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.3.0-cp314-cp314-macosx_11_0_arm64.whl (70.9 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

mod_dict-0.3.0-cp314-cp314-macosx_10_15_x86_64.whl (77.0 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

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

Uploaded CPython 3.13Windows x86-64

mod_dict-0.3.0-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.3.0-cp313-cp313-macosx_11_0_arm64.whl (70.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mod_dict-0.3.0-cp313-cp313-macosx_10_13_x86_64.whl (76.9 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

mod_dict-0.3.0-cp312-cp312-win_amd64.whl (256.8 kB view details)

Uploaded CPython 3.12Windows x86-64

mod_dict-0.3.0-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.3.0-cp312-cp312-macosx_11_0_arm64.whl (70.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mod_dict-0.3.0-cp312-cp312-macosx_10_13_x86_64.whl (76.9 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

mod_dict-0.3.0-cp311-cp311-win_amd64.whl (256.3 kB view details)

Uploaded CPython 3.11Windows x86-64

mod_dict-0.3.0-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.3.0-cp311-cp311-macosx_11_0_arm64.whl (70.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

mod_dict-0.3.0-cp311-cp311-macosx_10_9_x86_64.whl (76.1 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

File details

Details for the file mod_dict-0.3.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.3.0-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.3.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e771a828a3e8e70d465244206c134a6ee4a854b647eda5f5fe4d587472ac6b94
MD5 936804387df39e53ed18cbe9fc856771
BLAKE2b-256 badb5cddad1f6bea6e6913adba2fde642503281e0f9db234f0f582f02b686e4c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6b7a0e997e082e73c6967d551a9ca4c3277123638ca9d2b98fe84092f4fe031e
MD5 c3de68c80ee092ddaad957fdb57116f3
BLAKE2b-256 f479e6ca32b8300d0d78afaeadebad5507d6ee7386d434f67f6b477a45e5cac1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.3.0-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.3.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4ee0c6e4ff10e3274eebf4e729a34a2e19b7e44f2d57e9ad00ad77061591fd8f
MD5 3a50072b81b0004587803d94868cec7c
BLAKE2b-256 bdefe61cc31ab4fcc7e07e48fb5350d0b738539772a81c6414e82e3b605a4a83

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 eed9f483f400b350a330c373730497c9b96be454acec146e8c9cb24ca1806626
MD5 bc65d93c34f2aec09730ca731d876a30
BLAKE2b-256 1813b98f11976fdf061f66620b58fd518b7ca03bd876e2648d58adc2c1e8b479

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.3.0-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.3.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: mod_dict-0.3.0-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.3.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 14f5a0c22e08105b7861a996957be0caefc86b1087ba674366624c7014e34150
MD5 d7284ef83d51f89b3326b111aa06f1c1
BLAKE2b-256 bc63940650af5551246ad0c053882d5cebc13289d1242060cb474941bf951c53

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 40debe2445a38be94adba64007617b7596173c3f92d1fc25273e9c837c81e30e
MD5 ff17ba5080bdae7d99f1d3e9be4d54dc
BLAKE2b-256 51e0714ea107a93df82d81aacd31f25a21dafe706b82d097dcc84326d9ace321

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.3.0-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.3.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c7752ecaa0608a93c0a077ba740c8021972205903cee6deb48bd69df4d3dfc56
MD5 47009dcd9ece1f1f502cfb4682e2e4f2
BLAKE2b-256 f1e64d728ddbcddaedf42eb2dc97c1c5139b73fff5b4d8e744c06148d4a6326c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 77868a77db481fb3a0208377860e06a50103af16ab0bd9376394fe05db1aa2b4
MD5 f9f6686567743c4b499c0537649b01d8
BLAKE2b-256 2d53c86017b953bc702b638dd11fb24d0416ecde350aba001e91d70b23a669d7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mod_dict-0.3.0-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.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4c6ce7112e3f3038ec952b89e8f7109690c5a41ae87847c63f933047a08f7284
MD5 1a03302635ba17fb0f51d61c4a18ee52
BLAKE2b-256 3bf49d96769ba8d40028208c830d6e306b669c20a91922920a2bbbd0823ef09a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e0418359b0ee5db2dcd00e93dd0fa78f82e0ee4a4d4475cbf173c136c56f0ca7
MD5 50b9dc0c42cfc3ca64d4c0e1a59891e1
BLAKE2b-256 8bee7cd3f353823cc0d260ef2478300be5b9e200d5fe99b4b7e38d7f37a2bc80

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.3.0-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.3.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1b6758adbf8b62e2ebe118cda9dfc0c87fef79ab56fcda4149827bf06b213742
MD5 8da678e3e3aa7973c3d6b48e203244de
BLAKE2b-256 8930a259d4babc7d780b8fb20267196cf0ddc34e54eab55be943b26ea2f69d52

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 356897f316a3d00a6679815e23168b1bfa1820f2482f7d609d685280f61ac5ba
MD5 a0158c5dd8fab79c210849076251d76b
BLAKE2b-256 2fcef8a69116fdfef90de271b349878e78f66fc8b94a9926eb027d9699364f6e

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mod_dict-0.3.0-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.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 666461a839d74622b4453e585009566490c5438664ecf205ae3059a249d0f350
MD5 d0539c1834d127617c5dc905818b92b0
BLAKE2b-256 3222c73ee189b1cf8dce198c3e286b78bac4a37c47dccbc6ec4c178266a21bd1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 34931c4abf266c4b4baec16fcac6464c59d5688edf259fdc554d6ab21b06bc20
MD5 9a3991810d2d1a19440d8a854f23142d
BLAKE2b-256 2510cf8aa9971b91a75e4ed14dce82b14e5c2525d3fc2635d339c023f77b4e45

See more details on using hashes here.

Provenance

The following attestation bundles were made for mod_dict-0.3.0-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.3.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f809c197588553c22bbc352698147d64860ad378d7c01f433a3a243a99637f70
MD5 91b08728604bc9c4f06cb84279c0157c
BLAKE2b-256 b09ea8ae4dcfcc2bf7d9fe573c683be9a0537c708d9119f7ba7434a2870d2d28

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mod_dict-0.3.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 62a874813a58f8f1893753d09b94b2115f791b9a003cbc3ff37364528244bd80
MD5 08dd15d932e9fe82dbc2102120e1041a
BLAKE2b-256 114c7dd784c8b1a07b6635f2fe13161f436ac73b3e1ac8ab0e0ac273c407c7cd

See more details on using hashes here.

Provenance

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