# AttrDictX
[![PyPI](https://img.shields.io/pypi/v/attrdictx.svg)](https://pypi.org/project/attrdictx/)
[![License](https://img.shields.io/pypi/l/attrdictx.svg)](https://github.com/kyo-takano/AttrDictX/blob/main/LICENSE)
## Overview
AttrDictX is a lightweight Python package that allows seamless access to dictionary values as if they were class attributes. This package offers an elegant and dependency-free solution, enhancing code legibility and ease of use.
Unlike traditional dictionary access (`random_dict["key"]`), AttrDictX simplifies the process (`random_dict.key`), saving three characters per access. Additionally, it supports nested dictionaries and is free of any dependency-related errors, addressing limitations present in other packages like `attrdict`.
## Installation
To install `attrdictx`, you can use `pip`:
```bash
pip install attrdictx
```
Alternatively, you may just copy & paste the following source code to use this package:
```python
class AttrDictX(dict):
def __init__(self, *args, **kwargs):
super(AttrDictX, self).__init__(*args, **kwargs)
for key, value in self.items():
if isinstance(value, dict):
self[key] = AttrDictX(value)
def __getattr__(self, name):
if name in self:
return self[name]
raise AttributeError(f"'AttrDictX' object has no attribute '{name}'")
def __setattr__(self, name, value):
self[name] = value
```
## Usage
After importing the `AttrDictX` class (when `import AttrDict` also works), you can create an instance from your dictionary and access values like class attributes.
```python
from attrdictx import AttrDictX
# Example: configuration for a random web app
config = {
'database': {
'host': 'localhost',
'port': 5432,
'username': 'my_user',
'password': 'my_password'
},
'api_keys': {
'weather_api': 'abc123def456',
'news_api': 'xyz789'
},
'cache_enabled': True
}
# Create an AttrDictX instance from the configuration dictionary
app_config = AttrDictX(config)
# Accessing configuration settings with recursive attributes
print(app_config.cache_enabled)
# => True
print(app_config.api_keys.weather_api)
# => abc123def456
```
## Contributions
We welcome contributions from the community to improve and expand AttrDictX. If you encounter any issues, have suggestions, or want to contribute code, feel free to open an issue or submit a pull request on our GitHub repository: [https://github.com/kyo-takano/AttrDictX](https://github.com/kyo-takano/AttrDictX)
## License
AttrDictX is released under the [MIT License](https://github.com/kyo-takano/AttrDictX/blob/main/LICENSE).
Raw data
{
"_id": null,
"home_page": "https://github.com/kyo-takano/attrdictx",
"name": "attrdictx",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "",
"author": "Kyo Takano",
"author_email": "kyo.takano@mentalese.co",
"download_url": "https://files.pythonhosted.org/packages/ab/a7/ecb6301108c4f57d8153233ef57cd1421d7f2708640f41f50e04c4f7a965/attrdictx-0.1.0.tar.gz",
"platform": null,
"description": "# AttrDictX\n\n[![PyPI](https://img.shields.io/pypi/v/attrdictx.svg)](https://pypi.org/project/attrdictx/)\n[![License](https://img.shields.io/pypi/l/attrdictx.svg)](https://github.com/kyo-takano/AttrDictX/blob/main/LICENSE)\n\n## Overview\n\nAttrDictX is a lightweight Python package that allows seamless access to dictionary values as if they were class attributes. This package offers an elegant and dependency-free solution, enhancing code legibility and ease of use.\n\nUnlike traditional dictionary access (`random_dict[\"key\"]`), AttrDictX simplifies the process (`random_dict.key`), saving three characters per access. Additionally, it supports nested dictionaries and is free of any dependency-related errors, addressing limitations present in other packages like `attrdict`.\n\n## Installation\n\nTo install `attrdictx`, you can use `pip`:\n\n```bash\npip install attrdictx\n```\n\nAlternatively, you may just copy & paste the following source code to use this package:\n\n```python\nclass AttrDictX(dict):\n def __init__(self, *args, **kwargs):\n super(AttrDictX, self).__init__(*args, **kwargs)\n for key, value in self.items():\n if isinstance(value, dict):\n self[key] = AttrDictX(value)\n\n def __getattr__(self, name):\n if name in self:\n return self[name]\n raise AttributeError(f\"'AttrDictX' object has no attribute '{name}'\")\n\n def __setattr__(self, name, value):\n self[name] = value\n```\n\n## Usage\n\nAfter importing the `AttrDictX` class (when `import AttrDict` also works), you can create an instance from your dictionary and access values like class attributes.\n\n```python\nfrom attrdictx import AttrDictX\n\n# Example: configuration for a random web app\nconfig = {\n 'database': {\n 'host': 'localhost',\n 'port': 5432,\n 'username': 'my_user',\n 'password': 'my_password'\n },\n 'api_keys': {\n 'weather_api': 'abc123def456',\n 'news_api': 'xyz789'\n },\n 'cache_enabled': True\n}\n\n# Create an AttrDictX instance from the configuration dictionary\napp_config = AttrDictX(config)\n\n# Accessing configuration settings with recursive attributes\nprint(app_config.cache_enabled)\n# => True\n\nprint(app_config.api_keys.weather_api)\n# => abc123def456\n```\n\n## Contributions\n\nWe welcome contributions from the community to improve and expand AttrDictX. If you encounter any issues, have suggestions, or want to contribute code, feel free to open an issue or submit a pull request on our GitHub repository: [https://github.com/kyo-takano/AttrDictX](https://github.com/kyo-takano/AttrDictX)\n\n## License\n\nAttrDictX is released under the [MIT License](https://github.com/kyo-takano/AttrDictX/blob/main/LICENSE).\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "An extended AttrDict class",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/kyo-takano/attrdictx"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cbeb081edbbc67f0509294ab47c613586b114a58a20cb3e3af27ed475bc80a11",
"md5": "4ea7736c840fb46b0e00cd80ec4ba939",
"sha256": "6adb9f2335c7ac5b8c92b875844329bd44601088e1d19c0e629ad4925ea7aa27"
},
"downloads": -1,
"filename": "attrdictx-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4ea7736c840fb46b0e00cd80ec4ba939",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 3794,
"upload_time": "2023-07-30T08:23:48",
"upload_time_iso_8601": "2023-07-30T08:23:48.343879Z",
"url": "https://files.pythonhosted.org/packages/cb/eb/081edbbc67f0509294ab47c613586b114a58a20cb3e3af27ed475bc80a11/attrdictx-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "aba7ecb6301108c4f57d8153233ef57cd1421d7f2708640f41f50e04c4f7a965",
"md5": "f10dd277b5f0ec86c2ee057ed5bf2894",
"sha256": "43c2c92b0c15a2c2b33abfc99402f631d075c3190f99fb9dd2443c830521fca8"
},
"downloads": -1,
"filename": "attrdictx-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "f10dd277b5f0ec86c2ee057ed5bf2894",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 2743,
"upload_time": "2023-07-30T08:23:50",
"upload_time_iso_8601": "2023-07-30T08:23:50.288637Z",
"url": "https://files.pythonhosted.org/packages/ab/a7/ecb6301108c4f57d8153233ef57cd1421d7f2708640f41f50e04c4f7a965/attrdictx-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-30 08:23:50",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kyo-takano",
"github_project": "attrdictx",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "attrdictx"
}