Convert dictionaries into objects with attribute-style access
Project description
๐ฆ Dict2Obj - Dictionary to Object Converter
๐ 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
- Clone the repository:
git clone https://github.com/yourusername/dict2obj.git cd dict2obj
- Install dependencies:
pip install -e .
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dict2objects-0.1.3.tar.gz.
File metadata
- Download URL: dict2objects-0.1.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d4fb87aa0571b3bb2e82fb8a389cd625f7abe6432f44d2661300e4671213f3c
|
|
| MD5 |
6b105da56a6029b4715dfa858b3edbf9
|
|
| BLAKE2b-256 |
ff035dee198bf89bbea90a2860a2d470a89a695f5dc2435bd5a4e7909030cfd5
|
File details
Details for the file dict2objects-0.1.3-py3-none-any.whl.
File metadata
- Download URL: dict2objects-0.1.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0c289b7298ad3619e8fc27fa228f87dcde9ec62ae6cc7190fe99872b3cfa87c
|
|
| MD5 |
de81d0898383659147e79786945d6e04
|
|
| BLAKE2b-256 |
74e5afd045a5e303604dc0e2b74871eb865e057de82ae23263346baf7f3467c2
|