Skip to main content

Common developer utilities for Python projects.

Project description

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

CircleCI codecov Codacy Badge Documentation Status License PyPI Python Code style: black Requirements Status

sigmaepsilon.deepdict is a lightweight Python library designed to handle nested dictionaries more easily, especially in cases where dictionaries are created dynamically. Its key feature is the DeepDict class, which extends the regular dict to support nested layouts. It allows easy manipulation of deeply nested structures, array-like indexing, and automatic creation of deep dictionary levels without manually defining all parent keys. It is particularly useful in scenarios involving hierarchical data. The library also supports printing dictionaries as trees.

Highlights

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 allows array-like indexing, and when combined with the default factory, enables the creation of deep levels without manually defining all parent keys:

>>> data = DeepDict()
>>> data["a", 0, "b", 1, 5] = 10

Each subdirectory is aware about its 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

The library can be installed from PyPI using pip on Python >= 3.10:

>>> 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-2.0.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

sigmaepsilon_deepdict-2.0.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sigmaepsilon_deepdict-2.0.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.7 Linux/5.15.0-1057-aws

File hashes

Hashes for sigmaepsilon_deepdict-2.0.0.tar.gz
Algorithm Hash digest
SHA256 a3e2c70efc1e6988114147f341b26d21a8b767b5b6bcb16eb8afb5a8579ef9b0
MD5 33aef97270a2b3370d037e52ff6fd33d
BLAKE2b-256 d697fba50e5bb5d6ebb5fd903aef509c76a7731fb996777e4c5c789d4dcb9fc4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sigmaepsilon_deepdict-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cfe77de210026c5c4159570664d14bd85f7906da948df0665c05e6e342aed57c
MD5 9f90de996dee14bc54ff37b2fa4a7922
BLAKE2b-256 dcfa4c1a204b4af4c9e89710068a3d4e742fdada53d5b29aa3407346e5905566

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