Transform dictionaries into whatever your heart desires (as long as it's another dictionary that kind of looks like the original dictionary)
Project description
transformd
Transform a dictionary
to another dictionary
, but keep the same shape based on a spec
.
What is a spec?
It is a string (or sequence of strings) that specifies which "parts" of the dictionary should be included or excluded in a new dictionary
that is returned from the transform
function.
A spec
uses dot-notation to specify how to traverse into the dictionary
. It can also use indexes if the value of the dictionary
is a list.
Note: Specs
are applied to the original dictionary
in the order they are defined -- the dictionary
gets updated after each spec.
Examples
from transformd import Transformer
# Initial `dictionary` we'll transform based on a spec below
data = {
"library": {
"name": "Main St Library",
"location": {
"street": "123 Main St",
"city": "New York City",
"state": "NY",
},
"books": [
{
"title": "The Grapes of Wrath",
"author": {"first_name": "John", "last_name": "Steinbeck"},
},
{
"title": "Slaughterhouse-Five",
"author": {"first_name": "Kurt", "last_name": "Vonnegut"},
},
],
}
}
# Only return a nested part of `data`
assert Transformer(data).transform(spec="library.name") == {
"library": {
"name": "Main St Library"
}
}
# Return multiple parts of `data`
assert Transformer(data).transform(spec=("library.name", "library.location.state")) == {
"library": {
"name": "Main St Library",
"location": {
"state": "NY"
},
}
}
# Return different parts of a nested list in `data`
assert Transformer(data).transform(spec=("library.books.0.title", "library.books.1")) == {
"library": {
"books": [
{
"title": "The Grapes of Wrath",
},
{
"title": "Slaughterhouse-Five",
"author": {"first_name": "Kurt", "last_name": "Vonnegut"},
},
],
}
}
# Exclude pieces from `data` by prefixing a spec with a dash
assert Transformer(data).transform(spec=("-library.books", "-library.location")) == {
"library": {
"name": "Main St Library"
}
}
Why?
I needed this functionality for Unicorn
, but could not find a suitable library. After writing the code, I thought maybe it would be useful for someone else. 🤷
Run tests
- Install rye
rye sync
rye run t
Test Coverage
rye run tc
Inspiration
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
Built Distribution
File details
Details for the file transformd-0.3.0.tar.gz
.
File metadata
- Download URL: transformd-0.3.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8be4f5d7a1dfdf8d80ee4adf2d415ad8e6b298f22389147fa778ccb52593836a |
|
MD5 | 6680fb6de71e10e1e6c888045d49e34f |
|
BLAKE2b-256 | 5580ecaddd98fadcbf6e761abc4d291bf99f5559f087cad0d1f234a6176cc4fd |
File details
Details for the file transformd-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: transformd-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ca14b3dc2a5d37e6b8a5a0434ce56eecaa7bc25def2c203bbd0bc85a6a68944 |
|
MD5 | 76e27ad4520fac29d5e450195f57b94c |
|
BLAKE2b-256 | c7a4375ce3ff7979d91d10b0e6bf67ba50e694b1172e3c3272c1ae0a31924a6b |