Skip to main content

No project description provided

Project description

SafeDict

Python does not natively support a chaining operator (like ?. in JavaScript) for safely accessing nested dictionary keys. This often leads to verbose code to check if a key exists or if its value is None before accessing deeper levels.

SafeDict is here to solve this problem.

SafeDict is a Python library that provides a safe way to access nested dictionary keys. When a key does not exist or its value is None, it returns None by default (or a user-specified default value). Unlike standard dictionaries, it prevents errors when accessing deeper levels of nested keys, making your code cleaner, easier to read, and less error-prone.


Key Features

  • Safely access nested dictionary keys without manual checks.
  • Returns an empty SafeDict when a key does not exist or its value is None.
  • Fully compatible with regular Python dictionaries.

Installation

Install the library via pip:

pip install safe-dict

Usage

from safe_dict import SafeDict

# Example with a nested dictionary
data = SafeDict({"a": {"b": {"c": 42}}})

# Safe key access using get
print(data.get("a").get("b").get("c"))  # Output: 42
print(data.get("a").get("x").get("y"))  # Output: None

# Safe key access with a default value
print(data.get("a").get("x", "default"))  # Output: default

Comparison

Traditional Way:

Using a standard dictionary, you would need to manually check each key:

data = {
    "a": {
        "b": {
            "c": 42
        },
        "d": None
    }
}

# Safe access
if "a" in data and data["a"] is not None:
    if "b" in data["a"] and data["a"]["b"] is not None:
        if "c" in data["a"]["b"]:
            print(data["a"]["b"]["c"])

# You can also use the "get" method, but if you access a key whose value is None, an error will occur:
print(data.get("a", {}).get("d", {}).get("c")) # AttributeError: 'NoneType' object has no attribute 'get', because "d" is not a dictionary.

With SafeDict:

You can write the same logic much more cleanly:

data = SafeDict({"a": {"b": {"c": 42}}})

print(data.get("a", {}).get("b", {}).get("c"))  # Output: 42
print(data.get("a", {}).get("d", {}).get("c"))  # Output: None

License

SafeDict is licensed under the MIT License, allowing you to use and modify it freely.

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

safe_dict-1.0.2.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safe_dict-1.0.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file safe_dict-1.0.2.tar.gz.

File metadata

  • Download URL: safe_dict-1.0.2.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for safe_dict-1.0.2.tar.gz
Algorithm Hash digest
SHA256 49970fd7428f76f8e7009bab70f2246c8fdd23ce1c9d1b72ac0a817efa98e684
MD5 19e64eb194761ab39d40edd674cf9096
BLAKE2b-256 b61b34c17427519349c485ab6a187f48b429c90a0ce7da68c4d1b9ffb38ca7fe

See more details on using hashes here.

File details

Details for the file safe_dict-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: safe_dict-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for safe_dict-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8022d2d95f541bb639b72a8ab78937a53b7d3799594b8986b1be8563615ff898
MD5 209aa957c2680c5da35a6e95dfe484aa
BLAKE2b-256 64905b44e204c4b5868bee94d5b6b9860421dbb5e8deac8e28f61b808f1b8690

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