Skip to main content

Common developer utilities for Python projects.

Project description

SigmaEpsilon.DeepDict - A lightweight Python library to handle nested dictionaries

CircleCI codecov Documentation Status License PyPI Python 3.7‒3.10 Code style: black Requirements Status

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

sigmaepsilon_deepdict-1.2.4.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

sigmaepsilon_deepdict-1.2.4-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file sigmaepsilon_deepdict-1.2.4.tar.gz.

File metadata

  • Download URL: sigmaepsilon_deepdict-1.2.4.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.13 Linux/5.15.0-1053-aws

File hashes

Hashes for sigmaepsilon_deepdict-1.2.4.tar.gz
Algorithm Hash digest
SHA256 a3f7b26d6ee258796bda7b33bd39a296ab18e7f7ea10b8053e5f5a05e0814ec0
MD5 4f1c474c329f8946486ea4cb54d4b6b2
BLAKE2b-256 38298bbf7a868def1d017064488d44a6b1f32fe84ad6d2adecc90068059a3a21

See more details on using hashes here.

File details

Details for the file sigmaepsilon_deepdict-1.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for sigmaepsilon_deepdict-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 85061c7ba21b7723431da6b47a486116647936b3775b942c09d848a36991b54b
MD5 a6a1e03f83e1472cd3a0b438d933b89a
BLAKE2b-256 55a94c1e516d50e0db6c6505fa3aed685402cc688a308aaee0884c55349e5644

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page