https://github.com/lucacappelletti94/deflate_dict
Python package to deflate and re-inflate dictionaries.
https://github.com/lucacappelletti94/deflate_dict
Last synced: 8 months ago
JSON representation
Python package to deflate and re-inflate dictionaries.
- Host: GitHub
- URL: https://github.com/lucacappelletti94/deflate_dict
- Owner: LucaCappelletti94
- License: mit
- Created: 2019-05-26T05:48:20.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-26T16:36:27.000Z (over 1 year ago)
- Last Synced: 2025-06-02T19:18:43.784Z (9 months ago)
- Language: Python
- Homepage:
- Size: 130 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 🎈🌵 Deflate Dict
[](https://pypi.org/project/deflate-dict/)
[](https://pypi.org/project/deflate-dict/)
[](https://pypi.org/project/deflate-dict/)
[](https://pepy.tech/project/deflate-dict)
[](https://github.com/LucaCappelletti94/deflate_dict/actions/)
[](https://github.com/LucaCappelletti94/deflate_dict/actions/)
Python package to deflate 🌵 and re-inflate 🎈 nested dictionaries.
## How do I install this package?
As usual, just download it using pip:
```shell
pip install deflate_dict
```
## Deflating a dictionary
A dictionary will be deflated down to its smallest non-further iterable elements, defined as those not containing lists or dictionaries.
```python
from deflate_dict import deflate
D = {
"a": [
{
"b": (0, 1, 2)
},
{
"c": [1, 2, 3]
}
]
}
result = deflate(D, sep="_")
# {'str(a)_listIndex(0)_str(b)': (0, 1, 2), 'str(a)_listIndex(1)_str(c)': [1, 2, 3]}
```
## Inflate a dictionary
To reinflate a dictionary to its forgotten glory, just go with:
```python
from deflate_dict import inflate
D = {"a_0_b": (0, 1, 2), "a_1_c": [1, 2, 3], "d": 4}
result = inflate(D, sep="_")
# {'a': [{'b': (0, 1, 2)}, {'c': [1, 2, 3]}], 'd': 4}
```
## Handling and restoring mixed types
To deflate and re-inflate mixed types and restore them to the original type you can use the `type_encode_key` keyword:
```python
from deflate_dict import deflate
D = {
"a":[
{
"b":(0,1,2)
},
{
"c": [1,2,3]
}
]
}
print(deflate(D, sep="_", type_encode_key=False))
# {
# 'a_listIndex(0)_b': (0, 1, 2),
# 'a_listIndex(1)_c': [1, 2, 3]
# }
print(deflate(D, sep="_", type_encode_key=True))
# {
# 'str(a)_listIndex(0)_str(b)': (0, 1, 2),
# 'str(a)_listIndex(1)_str(c)': [1, 2, 3]
# }
```
## License
This software is distributed under MIT license.