AttribDict
Introduction
AttribDict is an easy to use and easy to read dict, it is more flexible and human readable.
Examples
>>> from attribdict import AttribDict as Dict
>>> _d = {"attr"+str(i): i for i in range(4)}
>>> d = Dict(_d) # create a AttribDict instance from a dict
>>> print(d)
attr0: 0
attr1: 1
attr2: 2
attr3: 3
You can also recursively create an attribute from other data type including dict:
# continue to previous code
>>> d.attr4.subattr1.subsubattr1 = {1, 2, 4} # recursively create attribute
>>> d.attr5.subattr1 = {"subsubattr"+str(i): i for i in range(3)} # recursively create from a dict
>>> print(d)
attr0: 0
attr1: 1
attr2: 2
attr3: 3
attr4:
- subattr1:
- subsubattr1: {1, 2, 4}
attr5:
- subattr1:
- subsubattr0: 0
- subsubattr1: 1
- subsubattr2: 2
Additionally, AttribDict also support create attributes from a recursively dict:
# continue to previous code
>>> _d = {"_attr1": {"_subattr1": {"_subsubattr1": "hello attribdict"}}} # create attributes from a recursively dict
>>> d._attr = _d
>>> print(d)
attr0: 0
attr1: 1
attr2: 2
attr3: 3
attr4:
- subattr1:
- subsubattr1: {1, 2, 4}
attr5:
- subattr1:
- subsubattr0: 0
- subsubattr1: 1
- subsubattr2: 2
_attr:
- _attr1:
- _subattr1:
- _subsubattr1: hello attribdict
If you want to convert an AttribDict instance into a python dict, just use .as_dict method:
# continue to previous code
>>> _dict = d.as_dict()
>>> print(_dict)
{'attr0': 0, 'attr1': 1, 'attr2': 2, 'attr3': 3, 'attr4': {'subattr1': {'subsubattr1': {1, 2, 4}}}, 'attr5': {'subattr1': {'subsubattr0': 0, 'subsubattr1': 1, 'subsubattr2': 2}}, '_attr': {'_attr1': {'_subattr1': {'_subsubattr1': 'hello attribdict'}}}}
You can also access value in traditional dict way, for example:
>>> print(d["_attr"].as_dict())
{'_attr1': {'_subattr1': {'_subsubattr1': 'hello attribdict'}}}
Please feel free if you want to access value in following style:
>>> print(d._attr["_attr1"]._subattr1["_subsubattr1"])
hello attribdict
AttribDict instance is iterable, thus you can use for statement and so on to iteratively visit the attributes and corresponding values. Note that it will return (attribute, value) pair, sub-AttribDict will be converted to a python dict for more convenient use:
# continue to previous code
>>> for key, value in d:
... print("key: ", key)
... print("value: ", value)
...
key: attr0
value: 0
key: attr1
value: 1
key: attr2
value: 2
key: attr3
value: 3
key: attr4
value: {'subattr1': {'subsubattr1': {1, 2, 4}}}
key: attr5
value: {'subattr1': {'subsubattr0': 0, 'subsubattr1': 1, 'subsubattr2': 2}}
key: _attr
value: {'_attr1': {'_subattr1': {'_subsubattr1': 'hello attribdict'}}}
Here is another example about iterable, the rest code is omitted:
>>> it = iter(d)
>>> next(it)
('attr0', 0)
>>> next(it)
('attr1', 1)
>>> next(it)
('attr2', 2)
AttribDict offers two ways to copy and deepcopy an instance:
>>> import copy
>>> d_copy = d.copy()
>>> print(d._attr is d_copy._attr)
True
>>> copy_d = copy.copy(d)
>>> print(d._attr is copy_d._attr)
>>> True
>>> d_deepcopy = d.deepcopy()
>>> print(d._attr is d_deepcopy._attr)
False
>>> print(d.attr4.subattr1.subsubattr1 is d_deepcopy.attr4.subattr1.subsubattr1)
False
>>> deepcopy_d = copy.deepcopy(d)
>>> print(d._attr is deepcopy_d._attr)
>>> False
>>> print(d.attr4.subattr1.subsubattr1 is deepcopy_d.attr4.subattr1.subsubattr1)
False
If you want to check whether an object contains attribute attrX, please use obj.hasattr("attrX") but hasattr(obj, "attrX") as the latter one will create a new AttribDict instance named attrX.
>>> d.hasattr("attrX")
False
>>> hasattr(d, "attrX")
True
Pickling AttribDict instance.
>>> import pickle
>>> with open("path2file", "wb") as fp:
>>> ... pickle.dump(d, fp)
>>> ...
>>> with open("path2file", "rb") as fp:
>>> ... loaded_d = pickle.load(fp)
>>> ...
>>> loaded_d == d
True
>>> loaded_d is d
False
Installation
You can install AttribDict by pip.
pip install attribdict
Metadata
Release files for attribdict 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| attribdict-0.0.5.tar.gz | 4.1 kB | Without provenance | Details for attribdict-0.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| attribdict-0.0.5-py3-none-any.whl | Python 3 | none | any | Details for attribdict-0.0.5-py3-none-any.whl |
Total release size: 9.0 kB
Release files / attribdict-0.0.5.tar.gz
| Download URL | attribdict-0.0.5.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eee26d936a66907cd2e2cc2d42505c889174eeac79a7539356e5a25cc9fa6178
|
|
BLAKE2b-256 checksum How to use checksums |
fecd61104e1ea717d1fc20200f8022da35dc025a2f1a543cef367539f9c8b5a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2.post20201201 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
|
Release files / attribdict-0.0.5-py3-none-any.whl
| Download URL | attribdict-0.0.5-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7d7562b5783df40529bc9eb7d5178e4ad816855e60ef1c0263e5af61a622a28c
|
|
BLAKE2b-256 checksum How to use checksums |
fac41a8fc63b5ea6b50fd672c7c49c7489136b2e5af6c2ea41827487b9956755
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2.post20201201 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
|
Security for attribdict 0.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.