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-dictVerified import paths — ran on the pinned version, not inferred.
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.
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.
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']`).
Always use `['dot.separated.path']` syntax for accessing values, even for deeply nested structures.
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.
Ensure consistent key typing within your nested dictionaries, especially avoiding ambiguous cases where different types could represent the same value.
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)
```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")
```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.
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='\\')
```No dependency data recorded yet.