Linked deep dictionaries in Python.
Project description
SigmaEpsilon.DeepDict - A lightweight Python library to handle nested dictionaries
The DeepDict
class works very similarly to a regular dict
, but makes life easier in sutiations where there are nested levels, especially when such dictionary is to be created dynamically.
Motivating examples
Consider the following simple dictionary
>>> d = {
... "a" : {"aa" : 1},
... "b" : 2,
... "c" : {"cc" : {"ccc" : 3}},
... }
This is what happens when you iterate through the values of it:
>>> list(d.values())
[{'aa': 1}, 2, {'cc': {'ccc': 3}}]
If you wrap it as a DeepDict
:
>>> from sigmaepsilon.deepdict import DeepDict
>>> dd = DeepDict.wrap(d)
>>> list(dd.values(deep=True))
[1, 2, 3]
The class supports array-like indexing, which combined with the default factory creates the possibility to create deep levels without having to create all the parents:
>>> data = DeepDict()
>>> data["a", 0, "b", 1, 5] = 10
Each subdirectory is aware about it's parent container
>>> data["a", 0, "b"].key
"b"
>>> data["a", 0, "b"].address
['a', 0, 'b']
>>> data["a", 0, "b"].parent.key
0
Given that the asciitree
library is installed, it is also possible to print dictionaries (any kind) as a tree:
>>> from sigmaepsilon.deepdict import asciiprint
>>> d = {
... "a" : {"aa" : 1},
... "b" : 2,
... "c" : {"cc" : {"ccc" : 3}},
... }
>>> data = DeepDict.wrap(d)
>>> data.name = "Data"
>>> asciiprint(data)
Data
+-- a
+-- c
+-- cc
See the documentation for more examples and explanation on behaviour.
Documentation
The documentation is built with Sphinx using the PyData Sphinx Theme and hosted on ReadTheDocs.
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 sigmaepsilon.deepdict
License
This package is licensed under the MIT license.
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 sigmaepsilon.deepdict-1.2.2.tar.gz
.
File metadata
- Download URL: sigmaepsilon.deepdict-1.2.2.tar.gz
- Upload date:
- Size: 29.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0073184afa7053601388b4df21c743d6306fda30ccdcb1f6aea1fe533303984 |
|
MD5 | 2fe900d5201ac9c6fd7133523c4f5f98 |
|
BLAKE2b-256 | e5725f7bdf59e230e5e0152dc5c786f78cb563b98a35f8fa2b74152faa434a25 |
File details
Details for the file sigmaepsilon.deepdict-1.2.2-py3-none-any.whl
.
File metadata
- Download URL: sigmaepsilon.deepdict-1.2.2-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f4189ae327e9da494163fc53bb69bda92bd8b65b0d2c5f4fdc38759fe4d42c2 |
|
MD5 | 788365ef8d254a8d7e5ab5d95b93a2e2 |
|
BLAKE2b-256 | bfd47a2799521254e95c4679378d6fad9353d54b6b24cf92f3e1791f591cfe40 |