dictmagic
dictmagic is a very small python 3 package that makes it easier to do certain weird things with dictionaries.
At the moment, it lets you convert to and from "flattened" dictionaries that use path syntax instead of nested dictionaries, and simplifies the process of accessing python dictionary elements as attributes. This package is mostly written to loading, saving, using, and coverting between various configuration methods easier.
installation
pip install dictmagic
Usage
using dictmagic.paths
flattening and unflattening hierarchies of dicts lets you access mydict['hello']['world'] with mydict['hello/world'] or vice versa. This is useful in contexts where having nested dicts is not practical (making hash maps that can be used in strictly typed languages, storing data in hdf5 attributes, etc)
dictmagic.paths.flatten()ordictmagic.flatten()dictmagic.paths.unflatten()ordictmagic.unflatten()
Overview
(identical for both functions)
-
in_dict: input dictionary. not modified -
sep: section separator (any char can be used,/is default)/in the style of unix, hdf5, and other things:in the style ofconfigparserfrom the standard library.in the style of YAML
using paths.flatten()
-
Args:
in_dict(dict): input (nested) dictionarysep(str, optional): separator between nested dicts. Defaults to '/'.except_nonstr_key(bool, optional): if True, raise exception if non-string key found. If False, converts key to string and proceeds. Defaults to True.
-
Returns:
dict: flattened dictionary
-
Raises:
TypeError: if non-string key found (including in dup_key_repl)
using paths.unflatten()
-
Args:
in_dict([type]): unput dictionarysep(str, optional): separator between nested paths inin_dict. Defaults to '/'.except_dup_key(bool, optional): if True, raise exception if a duplicate key is found. For behavior if False, see below. Defaults to True.dup_key_repl([type], optional): if exception is not raised, map duplicate to this value: ifDcontains{ 'a' : 'val1', 'a/b' : 'val2' }thenunflatten(D)will have{ 'a' : { dup_key_repl : 'val1', 'b' : 'val2' } }Defaults to None.except_nonstr_key(bool, optional): if True, raise exception if non-string key found. If False, converts key to string and proceeds. Defaults to True.
-
Raises:
KeyError: ifexcept_dup_keyis True, raises key error when duplicate key foundTypeError: if non-string key found (including indup_key_repl)
-
Returns:
dict: nested dictionary
using dictmagic.obj
dictmagic.obj.ObjectDictordictmagic.ObjectDict
this class, taking a single argument data, allows accessing elements of a that dictionary as object.key instead of object['key'].
why is this useful when you can just dump it into object.__dict__ ?
primarily because doing that will make copying your dict rather difficult without ending up with somewhat of a mess.
misc
originally developed by @mivanit for a config manager, then moved to knc-tools for a time. Check out that repo for more weird tools.
PRs and feature requests welcome!
you can find this project on PyPI: pypi.org/project/dictmagic/
building
to build the package: (you probably don't need to do this unless you're uploading a fork to PyPI)
python setup.py sdist bdist_wheel
Metadata
Release files for dictmagic 0.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 |
|---|---|---|---|---|
| dictmagic-0.1.1.tar.gz | 5.4 kB | Without provenance | Details for dictmagic-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dictmagic-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for dictmagic-0.1.1-py3-none-any.whl |
Total release size: 24.0 kB
Release files / dictmagic-0.1.1.tar.gz
| Download URL | dictmagic-0.1.1.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4f3aa6173f4f55f1fce02c9bd98ac70a02863e065ce2b662e3a0d97f4cb45235
|
|
BLAKE2b-256 checksum How to use checksums |
56dcb40ded7b2552ebfd982fa026f0cdf616dba1f8912ef8afa44405a465db70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.22.0 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.55.2 CPython/3.8.0
|
Release files / dictmagic-0.1.1-py3-none-any.whl
| Download URL | dictmagic-0.1.1-py3-none-any.whl |
|---|---|
| Size | 18.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d7f1d0d45713e2e0dff9f21688e7aff992d4bbb709fae16b5c087646d9a54af2
|
|
BLAKE2b-256 checksum How to use checksums |
a42dd203b54c944ac77302062c67644ce093a7cb6f494f26a2b6d617563a47b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.22.0 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.55.2 CPython/3.8.0
|
Security for dictmagic 0.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.