LinkedDeepDict
A Lightweight Python library to manage nested dictionaries with parent-child relationships.
On top of being a compatible drop-in replcement of the build in dict class, the self replicating default factory makes the creation of complex nested layouts effortless.
Documentation
Click here to read the documentation.
Installation
This is optional, but we suggest you to create a dedicated virtual enviroment at all times to avoid conflicts with your other projects. Create a folder, open a command shell in that folder and use the following command
>>> python -m venv venv_name
Once the enviroment is created, activate it via typing
>>> .\venv_name\Scripts\activate
The library can be installed (either in a virtual enviroment or globally) from PyPI using pip on Python >= 3.7:
>>> pip install linkeddeepdict
Usage
In every case where you'd want to use a dict, you can use a LinkedDeepDict as a drop-in replacement, but on top of what a simple dictionary provides, a LinkedDeepDict is more capable, as it provides a machinery to handle nested layouts. It is basically an ordered defaultdict with a self replicating default factory.
>>> from linkeddeepdict import LinkedDeepDict
>>> data = LinkedDeepDict()
A LinkedDeepDict is essentially a nested default dictionary. Being nested refers to the fact that you can do this:
>>> data['a']['b']['c']['e'] = 1
>>> data['a']['b']['d'] = 2
Notice that the object carves a way up until the last key, without needing to create each level explicitly. What happens is that every time a key is missing in a data, the object creates a new instance, which then is also ready to handle missing keys or data. Accessing nested subdictionaries works in a similar fashion:
>>> data['a']['b']['c']['e']
1
To allow for a more Pythonic feel, it also supports array-like indexing, so that the following operations are valid:
>>> data['a', 'b', 'c', 'e'] = 3
>>> data['a', 'b', 'c', 'e']
3
Of course, this is something that we can easily replicate using pure Python in one line, without the need for fancy stuff:
>>> data = {'a' : {'b' : {'c' : {'e' : 3}, 'd' : 2}}}
The key point is that we loop over a pure dict instance, we get
>>> [k for k in data.keys()]
['a']
But if we use a LinkedDeepDict class and the option deep=True when accessing
keys, values or items of dictionaries, the following happens:
>>> [k for k in LinkedDeepDict(data).keys(deep=True)]
['e', 'd']
We can see, that in this case, iteration goes over keys, that actually hold on to some data, and does not return the containers themselves. If we do the same experiment with the values, it shows that the LinkedDeepDict only returns the leafs of the data-tree and the behaviour is fundamentally different:
>>> [k for k in data.values()]
[{'b': {'c': {'e': 3}, 'd': 2}}]
>>> [k for k in LinkedDeepDict(data).values(deep=True)]
[3, 2]
It is important, that the call obj.values(deep=True) still returns a generator object, which makes it memory efficient when looping over large datasets.
>>> LinkedDeepDict(data).values(deep=True)
<generator object OrderedDefaultDict.values at 0x0000028F209D54A0>
Testing
To run all tests, open up a console in the root directory of the project and type the following
>>> python -m unittest
Dependencies
The only dependency is six, to provide basic continuity between major Python versions 2 and 3.
License
This package is licensed under the MIT license.
Metadata
Release files for linkeddeepdict 1.1.0
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 | |
|---|---|---|---|---|
| linkeddeepdict-1.1.0.tar.gz | 11.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| linkeddeepdict-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.8 kB
Release files / linkeddeepdict-1.1.0.tar.gz
| Download URL | linkeddeepdict-1.1.0.tar.gz |
|---|---|
| Size | 11.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c65bc34c110ea1a2071a411f6481485d4a5da9da029996ead8aca2c39288609
|
|
BLAKE2b-256 checksum How to use checksums |
348f21a617a363e2a9011612f64b2117856affcbc57d7e41e6d1411c084a6d7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.11
|
Release files / linkeddeepdict-1.1.0-py3-none-any.whl
| Download URL | linkeddeepdict-1.1.0-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c076e7feedeea7356276f1dabc07b94f014349b3aba7ee225a56eb8d9011288
|
|
BLAKE2b-256 checksum How to use checksums |
8f3c202f6aa0d0566d647280ceff9b18b30f49098be7a754c7d24cc3b4231dc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.11
|
Security for linkeddeepdict 1.1.0
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.