Skip to main content

A Python library that provides some classes and functions that add quality of life improvements to the standard library's `dict`

Project description

Slightly Better Dicts

A Python library that provides some classes and functions that add quality of life improvements to the standard library's dict.

This is a library that I end up re-writing in some form or another every time I work on a new Python project. I decided that it was long past time I threw together a module.


Before and after:

my_data = {
    "id": 123,
    "profile": {
        "name": "Mr. Cool Guy",
        "email": "cool.guy@matthewhale.xyz",
        "address": {
            "street": "123 Baller Ave",
            "city": "Coolsville",
            "state": "HI",
            "zip": 55555,
        },
    },
    "preferences": {
        "notifications": {
            "messages": True,
            "promotions": False,
        },
    },
}

my_data.get("profile").get("address").get("zip")
# 55555

my_data.get("preferences").get("oops").get("frobnicate")
# AttributeError: 'NoneType' object has no attribute 'get'

my_data.get("preferences", {}).get("clunky, huh?", {}).get("frobnicate", "default")
# 'default'


from slightly_better_dicts import BetterDict

my_better_data = BetterDict(my_data)

my_better_data.get_in(["profile", "address", "zip"])
# 55555

my_better_data.get_in(["preferences", "oops", "frobnicate"], "default")
# 'default'

if state := my_better_data.get_in(["profile", "address", "state"]) == "HI":
    print("Hi, HI!")
# Hi, HI!

Installation

It's on PyPI. pip install slightly_better_dicts, poetry add slightly_better_dicts, uv add slightly_better_dicts and so on and so forth and what have you.

Usage

Functionality is provided via two interfaces: subclasses of dict, and a collection of pure functions. All of the methods found in the dict subclasses are also available as regular functions that accept a mapping as their first argument.

For example:

from slightly_better_dicts import BetterDict, get_in

data = {
    "a": {
        "b": {
            "c": True,
        },
    },
}
better_data = BetterDict(data)

better_data.get_in(["a", "b", "c"])
# True

get_in(data, ["a", "b", "c"])
# True

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

slightly_better_dicts-0.1.0.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

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

slightly_better_dicts-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file slightly_better_dicts-0.1.0.tar.gz.

File metadata

File hashes

Hashes for slightly_better_dicts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9caf53d2898d0f75e6eb8747c2e68359450e70290bbcbe5342c878d94f4a1e57
MD5 85298688a549cbd0770ecdcbb9911c42
BLAKE2b-256 59413abdb7ac746c07e1e55be94488826502017ef60096ec29e9eb25cab9d7a8

See more details on using hashes here.

File details

Details for the file slightly_better_dicts-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for slightly_better_dicts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a1ba95647f3b5c5b03d3fbf358b5631283be3c2f7d39b447dd4425a21644978
MD5 895afa7a63c1e22195f33bde2da1830b
BLAKE2b-256 c5d3281d21df10759686f516a1db78745f034eab09fc0c56f811766ff9835f75

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