Dotli
Yet another library for flattening structures
Installation
pip install dotli
Usage
flatten
from dotli import Dotli
data = {
'a': {
'c': 'val1',
'd': 'val2',
},
'b': {
'c': 2,
'd': {'key3': 'val3', 'key4': 'val4'},
}
}
d = Dotli() # create an obj with a default configuration
flat = d.flatten(data) # flatten
orig = d.unflatten(flat) # unflatten
assert data == orig
print(flat)
{
'a.c': 'val1',
'a.d': 'val2',
'b.c': 2,
'b.d.key3': 'val3',
'b.d.key4': 'val4',
}
The separator can be configured
data = {
'a': {'c': 'd'},
'b': {'e': 'f'}
}
d = Dotli(separator='-')
print(d.flatten(data))
{
'a-c': 'd',
'b-e': 'f',
}
It is also possible to flatten lists and a mixture of lists and dicts
data = {
'a': {
'c': [1, 2, 3],
'd': ['e1', 'e2', 'e3'],
},
'b': 'h'
}
d = Dotli()
print(d.flatten(data))
{
'a.c.0': 1,
'a.c.1': 2,
'a.c.2': 3,
'a.d.0': 'e1',
'a.d.1': 'e2',
'a.d.2': 'e3',
'b': 'h',
}
List indices can be wrapped in square brackets to allow numerical strings in dicts as keys
data = {
'a': {
'1': [1, 2, 3],
'2': ['e1', 'e2', 'e3'],
},
'b': 'h'
}
d = Dotli(list_brackets=True)
flat = d.flatten(data)
orig = d.unflatten(flat)
assert data == orig
print(flat)
{
'a.1.[0]': 1,
'a.1.[1]': 2,
'a.1.[2]': 3,
'a.2.[0]': 'e1',
'a.2.[1]': 'e2',
'a.2.[2]': 'e3',
'b': 'h',
}
There will be a nice error message if the dict can not be flattened including the path to the invalid element.
from dotli.errors import SeparatorInKeyError
data = {
'a': {
'b.b': 1,
},
'b': 'h'
}
try:
Dotli().flatten(data)
except SeparatorInKeyError as e:
print(e)
Separator "." is in key "b.b"! @ root.a
unflatten
When elements are missing in a list Dotli will throw an error
from dotli.errors import IncompleteListError
data = {
'a.0': 0,
'a.2': 2,
}
try:
d = Dotli()
d.unflatten(data)
except IncompleteListError as e:
print(e)
No entry for index "1" in list! @ a
However it is possible to specify a fill value which will automatically be inserted into the list for missing entries
data = {
'a.0': 0,
'a.2': 2,
}
d = Dotli(fill_value_list=None)
print(d.unflatten(data))
{
'a': [0, None, 2],
}
Metadata
Release files for dotli 1.1
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 | Actions |
|---|---|---|---|---|
| dotli-1.1.tar.gz | 5.0 kB | Without provenance | Details for dotli-1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dotli-1.1-py3-none-any.whl | Python 3 | none | any | Details for dotli-1.1-py3-none-any.whl |
Total release size: 13.2 kB
Release files / dotli-1.1.tar.gz
| Download URL | dotli-1.1.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0491bac4aa428a8ee611adb9cbf8a3358e21213de1e6231b5048beefdb726113
|
|
BLAKE2b-256 checksum How to use checksums |
a450a6159e01f605228cb7c649c3af5c7ae3ae8090663a5b587b4d1e5bfcffa5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4
|
Release files / dotli-1.1-py3-none-any.whl
| Download URL | dotli-1.1-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e029fba79d0550916d64ca923a5f83cf52ee96a428ed378a66756b07c6532dfb
|
|
BLAKE2b-256 checksum How to use checksums |
d1e6932a8b420ab454c3722af7de98f6704aba9e3236090f9ef36ed05c22d1bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4
|
Security for dotli 1.1
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.