-
dicteris Python package with advanced dictionary functions:- Traverse through nested dicts to retrieve key-path.
- Set value in dictionary using key-path
- Get value in dictionary using key-path.
- Flattens dicts.
- Compare two dicts.
- Store and load in json.
Star this repo if you like it! ⭐️
Documentation
Installation
- Install dicter from PyPI (recommended). dicter is compatible with Python 3.6+ and runs on Linux, MacOS X and Windows.
- A new environment can be created as following:
pip install -U dicter
- Alternatively, you can install from the GitHub source:
# Directly install from github source
pip install git+https://github.com/erdogant/dicter
Examples
Import dicter package
import dicter as dt
Traverse all paths in dictionary.
import dicter as dt
# Example dict:
d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}
# Walk through dict to get all paths
paths = dt.traverse(d)
print(paths)
# [[['level_a'], 1],
# [['level_c'], 3],
# [['level_e'], 2],
# [['level_b', 'a'], 'hello world'],
# [['level_d', 'a'], 1],
# [['level_d', 'b'], 2],
# [['level_d', 'c', 'e'], 10]]
Get value from dictionary using nested keys.
# Import dicter
import dicter as dt
# Example dictionary
d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}
# Get the value for the nested path for:
value = dt.get_nested(d, key_path=["level_b", "a"])
print(value)
# 'hello world'
Set value from dictionary using nested keys.
# Import dicter
import dicter as dt
# Example: New path and value in dictionary.
d = {}
key_path = ['person', 'address', 'city']
dt.set_nested(d, key_path, 'New York')
# Print updated dictionary
print(d)
# {'person': {'address': {'city': 'New York'}}}
Set value from dictionary using nested keys.
# Import dicter
import dicter as dt
# Example dict
d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}
# Flatten dictionary
dflat = dt.flatten(d)
print(d_flat)
# [['level_a', 1],
# ['a', 'hello world'],
# ['level_c', 3],
# ['a', 1],
# ['b', 2],
# ['e', 10],
# ['level_e', 2]]
Depth of dictionary.
# Import dicter
import dicter as dt
d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}
n = dt.depth(d)
Compare dictionary.
# Import dicter
import dicter as dt
Example: Add
d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 'new in d2'}
d2 = {'level_a': 1, 'level_b': {'a': 'hello world'}}
out = dt.compare(d1, d2)
print(out)
Example: Remove
d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}}
d2 = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 'new in d2'}
out = dt.compare(d1, d2)
print(out)
Example: Modified
d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}}
d2 = {'level_a': 1, 'level_b': {'a': 'modified'}}
out = dt.compare(d1, d2)
print(out['modified'])
Save and load dictionary.
# Import dicter
import dicter as dt
d = {'level_a': None, 'level_b': {'a': 'hello world'}, 'level_c': True, 'level_d': 2.3, 'level_e': [[1,2,3], [1,2]]}
filepath='c:/temp/test/dicter_save.json'
# First save
dt.save(d, filepath=filepath, overwrite=True)
# Load
d = dt.load(filepath)
Citation
Please cite in your publications if this is useful for your research (see citation).
☕ Support
If you find this project useful, consider supporting me:
Metadata
Release files for dicter 0.1.7
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 |
|---|---|---|---|---|
| dicter-0.1.7.tar.gz | 12.1 kB | Without provenance | Details for dicter-0.1.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dicter-0.1.7-py3-none-any.whl | Python 3 | none | any | Details for dicter-0.1.7-py3-none-any.whl |
Total release size: 23.3 kB
Release files / dicter-0.1.7.tar.gz
| Download URL | dicter-0.1.7.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3cafd881d73beebaac236492165111537e9e613f62a566adbb3c854ad351528d
|
|
BLAKE2b-256 checksum How to use checksums |
f37e8d7d06a8638163825b150b56f022624d12d978af722126114e9daafadfab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / dicter-0.1.7-py3-none-any.whl
| Download URL | dicter-0.1.7-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5edd0adf79cd4a75aa220f1b23d7d4aec3e59bdec95a75f13d1d9f0d05351705
|
|
BLAKE2b-256 checksum How to use checksums |
342ba2d7038bdb8b0771b725ebdfd81b34b928e53a0f7346cfa3b676736eaae9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for dicter 0.1.7
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.