An easy to use and easy to read dict
Project description
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
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 attribdict-0.0.5.tar.gz
.
File metadata
- Download URL: attribdict-0.0.5.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eee26d936a66907cd2e2cc2d42505c889174eeac79a7539356e5a25cc9fa6178 |
|
MD5 | b3195e933519250b8304c819f61f0aab |
|
BLAKE2b-256 | fecd61104e1ea717d1fc20200f8022da35dc025a2f1a543cef367539f9c8b5a3 |
File details
Details for the file attribdict-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: attribdict-0.0.5-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d7562b5783df40529bc9eb7d5178e4ad816855e60ef1c0263e5af61a622a28c |
|
MD5 | 0edf04e2508eb23a0d82a94184ddf3d2 |
|
BLAKE2b-256 | fac41a8fc63b5ea6b50fd672c7c49c7489136b2e5af6c2ea41827487b9956755 |