Skip to main content

Convert dictionaries into objects with attribute-style access

Project description

๐Ÿ“ฆ Dict2Obj - Dictionary to Object Converter

Python Version License

๐Ÿš€ 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()
โœ… Supports non-string keys like integers and tuples


๐Ÿ›  Installation

pip install 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

Dict2Obj supports access to nested keys using attribute-style access and special handling of non-string keys.

from dict2objects import Dict2Obj

data = {
    "user": {
        "name": "Alice",
        "age": 30,
        (1, 2): "coordinates"
    },
    100: "century",
    "details": {
        "nested": {
            "value": 42
        }
    }
}

obj = Dict2Obj(data)

print(obj.user.name)          # Alice
print(obj.details.nested.value)  # 42
print(obj.to_dict()[(1, 2)])     # coordinates
print(obj.to_dict()[100])        # century

๐Ÿ” Access Table

Description Access Pattern Output
โœ… Top-level key (string) obj.user.name "Alice"
โ“ Nested key obj.details.nested.value 42
๐Ÿงฉ Non-string tuple key (original) obj.to_dict()[(1, 2)] "coordinates"
๐Ÿ“ฆ Non-string integer key (original) obj.to_dict()[100] "century"
๐Ÿ“œ Missing key obj.unknown None

๐Ÿ“ 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:
    pip install -e .
    
  3. Run tests:
    python -m unittest discover tests
    

๐Ÿ“„ 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.2.tar.gz (3.6 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.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dict2objects-0.1.2.tar.gz
  • Upload date:
  • Size: 3.6 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.2.tar.gz
Algorithm Hash digest
SHA256 6eaa4e267785136c3cfef76852682169a83abeb795b5956f3216c09872c6c9f1
MD5 bda88b1aaaacce3b67f2f91ed71b6835
BLAKE2b-256 31163c7967f74489a4bb4c832b04352cfad72c73ec7ab6e9336d73ae76e7dc74

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dict2objects-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a95946749951fbf5c99b07bfddd6b4f76a44f043d10fa8061df4ded17ea70f01
MD5 16939f2896a41ce0b01d365cb48f834e
BLAKE2b-256 f2cb1c128b0e416b109315f7c846de95b5e2c347ba911b09a607785a266c7f70

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