A dict implementation with support for easy and clean access of its values through attributes
Project description
attridict
A Python package implementing atrribute dictionary.
This provides an easier and cleaner way to access dict values using their keys as attributes. It is typically a dict child, maintaining all the dict functionalities, but including some extra features.
Installation
To install the package from PyPI, use:
pip install attridict
Usage
Creating an attridict object
>>> import attridict
>>> att = attridict()
>>> att
{}
>>> att.foo = "bar"
>>> att.foo
'bar'
>>> att
{'foo': 'bar'}
A dict can be converted into an attridict object by passing it as an argument
>>> import attridict
>>> data = {'red': 'hot', 'blue': 'cold'}
>>> colors = attridict(data)
>>> colors
{'red': 'hot', 'blue': 'cold'}
>>> colors.red
'hot'
Modifying attridict object
>>> import attridict
>>> data = {'red': 'hot', 'blue': 'cold'}
>>> colors = attridict(data)
>>> colors
{'red': 'hot', 'blue': 'cold'}
>>> colors.blue
'cold'
>>> colors.blue = "sky"
>>> colors.red = "rose"
>>> colors.blue
'sky'
>>> colors.red
'rose'
>>> colors
{'red': 'rose', 'blue': 'sky'}
>>> colors.green = "grass"
>>> colors
{'red': 'rose', 'blue': 'sky', 'green': 'grass'}
Typical dict operations work on attridict objects
>>> import attridict
>>> data = {'red': 'rose', 'blue': 'sky'}
>>> colors = attridict(data)
>>> colors
{'red': 'rose', 'blue': 'sky'}
>>> colors.red
'rose'
>>> colors["red"]
'rose'
>>> colors["red"] = "tomato"
>>> colors["red"]
'tomato'
>>> colors.red
'tomato'
Nested attribute access
>>> import attridict
>>> data = {'foo': {}}
>>> att = attridict(data)
>>> att
{'foo': {}}
>>> att.foo.bar = 'baz'
>>> att.foo.bar
'baz'
>>> att
{'foo': {'bar': 'baz'}}
YAML serializable
>>> import attridict
>>> import yaml
>>> data = {'foo': {'bar': 'baz'}}
>>> att = attridict(data)
>>> yaml.dump(att)
'!attridict\nfoo:\n bar: baz\n'
>>> yaml.safe_dump(att)
'foo:\n bar: baz\n'
License
The project is MIT licensed
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 attridict-0.0.9.tar.gz.
File metadata
- Download URL: attridict-0.0.9.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0d6b140a17998f32dd71268b3f20a7b74ae5c7d14d628de75cf68f83c61f4ee
|
|
| MD5 |
5703cfd050f894f17f880bef01aae048
|
|
| BLAKE2b-256 |
846e244ca3d5a0d10a575caeb907827741dd0c86afe47c45729c07db46f69037
|
File details
Details for the file attridict-0.0.9-py3-none-any.whl.
File metadata
- Download URL: attridict-0.0.9-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6498b2f4e669105225b759feb2e47af8782fdbdb33610b50dd5d086d1875e49
|
|
| MD5 |
68f76a9253a9a8d40ba5bd1f9b21de01
|
|
| BLAKE2b-256 |
e688356373ef3556e65a495f0adc62177cf435314936cf4187f31e6e1ab97ddf
|