Restructure dictionary values by key
Project description
Dictionary Restructure Tool
Project
This project implements in Python a function called restructure():
def restructure(data: dict, specification: dict):
...
It is useful to restructure where keys are in a dictionary, for example to upgrade a configuration file to a new schema.
Usage
from restructure import restructure
Moving Keys
To move a nested dictionary to the top-level:
input_data = {
'key1': {
'key2': {
'key3': 'value',
},
},
}
specification = {
'key1.key2.key3': 'key',
}
output = restructure(input_data, specification)
assert output == {
'key': 'value',
}
or the opposite:
input_data = {
'key': 'value',
}
specification = {
'key': 'key1.key2.key3',
}
output = restructure(input_data, specification)
assert output == {
'key1': {
'key2': {
'key3': 'value',
},
}
}
Or to swap keys:
input_data = {
'key1': {
'key2': 'value1',
},
'key3': {
'key4': 'value2',
},
}
specification = {
'key1.key2': 'key3.key4',
'key3.key4': 'key1.key2',
}
output = restructure(input_data, specification)
assert output == {
'key1': {
'key2': 'value2',
},
'key3': {
'key4': 'value1',
},
}
Copying Keys
Keys can be copied using sets of key-paths:
input_data = {
'key1': {
'key2': {
'key3': 'value1',
},
},
}
specification = {
'key1.key2.key3': {'key1.key2.key3.key4', 'key1.key2.key5', 'key1.key6', 'key7'},
}
output = restructure(input_data, specification)
assert output == {
'key1': {
'key2': {
'key3': {
'key4': 'value1',
},
'key5': 'value1',
},
'key6': 'value1',
},
'key7': 'value1',
}
Merging Keys
Keys which contain dictionaries or equivalent values can be merged:
input_data = {
'key1': {
'key2': {
'key3': 'value1',
},
},
'key4': {
'key5': 'value2',
},
'key6': 'value1',
}
specification = {
'key4': 'key1.key2',
'key6': 'key1.key2.key3',
}
output = restructure(input_data, specification)
assert output == {
'key1': {
'key2': {
'key3': 'value1',
'key5': 'value2',
},
},
}
Removing Keys
Keys can be removed by providing None or an empty string:
input_data = {
'key1': {
'key2': 'value1',
'key3': 'value2',
'key4': 'value3',
},
}
specification = {
'key1.key3': None,
'key1.key4': '',
}
output = restructure(input_data, specification)
assert output == {
'key1': {
'key2': 'value1',
},
}
For Developers
- Follows Semantic Versioning 2.0.0
- Follows this package structure
Testing
To run unit tests, run the following command from the project root directory:
python -m unittest
Packaging
Before packaging, update the version number in pyproject.toml
To package & upload the project, run the following commands from the project root directory:
python -m build
python -m twine upload dist/*
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
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 restructure-0.2.0.tar.gz.
File metadata
- Download URL: restructure-0.2.0.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d2cb4942b8a4dabc8a388dbfba6e348a16cc32790a4bd340564ca3dca4c0b42
|
|
| MD5 |
ddae38980818df365a65d97a3d93befb
|
|
| BLAKE2b-256 |
2f8b0a4d4802b394f652e0f64ca1154c78a6ea7d20a315ae33ab3b6c6ad57e1c
|
File details
Details for the file restructure-0.2.0-py3-none-any.whl.
File metadata
- Download URL: restructure-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62981af6a8bbeb3d9d00639207b5502e7610f4e2835fc5af3f68fbb66b23434e
|
|
| MD5 |
2bba92f06a1fa8b7fee96be97dbcea74
|
|
| BLAKE2b-256 |
82166849a9a49a4fbe73fd8643ca15d64ebbcbb21c5afc0ca80f23e14e38bb08
|