Skip to main content

An extended AttrDict class

Project description

AttrDictX

PyPI 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:

pip install attrdictx

Alternatively, you may just copy & paste the following source code to use this package:

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.

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

License

AttrDictX is released under the MIT License.

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

attrdictx-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

attrdictx-0.1.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file attrdictx-0.1.0.tar.gz.

File metadata

  • Download URL: attrdictx-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.2

File hashes

Hashes for attrdictx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 43c2c92b0c15a2c2b33abfc99402f631d075c3190f99fb9dd2443c830521fca8
MD5 f10dd277b5f0ec86c2ee057ed5bf2894
BLAKE2b-256 aba7ecb6301108c4f57d8153233ef57cd1421d7f2708640f41f50e04c4f7a965

See more details on using hashes here.

File details

Details for the file attrdictx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: attrdictx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.2

File hashes

Hashes for attrdictx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6adb9f2335c7ac5b8c92b875844329bd44601088e1d19c0e629ad4925ea7aa27
MD5 4ea7736c840fb46b0e00cd80ec4ba939
BLAKE2b-256 cbeb081edbbc67f0509294ab47c613586b114a58a20cb3e3af27ed475bc80a11

See more details on using hashes here.

Supported by

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