Skip to content

JanardhanSingh98/dict2obj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📦 Dict2Obj - Dictionary to Object Converter

Python Version License PyPI version

🚀 Convert Python dictionaries into objects with attribute-style access.
🔄 Supports nested dictionaries.
🔍 Easily flatten dictionaries into dot notation.


🐜 Features

✅ 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()


🛠 Installation

pip install dict2objects

Or using Poetry:

poetry add dict2objects

🚀 Usage

Basic Example

from 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'}}

Flatten Dictionary

print(obj.to_dot_dict())  
# {'name': 'Alice', 'age': 30, 'address.city': 'New York', 'address.zip': '10001'}

Handling Missing Keys

print(obj.salary)  # None (key does not exist)
print(obj.address.country)  # None (nested non-existent key)

📌 Access Patterns

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'}

💂️ Project Structure

dict2obj/
├── dict2obj/
│   ├── __init__.py
│   ├── converter.py
├── tests/
│   ├── test_converter.py
├── poetry.lock
├── pyproject.toml
├── README.md
├── LICENSE
├── .gitignore

🛠 Development & Contribution

  1. Clone the repository:
    git clone https://github.com/yourusername/dict2obj.git
    cd dict2obj
  2. Install dependencies:
    poetry install
  3. Run tests:
    poetry run pytest

🐜 License

This project is licensed under the MIT License. See the LICENSE file for details.


🌟 Like this project? Give it a star ⭐ on GitHub!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages