Registry / serialization / mo-dots

mo-dots

JSON →
library10.685.25166pypypi✓ verified 85d ago

mo-dots is a Python library that provides 'dot-access' functionality to dictionary-like data structures, similar to JavaScript objects. It boxes JSON-like data into a `Data` object, offering null-safe attribute access and a consistent interface. The library aims to be a more consistent and easier-to-use replacement for standard Python dictionaries, facilitating data transformation tasks. It is actively maintained, with the current version being 10.685.25166.

pip install mo-dots
INSTALL
IMPORT
SIG · MO-DOTS
M
mo-dots
serializationpythonv10.685.25166
Install
1.8s avg
Import
107ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v10.685.25166 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.110s · 18.1MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.8s · import 0.104s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Data
from mo_dots import Data
import mo_dots.Data
The primary class for dot-accessible data is 'Data' and it's intended for direct import.
to_data
from mo_dots import to_data
Used to convert existing dicts to Data objects.
from_data
from mo_dots import from_data
Essential for serializing Data objects to standard JSON.

This quickstart demonstrates how to create `Data` objects, access nested properties using dot notation and path strings, perform path assignments, and handle the specialized path accumulation with `+=`. It also highlights the crucial step for JSON serialization using `from_data`.

import json from mo_dots import Data, to_data, from_data # Create a Data object directly d = Data(a=1, b={'c': 2, 'd': [3, 4]}) print(f"Initial Data object: {d}") # Access with dot notation (null-safe) print(f"d.a: {d.a}") print(f"d.b.c: {d.b.c}") print(f"d.b.e: {d.b.e} (returns Null, not KeyError)") # Path assignment d['x.y.z'] = 99 print(f"After path assignment: {d}") # Path accumulation with += d.stats.count += 1 # Initializes to 0 then adds 1 d.stats.items += ['apple'] # Initializes to [] then appends d.stats.items += ['banana'] print(f"After path accumulation: {d}") # Convert an existing dictionary my_dict = {'user': {'name': 'Alice', 'id': 123}} data_from_dict = to_data(my_dict) print(f"Data from dict: {data_from_dict.user.name}") # Serialize to JSON (requires from_data) try: json.dumps(d) # This will raise TypeError without default=from_data except TypeError as e: print(f"Caught expected TypeError during direct JSON serialization: {e}") json_output = json.dumps(d, default=from_data) print(f"JSON output: {json_output}") assert json.loads(json_output) == {'a': 1, 'b': {'c': 2, 'd': [3, 4]}, 'x': {'y': {'z': 99}}, 'stats': {'count': 1, 'items': ['apple', 'banana']}}
Debug
Known issues
gotchaThe standard Python `json` library does not natively recognize `mo_dots.Data` objects for serialization, leading to `TypeError`.
fix
When serializing `Data` objects to JSON, use `json.dumps(data_object, default=from_data)` to convert `Data` structures into Python primitives.
affects: All versions
gotchaAccessing a non-existent key or attribute on a `Data` object using dot notation (`data.non_existent`) or bracket notation (`data['non.existent']`) will return `Null` instead of raising a `KeyError` or `AttributeError`.
fix
Always check for `Null` when accessing potentially non-existent paths, or use `data.get('path', default_value)` for explicit default handling if `Null` is not desired. Be aware that subsequent operations on a `Null` value will raise `AttributeError`.
affects: All versions
gotchaThe `+=` operator on a non-existent path (`data.new_path += value`) has special behavior: for numerical types, `new_path` is initialized to `0` before adding `value`; for list types, `new_path` is initialized to `[]` before appending `value`.
fix
Understand this automatic initialization. If you require different default behavior for new paths or explicit control over type initialization, initialize the path yourself (e.g., `data.new_path = 0` or `data.new_path = []`) before using `+=`.
affects: All versions
Errors
Common errors & fixes
TypeError: Object of type Data is not JSON serializable
`json.dumps()` was called directly on a `mo_dots.Data` object without a custom default serializer.
fix
Import `from_data` and pass it to the `default` argument of `json.dumps()`: `json.dumps(my_data, default=from_data)`.
KeyError: 'some_missing_key'
Attempting to access a non-existent key on a `mo_dots.Data` object using standard dictionary bracket notation, but `mo-dots` usually returns `Null` for missing keys accessed via dot notation or path strings, and handles some bracket accesses differently.
fix
For `Data` objects, non-existent keys accessed via dot notation (e.g., `data.some_missing_key`) or path strings (e.g., `data['a.b.c']`) will return `Null`. If you are explicitly trying to catch `KeyError`, ensure you are using standard dicts or adapt to `mo-dots`'s null-safe behavior. If you are getting this with `data[key]`, check if `key` actually exists or use `data.get(key)`.
AttributeError: 'Null' object has no attribute 'some_method'
A chained dot access or an operation was attempted on a `Null` value, which was returned by `mo-dots` for a non-existent path.
fix
Always check if a path exists or returns `Null` before performing further operations on it. For example, `if data.path and data.path.attribute: ...` or `value = data.get('path.attribute', None); if value is not None: ...`
Upgrade
Version history
10.685.25166latest on PyPI · released Jun 15, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
mo-dots — pip install mo-dots · libregistry