minimal attrdict implementation
Project description
attribute-mapping is a minimalistic python library to allow attribute lookups in dictionaries and mappings.
Really? Another implementation?
Compared to many other implementations of the same idea, going by names such as AttrDict and various others, this library is extremely minimal, to the point that it has _no_ restrictions, _no_ surprises and virtually _no_ API.
Installation
python -m pip install attribute-mapping
Usage
Make an AttributeMapping instance by passing a dictionary or another mapping to the constructor:
from attribute_mapping import AttributeMapping d = {"a": 1, "b": {"c": 2, "d": 3}} x = AttributeMapping(d)
Now you can access the contents using attribute lookups:
x.a # gives 1 x.b.c # gives 2 x.foo = 123 hasattr(x, "foo") # True del x.foo
In addition to attribute access, subscription (__getitem__ and friends) and containment checks (in) also work:
x["a"] # gives 1 x["b"]["c"] # gives 2 x["foo"] = 123 "foo" in x # True del x["foo"]
However, there are _no_ other dict-like methods or reserved names, so you can happily use attributes like keys and items:
x.items = [1, 2, 3]
Iteration yields (key, value) tuples, just like .items() on normal mappings would do:
for key, value in x: ...
Finally, to obtain the original object that was used for the AttributeMapping, use the built-in vars() function:
d = {"a": 1} x = AttributeMapping(d) vars(x) is d # True
Credits
This library is written by wouter bolsterlee (wbolster).
There are a gazillion similar implementations, so the author thanks the whole Python community for the inspiration to make yet another implementation of this idea.
License
BSD; see LICENSE file for details.
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
File details
Details for the file attribute-mapping-1.0.0.tar.gz
.
File metadata
- Download URL: attribute-mapping-1.0.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67f2ff35eb6643be7a9f5333d7331a3539141004804aa627a3bfa2b828307de4 |
|
MD5 | d34b75cfbd1d053169fbe9e63ba68b68 |
|
BLAKE2b-256 | 11943cdfcc9d46b5f50ad4e370b6251b25e0cecf1c46a612a0a1621f6d14ab37 |
File details
Details for the file attribute_mapping-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: attribute_mapping-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98065f29a597a37f347cf8b5b913ba0bb8273275f863c8283eec45f344ca656c |
|
MD5 | 8e096f1c34311c0e2464bd8bdde08426 |
|
BLAKE2b-256 | 91cb8e29d05d13ad2339650de6a73d8051d484aa0b1cf79de906300a528c6db7 |