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-dotsVerified import paths — ran on the pinned version, not inferred.
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`.
When serializing `Data` objects to JSON, use `json.dumps(data_object, default=from_data)` to convert `Data` structures into Python primitives.
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`.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 `+=`.
Import `from_data` and pass it to the `default` argument of `json.dumps()`: `json.dumps(my_data, default=from_data)`.
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)`.
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: ...`No dependency data recorded yet.