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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file slightly_better_dicts-0.1.0.tar.gz.
File metadata
- Download URL: slightly_better_dicts-0.1.0.tar.gz
- Upload date:
- Size: 2.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9caf53d2898d0f75e6eb8747c2e68359450e70290bbcbe5342c878d94f4a1e57
|
|
| MD5 |
85298688a549cbd0770ecdcbb9911c42
|
|
| BLAKE2b-256 |
59413abdb7ac746c07e1e55be94488826502017ef60096ec29e9eb25cab9d7a8
|
File details
Details for the file slightly_better_dicts-0.1.0-py3-none-any.whl.
File metadata
- Download URL: slightly_better_dicts-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a1ba95647f3b5c5b03d3fbf358b5631283be3c2f7d39b447dd4425a21644978
|
|
| MD5 |
895afa7a63c1e22195f33bde2da1830b
|
|
| BLAKE2b-256 |
c5d3281d21df10759686f516a1db78745f034eab09fc0c56f811766ff9835f75
|