🚀 Convert Python dictionaries into objects with attribute-style access.
🔄 Supports nested dictionaries.
🔍 Easily flatten dictionaries into dot notation.
✅ Convert dictionary keys to object attributes
✅ Return None for missing attributes instead of raising errors
✅ Convert back to dictionary with to_dict()
✅ Flatten to dot notation with to_dot_dict()
pip install dict2objectsOr using Poetry:
poetry add dict2objectsfrom dict2objects import Dict2Obj
data = {"name": "Alice", "age": 30, "address": {"city": "New York", "zip": "10001"}}
obj = Dict2Obj(data)
print(obj.name) # Alice
print(obj.address.city) # New York
print(obj.to_dict())
# {'name': 'Alice', 'age': 30, 'address': {'city': 'New York', 'zip': '10001'}}print(obj.to_dot_dict())
# {'name': 'Alice', 'age': 30, 'address.city': 'New York', 'address.zip': '10001'}print(obj.salary) # None (key does not exist)
print(obj.address.country) # None (nested non-existent key)| Access Type | Example | Output |
|---|---|---|
| Attribute-style | obj.name |
'Alice' |
| Nested attribute | obj.address.city |
'New York' |
| Missing attribute | obj.not_exist |
None |
| Flatten dot notation | obj.to_dot_dict() |
{...} |
obj = Dict2Obj({"user": {"details": {"name": "Janardhan"}}})
print(obj.user.details.name) # "Janardhan"
print(obj.user.age) # None
print(obj.to_dot_dict())
# {'user.details.name': 'Janardhan'}dict2obj/
├── dict2obj/
│ ├── __init__.py
│ ├── converter.py
├── tests/
│ ├── test_converter.py
├── poetry.lock
├── pyproject.toml
├── README.md
├── LICENSE
├── .gitignore
- Clone the repository:
git clone https://github.com/yourusername/dict2obj.git cd dict2obj - Install dependencies:
poetry install
- Run tests:
poetry run pytest
This project is licensed under the MIT License. See the LICENSE file for details.