Registry / serialization / dotty-dict

dotty-dict

JSON →
library1.3.1pypypi✓ verified 25d ago

Dotty Dict is a Python library that provides a dictionary-like object for quick access to deeply nested keys using dot notation. It wraps standard Python dictionaries and supports various dictionary operations including creation, reading, updating, and deleting nested keys. The current stable version is 1.3.1, released in July 2022, suggesting a moderate release cadence.

pip install dotty-dict
INSTALL
IMPORT
SIG · DOTTY-DICT
D
dotty-dict
serializationpythonv1.3.1
Install
1.6s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.1 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

dotty
from dotty_dict import dotty
Recommended factory function for creating Dotty instances.
Dotty
from dotty_dict import Dotty
The class constructor, used for custom separators or escape characters.

Demonstrates creating a new Dotty instance, setting deeply nested values, accessing them, and wrapping an existing dictionary to modify its contents using dot notation and list indexing.

from dotty_dict import dotty # Create a new dotty dict data = dotty() data['user.address.street'] = 'Main St' data['user.address.city'] = 'Anytown' data['items.0.name'] = 'Laptop' data['items.0.price'] = 1200 print(f"Street: {data['user.address.street']}") print(f"First item name: {data['items.0.name']}") # Wrap an existing dictionary existing_dict = {'product': {'details': {'id': 'P101', 'stock': 50}}} dot_wrapper = dotty(existing_dict) print(f"Product ID: {dot_wrapper['product.details.id']}") dot_wrapper['product.details.stock'] = 45 print(f"Updated existing dict: {existing_dict}") # The underlying dict is modified
Debug
Known issues
gotchaInteger keys in string paths are automatically treated as list indices. For example, `dot['items.0.name']` accesses the 'name' key of the first item in the 'items' list. If you intend to use an integer as a dictionary key, it will be interpreted as a list index.
fix
Ensure your dictionary keys are strings if they should not be interpreted as list indices, or structure your data accordingly. There is no direct mechanism to force an integer in a dot-separated string path to be a dictionary key if it's numerically parsed.
affects: All versions
gotchaDictionary keys cannot contain dots (`.`) by default. If a key genuinely contains a dot, it will be interpreted as a path separator, leading to unexpected nested dictionary structures.
fix
Initialize `Dotty` with a custom separator (e.g., `Dotty(separator='_')`) or use the escape character (backslash `\`) to explicitly include a dot in a key (e.g., `dot['my\.key.with\.dots']`).
affects: All versions
gotchaDotty Dict does not support attribute-style access (e.g., `data.user.address.street`). All access to nested keys must be done using dictionary-style item access with string paths (e.g., `data['user.address.street']`).
fix
Always use `['dot.separated.path']` syntax for accessing values, even for deeply nested structures.
affects: All versions
gotchaBoolean type keys are only partially supported. Accessing boolean keys directly (e.g., `dot[True]`) works, but using them within a dot-notation string path might lead to unexpected behavior or errors.
fix
Avoid using boolean values within dot-notation string paths. If you need to store boolean data, consider using string representations (e.g., 'true', 'false') if they are part of a dot-separated path.
affects: All versions
gotchaIn very rare edge cases, when a nested dictionary contains two keys of different types but with the same value (e.g., an integer key and a string key both representing '1'), `dotty-dict` might return a dict or list under a 'random' key with the passed value, leading to inconsistent behavior.
fix
Ensure consistent key typing within your nested dictionaries, especially avoiding ambiguous cases where different types could represent the same value.
affects: All versions
Errors
Common errors & fixes
AttributeError: 'dict' object has no attribute 'some_key'
This error occurs when attempting to access keys using dot notation (e.g., `my_dict.some_key`) on a standard Python dictionary, which does not natively support attribute-style access. `dotty-dict` is designed to provide this functionality, so this error typically means the dictionary has not been wrapped by `dotty-dict` or the developer is trying to use dot notation on the original unwrapped dictionary.
fix
Wrap the dictionary with the `dotty` factory function or `Dotty` class from `dotty_dict` before attempting dot notation access. 
```python
from dotty_dict import dotty
my_regular_dict = {'parent': {'child_key': 'value'}}
my_dotty_dict = dotty(my_regular_dict)
print(my_dotty_dict.parent.child_key)
```
KeyError: 'some.nested.key'
This error is raised by `dotty-dict` when a requested nested key path does not exist within the dictionary. Even though `dotty-dict` allows accessing nested keys via dot notation strings (e.g., 'parent.child'), it will still raise a `KeyError` if any part of that path is missing.
fix
Before accessing a potentially non-existent nested key, use the `.get()` method with a default value, or check for the key's existence using the `in` operator. 
```python
from dotty_dict import dotty
d = dotty({'a': {'b': 1}})
# Using .get() to avoid KeyError
value = d.get('a.non_existent_key', 'default_value')
print(value) # Output: default_value

# Checking for existence
if 'a.b' in d:
    print(d['a.b']) # Output: 1
if 'a.c' not in d:
    print("'a.c' does not exist")
```
ModuleNotFoundError: No module named 'dotty_dict'
This error indicates that the `dotty-dict` library has not been installed in the Python environment, or there's an issue with the Python environment's path configuration preventing the interpreter from finding the installed package.
fix
Install the library using pip: 
```bash
pip install dotty-dict
```
If already installed, ensure you are running the script in the correct Python environment where `dotty-dict` is installed.
from dotty_dict import Dotty
While `Dotty` is the class that powers `dotty-dict`, the library's primary documentation and examples promote importing and using the `dotty()` factory function (lowercase 'dotty') for creating instances. Importing `Dotty` directly is not strictly an error, but it's a less common pattern and might lead to confusion if the user expects the factory function's behavior (e.g., default separator handling).
fix
It is generally recommended to import the `dotty` factory function for convenience and consistency with library examples. 
```python
from dotty_dict import dotty
my_dotty_dict = dotty({'key': 'value'})
```
If you intentionally need to instantiate the `Dotty` class directly, remember to pass the dictionary, separator, and escape character explicitly: 
```python
from dotty_dict.dotty_dict import Dotty # Or just from dotty_dict import Dotty if the internal structure allows
my_dotty_dict = Dotty({'key': 'value'}, separator='.', esc_char='\\')
```
Upgrade
Version history
1.3.1latest on PyPI · released Jul 9, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
dotty-dict — pip install dotty-dict · libregistry