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 syncrye run t
Test Coverage
rye run tc
Inspiration
Metadata
Release files for transformd 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| transformd-0.3.0.tar.gz | 6.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| transformd-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.8 kB
Release files / transformd-0.3.0.tar.gz
| Download URL | transformd-0.3.0.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8be4f5d7a1dfdf8d80ee4adf2d415ad8e6b298f22389147fa778ccb52593836a
|
|
BLAKE2b-256 checksum How to use checksums |
5580ecaddd98fadcbf6e761abc4d291bf99f5559f087cad0d1f234a6176cc4fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / transformd-0.3.0-py3-none-any.whl
| Download URL | transformd-0.3.0-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7ca14b3dc2a5d37e6b8a5a0434ce56eecaa7bc25def2c203bbd0bc85a6a68944
|
|
BLAKE2b-256 checksum How to use checksums |
c7a4375ce3ff7979d91d10b0e6bf67ba50e694b1172e3c3272c1ae0a31924a6b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for transformd 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.