Name | transformd JSON |
Version |
0.3.0
JSON |
| download |
home_page | |
Summary | Transform dictionaries into whatever your heart desires (as long as it's another dictionary that kind of looks like the original dictionary) |
upload_time | 2023-12-04 03:52:51 |
maintainer | |
docs_url | None |
author | Adam Hill |
requires_python | >=3.8 |
license | |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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
```python
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`](https://www.django-unicorn.com), 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](https://rye-up.com)
- `rye sync`
- `rye run t`
### Test Coverage
- `rye run tc`
## Inspiration
- Django Templates for the dot-notation inspiration
- A lot of existing JSON-related tools, but especially [`glom`](https://glom.readthedocs.io/), [`jello`](https://github.com/kellyjonbrazil/jello), [`jq`](https://jqlang.github.io/jq/), and [`gron`](https://github.com/TomNomNom/gron); all of which did not quite do what I wanted, but were useful on the journey
Raw data
{
"_id": null,
"home_page": "",
"name": "transformd",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "",
"author": "Adam Hill",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/55/80/ecaddd98fadcbf6e761abc4d291bf99f5559f087cad0d1f234a6176cc4fd/transformd-0.3.0.tar.gz",
"platform": null,
"description": "# transformd\n\nTransform a `dictionary` to another `dictionary`, but keep the same shape based on a `spec`.\n\n## What is a spec?\n\nIt 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.\n\nA `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.\n\nNote: `Specs` are applied to the original `dictionary` in the order they are defined -- the `dictionary` gets updated after each spec.\n\n## Examples\n\n```python\nfrom transformd import Transformer\n\n# Initial `dictionary` we'll transform based on a spec below\ndata = {\n \"library\": {\n \"name\": \"Main St Library\",\n \"location\": {\n \"street\": \"123 Main St\",\n \"city\": \"New York City\",\n \"state\": \"NY\",\n },\n \"books\": [\n {\n \"title\": \"The Grapes of Wrath\",\n \"author\": {\"first_name\": \"John\", \"last_name\": \"Steinbeck\"},\n },\n {\n \"title\": \"Slaughterhouse-Five\",\n \"author\": {\"first_name\": \"Kurt\", \"last_name\": \"Vonnegut\"},\n },\n ],\n }\n}\n\n# Only return a nested part of `data`\nassert Transformer(data).transform(spec=\"library.name\") == {\n \"library\": {\n \"name\": \"Main St Library\"\n }\n}\n\n# Return multiple parts of `data`\nassert Transformer(data).transform(spec=(\"library.name\", \"library.location.state\")) == {\n \"library\": {\n \"name\": \"Main St Library\",\n \"location\": {\n \"state\": \"NY\"\n },\n }\n}\n\n# Return different parts of a nested list in `data`\nassert Transformer(data).transform(spec=(\"library.books.0.title\", \"library.books.1\")) == {\n \"library\": {\n \"books\": [\n {\n \"title\": \"The Grapes of Wrath\",\n },\n {\n \"title\": \"Slaughterhouse-Five\",\n \"author\": {\"first_name\": \"Kurt\", \"last_name\": \"Vonnegut\"},\n },\n ],\n }\n}\n\n# Exclude pieces from `data` by prefixing a spec with a dash\nassert Transformer(data).transform(spec=(\"-library.books\", \"-library.location\")) == {\n \"library\": {\n \"name\": \"Main St Library\"\n }\n}\n```\n\n## Why?\n\nI needed this functionality for [`Unicorn`](https://www.django-unicorn.com), but could not find a suitable library. After writing the code, I thought maybe it would be useful for someone else. \ud83e\udd37\n\n## Run tests\n\n- Install [rye](https://rye-up.com)\n- `rye sync`\n- `rye run t`\n\n### Test Coverage\n\n- `rye run tc`\n\n## Inspiration\n\n- Django Templates for the dot-notation inspiration\n- A lot of existing JSON-related tools, but especially [`glom`](https://glom.readthedocs.io/), [`jello`](https://github.com/kellyjonbrazil/jello), [`jq`](https://jqlang.github.io/jq/), and [`gron`](https://github.com/TomNomNom/gron); all of which did not quite do what I wanted, but were useful on the journey\n",
"bugtrack_url": null,
"license": "",
"summary": "Transform dictionaries into whatever your heart desires (as long as it's another dictionary that kind of looks like the original dictionary)",
"version": "0.3.0",
"project_urls": {
"Homepage": "https://github.com/adamghill/transformd"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c7a4375ce3ff7979d91d10b0e6bf67ba50e694b1172e3c3272c1ae0a31924a6b",
"md5": "76e27ad4520fac29d5e450195f57b94c",
"sha256": "7ca14b3dc2a5d37e6b8a5a0434ce56eecaa7bc25def2c203bbd0bc85a6a68944"
},
"downloads": -1,
"filename": "transformd-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "76e27ad4520fac29d5e450195f57b94c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 6254,
"upload_time": "2023-12-04T03:52:50",
"upload_time_iso_8601": "2023-12-04T03:52:50.149515Z",
"url": "https://files.pythonhosted.org/packages/c7/a4/375ce3ff7979d91d10b0e6bf67ba50e694b1172e3c3272c1ae0a31924a6b/transformd-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5580ecaddd98fadcbf6e761abc4d291bf99f5559f087cad0d1f234a6176cc4fd",
"md5": "6680fb6de71e10e1e6c888045d49e34f",
"sha256": "8be4f5d7a1dfdf8d80ee4adf2d415ad8e6b298f22389147fa778ccb52593836a"
},
"downloads": -1,
"filename": "transformd-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "6680fb6de71e10e1e6c888045d49e34f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 6510,
"upload_time": "2023-12-04T03:52:51",
"upload_time_iso_8601": "2023-12-04T03:52:51.626607Z",
"url": "https://files.pythonhosted.org/packages/55/80/ecaddd98fadcbf6e761abc4d291bf99f5559f087cad0d1f234a6176cc4fd/transformd-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-04 03:52:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "adamghill",
"github_project": "transformd",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "transformd"
}