dictor: An Elegant Dictionary and JSON Handler

0.1.12 · active · verified Tue Apr 14

Dictor is a Python JSON and Dictionary (Hash, Map) handler. It takes a dictionary or JSON data and returns a value for a specific key, simplifying access to deeply nested structures. Its primary goal is to eliminate the need for repetitive `try/except` blocks when performing lookups and to provide flexible fallback values for missing keys. The current version is 0.1.12, with new releases typically driven by feature additions or bug fixes rather than a strict cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates basic usage of `dictor` to safely retrieve nested values from a dictionary using dot notation, providing a default value for non-existent keys, and accessing elements within lists.

import json
from dictor import dictor

data = {
    "characters": {
        "Lonestar": {
            "id": 55923,
            "role": "renegade",
            "items": ["space winnebago", "leather jacket"]
        },
        "Barfolomew": {
            "id": 55924,
            "role": "mawg",
            "items": ["peanut butter jar", "waggy tail"]
        }
    }
}

# Accessing a nested value using dot notation
lonestar_role = dictor(data, "characters.Lonestar.role")
print(f"Lonestar's role: {lonestar_role}")

# Accessing a non-existent key with a fallback default value
dark_helmet_role = dictor(data, "characters.Dark Helmet.role", default="villain")
print(f"Dark Helmet's role: {dark_helmet_role}")

# Accessing an item from a list by index
first_lonestar_item = dictor(data, "characters.Lonestar.items.0")
print(f"First item for Lonestar: {first_lonestar_item}")

view raw JSON →