Skip to main content

DictDots is a tool to access nested dictionaries without long if-else chains.

Project description

Python Dict Dots


A way access values in nested key-value dicts using dot notation, e.g.

Before:

data = None
if needle in haystack:
    if nested_needle in haystack[needle]:
        data = haystack[needle][nested_needle]

if not data:
    data = default_data

After:

from dictdots import DictDots

data = DictDots.get(
    "needle.nested_needle", haystack, default=default_data
)

How to


  • Run pip install DictDots
  • from dictdots import DictDots
  • DictDots.get("needle", haystack_dict, default=default_value)

DictDots supports List and Dict.

For example

from dictdots import DictDots
haystack = [
    {
        "needle": {
            "nested": "you found me",
        },
    },
]

value = DictDots.get(haystack, "0.needle.nested")

print(value)

Would output you found me.

A valid query string is a string of keys and/or indicies, separated by periods. Strings can only contain alphanumeric characters, periods, and underscores. Strings can not contain double-or-more dots. Strings can not begin or end with a dot. Each period in the string will tell DictDots to dig another layer into your nested data structure. Dict dots does not hold your hand, if you give it a bad string, e.g _hello...world, then it will just raise an InvalidQueryString.

Future


Query Language wishlist

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

DictDots-0.2.0.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file DictDots-0.2.0.tar.gz.

File metadata

  • Download URL: DictDots-0.2.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/3.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.9

File hashes

Hashes for DictDots-0.2.0.tar.gz
Algorithm Hash digest
SHA256 46eb7e904cb9ff0b8a7195e1f6a932afeb51d739e046cad2a6a13a494adef2d4
MD5 8333d5d9fd369da06ed4621c2a592d5c
BLAKE2b-256 f8b6321a33a278606ad02551c4ce964c8fc02620da254615542b549ea8105740

See more details on using hashes here.

Supported by

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