AttrDict
This package contains a generic dictionary AttrDict, which implements nested dictionaries that are easy to access, filter, combine, and write to.
Installation: pip install attr-dicts:
PyPI: https://pypi.org/project/attr-dicts/
Creation: Let's say we want to store the following nested structure of arrays:
- food
- carrot: [1,2,3]
- apple: [4,5,6]
- broccoli: [7,8,9,10]
- utensils
- fork
- three_prong: [11,12]
- four_prong: [13,14,15]
- spoon: [16,17,18]
AttrDicts use '/' to separate keys, and this is built in to read/write operations. Here's two examples of how to instantiate the above structure.
d = AttrDict()
d['food/carrot'] = [1,2,3]
d.food.apple = [4,5,6]
d['food/broccoli'] = [7,8,9,10]
d['utensils/fork/three_prong'] = [11,12]
d['utensils/fork/four_prong'] = [13,14,15]
d['utensils/spoon'] = [16,17,18]
Note that both indexing and dot access work, since AttrDicts inherit from the DotMap class. Here's a slightly less effort way:
d = AttrDict(
food=AttrDict(carrot=[1,2,3], apple=[4,5,6], broccoli=[7,8,9,10]),
utensils=AttrDict(
fork=AttrDict(three_prong=[11,12], four_prong=[13,14,15]),
spoon=[16,17,18]
)
)
Access: There are several ways to access an AttrDict.
d['utensils/fork/three_prong']: standard dictionary access, but using '/' to implicitly sub-index (KeyError if missing)d.utensils.fork.three_prong: dotmap access (AttributeError if missing)d.utensils['fork/three_prong']: mixed indexing + dotmap (either AttributeError or KeyError if missing)d >> 'utensils/fork/three_prong: (DEPRECATED) required key access, will error if not present.d << 'utensils/fork/three_prong: optional key access, will return None if not presentd > ['utensils/fork/three_prong,'utensils/spoon']: required key filtering, returns sub-dict. errors if a key in the arg list is not present.d < ['utensils/fork/three_prong,'utensils/spoon']: optional key access, returns sub-dict, ignores keys that aren't present.
Node/Leaf operations: Leaf nodes are any access pattern that returns something that isn't an AttrDict. In the above example, 'food' is a node key, while 'food/carrot' is a leaf key. We can operate on all leaf nodes at once, here are some example methods:
d.leaf_keys(): Generator that yields leaf keys under a depth first traverse.d.list_leaf_keys(): Outputs a list instead of generator.d.leaf_values(): Generator that yields leaf values under a depth first traverse.applied_d = d.leaf_apply(lambda v: <new_v>): Apply a function(value) on all leaf values, and create a new AttrDict.filtered_d = d.leaf_filter(lambda k,v: <condition>): Only keep leaf keys whereconditionis true in new AttrDict.
Similarly, there are functions that operate on both nodes and leaves.
Combining: Combining AttrDicts can be done in several ways:
new_d = d1 & d2: Standard join, returns a new AttrDict, which will favor keys from d2 if there are duplicates.d1.combine(d2): Mutates d1 to join the arrays.new_d = AttrDict.leaf_combine_and_apply([d1, d2, ...], lambda vs: <return one value>): Given a list of AttrDicts with the same keys, will create one AttrDict where the value for a given keykis some function ofvs = [d1[k], d2[k], ...].
Metadata
Release files for attr-dicts 1.0.3
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 |
|---|---|---|---|---|
| attr_dicts-1.0.3.tar.gz | 7.9 kB | Without provenance | Details for attr_dicts-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| attr_dicts-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for attr_dicts-1.0.3-py3-none-any.whl |
Total release size: 17.3 kB
Release files / attr_dicts-1.0.3.tar.gz
| Download URL | attr_dicts-1.0.3.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e5044b9cccb81b76a82877d581daa2e92c0aac9751a1166567ca6b2d356dd5da
|
|
BLAKE2b-256 checksum How to use checksums |
854e3c370fa21f5ba950f0abc7481ecb8bb0d2dd8e4cf4bbd091e5fce6fba3fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.8.17
|
Release files / attr_dicts-1.0.3-py3-none-any.whl
| Download URL | attr_dicts-1.0.3-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
228236d3a8794c5653135c781b087d1bb6f68dd814bff024f999deeab67469d5
|
|
BLAKE2b-256 checksum How to use checksums |
60fc63320c9c80a56b886471f34655f116c956c33a3dab1ceae4b2082805304b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.8.17
|
Security for attr-dicts 1.0.3
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.