Skip to main content

Convert dictionaries into objects with attribute-style access

Project description

๐Ÿ“ฆ 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
โ”œโ”€โ”€ setup.py
โ”œโ”€โ”€ 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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dict2objects-0.1.4.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dict2objects-0.1.4-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file dict2objects-0.1.4.tar.gz.

File metadata

  • Download URL: dict2objects-0.1.4.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.17 Linux/6.8.0-1021-azure

File hashes

Hashes for dict2objects-0.1.4.tar.gz
Algorithm Hash digest
SHA256 47ee27fa40992ee696f31be6413d5f76fec0e99209cd5fde74ea5a7d53ce2121
MD5 6742e9b3ffe784f4b0745bf85eec4f0e
BLAKE2b-256 4604b6d9dbf88a737b377c0faf4ce7ae149a5be0a1dd508f7e844ea092c980a0

See more details on using hashes here.

File details

Details for the file dict2objects-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: dict2objects-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.17 Linux/6.8.0-1021-azure

File hashes

Hashes for dict2objects-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5d3c5dad2fde5f27265394f93e985d99dda47e2147b3c021e799605fcdcc5abd
MD5 ca0a61a1af61e48972f46769b349f403
BLAKE2b-256 3a938a9c5778ba0e03c44cf42aa64fe817f33532fc63733f5ec68c102dcc34c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page