Skip to main content

Dictionary types that make it easier to work trees of nested dictionaries.

Consider a nested regular dictionaries:

d = {
    "en": {
        "gb": "English (United Kingdom)",
        "us": "English (United States)",
        "ca": "English (Canada)",
        "za": "English (South Africa)",
    },
    "no": {
        "nb": "Norwegian (Bokmål)",
        "nn": "Norwegian (Nynorsk)",
    },
    "fr": {
        "be": "French (Belgium)",
        "ca": "French (Canada)",
        "fr": "French (Standard)",
        "ch": "French (Switzerland)",
    }
}

from which we can construct a DeepDict:

>>> from dendrodict import DeepDict
>>> languages = DeepDict(d)

Nested dictionaries can be accessed with single application of the square brackets get-item operator:

>>> languages["fr", "ca"]
'French (Canada)'

or by using a tuple as a key:

>>> languages[("no", "nb")]
'Norwegian (Bokmål)'

The tuple key can also be used with other dictionary methods, such as get():

>>> languages.get(("en", "us"))
'English (United States)'

It’s still possible index the inner dictionary levels:

>>> languages["en"]
DeepDict(
    {
        'gb': 'English (United Kingdom)',
        'us': 'English (United States)',
        'ca': 'English (Canada)',
        'za': 'English (South Africa)'
    }
)

which means that separate lookup operations are still supported:

>>> languages["en"]["za"]
'English (South Africa)'

The tuple key form can also be used on construction, so flat dictionaries with tuple keys will be expanded into nested dictionaries. Consider the following single-level mapping of tuple keys to values:

blocks = {
    ("mineral", "sandstone", "block"): "Sandstone",
    ("mineral", "sandstone", "chiseled"): "Sandstone (Chiseled)",
    ("mineral", "cobblestone", "block"): "Cobblestone",
    ("mineral", "cobblestone", "slab"): "Cobblestone (Slab)",
    ("mineral", "gold", "ore"): "Gold Ore",
    ("wood", "birch", "block"): "Birch",
    ("wood", "birch", "slab"): "Birch-Wood Slab",
    ("wood", "oak", "block"): "Oak",
    ("wood", "oak", "plank"): "Wooden Plank (Oak)",
}

Created a DeepDict from this, gives a three-level structure:

>>> minecraft = DeepDict(blocks)

which we can see by converted back to dictionaries using the to_dict() method:

>>> minecraft.to_dict()
{'mineral': {
    'cobblestone': {
        'block': 'Cobblestone',
        'slab': 'Cobblestone (Slab)'
    },
    'gold': {
        'ore': 'Gold Ore'
    },
    'sandstone': {
        'block': 'Sandstone',
        'chiseled': 'Sandstone (Chiseled)'
    }
},
'wood': {
    'birch': {
        'block': 'Birch',
        'slab': 'Birch-Wood Slab'
    },
    'oak': {
        'block': 'Oak',
        'plank': 'Wooden Plank (Oak)'}
    }
}

Particularly when dealing with configuration data, it can be useful to be able to use dotted names for access to nested items. Use DottedDeepDict for this:

from dendrodict import DottedDeepDict

displays = DottedDeepDict(
    {
        "resolution": {
            "vga": {
                "width": 640,
                "height": 480,
            },
            "wxga": {
                "width": 1280,
                "height": 800,
            },
            "wqhd": {
                "width": 2560,
                "height": 1440,
            },
        }
    }
)

Dotted name syntax can be used for lookup:

>>> displays["resolution.vga.width"]
640

or for assignment at arbitrary levels:

>>> displays["resolution.1080p.width"] = 1920
>>> displays["resolution.1080p.height"] = 1080

Shallower levels are still available individually:

>>> displays["resolution.1080p"]
DottedDeepDict({'width': 1920, 'height': 1080})

and can be converted back to regular dictionaries:

>>> displays["resolution.wqhd"].to_dict()
{'width': 2560, 'height': 1440}

Metadata

Release files for dendrodict 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for dendrodict 1.0.4
File Interpreter ABI Platform
dendrodict-1.0.4-py3-none-any.whl Python 3 none any Details

Release files / dendrodict-1.0.4-py3-none-any.whl

Download URL dendrodict-1.0.4-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab42831092433a2eef0982478e323c9ce4eb984ce64b4d78680decdf6cee605f
BLAKE2b-256 checksum
How to use checksums
28ad8cb8d8273fe70bc7e00bbd94b5b47777085a5da18e56281f93f0392d964f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Release history Release notifications | RSS feed

This release

1.0.4 This release

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page