<p align="center">
<a href="https://erdogant.github.io/dicter/">
<img src="https://github.com/erdogant/dicter/blob/main/docs/figs/logo.png" width="400" />
</a>
</p>
[](https://img.shields.io/pypi/pyversions/dicter)
[](https://pypi.org/project/dicter/)
[](https://erdogant.github.io/dicter/)
[](https://github.com/erdogant/dicter/)
[](https://pepy.tech/project/dicter)
[](https://pepy.tech/project/dicter)
[](https://github.com/erdogant/dicter/blob/master/LICENSE)
[](https://github.com/erdogant/dicter/network)
[](https://github.com/erdogant/dicter/issues)
[](http://www.repostatus.org/#active)
[](https://zenodo.org/badge/latestdoi/584101058)


[](https://erdogant.github.io/dicter/pages/html/Documentation.html#)
<!---[](https://www.buymeacoffee.com/erdogant)-->
<!---[](https://erdogant.github.io/donate/?currency=USD&amount=5)-->
* ``dicter`` is 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
* [**dicter documentation pages (Sphinx)**](https://erdogant.github.io/dicter/)
## 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:
```bash
pip install -U dicter
```
* Alternatively, you can install from the GitHub source:
```bash
# Directly install from github source
pip install git+https://github.com/erdogant/dicter
```
## Examples
#### Import dicter package
```python
import dicter as dt
```
#### Traverse all paths in dictionary.
```python
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.
```python
# 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.
```python
# 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.
```python
# 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.
```python
# 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.
```python
# 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.
```python
# 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)
```
## General
#### References
* https://github.com/erdogant/dicter
#### Citation
Please cite in your publications if this is useful for your research (see citation).
### Maintainers
* Erdogan Taskesen, github: [erdogant](https://github.com/erdogant)
### Contribute
* All kinds of contributions are welcome!
* If you wish to buy me a <a href="https://www.buymeacoffee.com/erdogant">Coffee</a> for this work, it is very appreciated :)
### Licence
See [LICENSE](LICENSE) for details.
Raw data
{
"_id": null,
"home_page": "https://github.com/erdogant/dicter",
"name": "dicter",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3",
"maintainer_email": null,
"keywords": null,
"author": "Erdogan Taskesen",
"author_email": "erdogant@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a1/10/c0775c285ba67b26d40911efa4ba5dee125e7b1f3ce0825c84d529d35b57/dicter-0.1.6.tar.gz",
"platform": null,
"description": "\r\n<p align=\"center\">\r\n <a href=\"https://erdogant.github.io/dicter/\">\r\n <img src=\"https://github.com/erdogant/dicter/blob/main/docs/figs/logo.png\" width=\"400\" />\r\n </a>\r\n</p>\r\n\r\n\r\n[](https://img.shields.io/pypi/pyversions/dicter)\r\n[](https://pypi.org/project/dicter/)\r\n[](https://erdogant.github.io/dicter/)\r\n[](https://github.com/erdogant/dicter/)\r\n[](https://pepy.tech/project/dicter)\r\n[](https://pepy.tech/project/dicter)\r\n[](https://github.com/erdogant/dicter/blob/master/LICENSE)\r\n[](https://github.com/erdogant/dicter/network)\r\n[](https://github.com/erdogant/dicter/issues)\r\n[](http://www.repostatus.org/#active)\r\n[](https://zenodo.org/badge/latestdoi/584101058)\r\n\r\n\r\n[](https://erdogant.github.io/dicter/pages/html/Documentation.html#)\r\n<!---[](https://www.buymeacoffee.com/erdogant)-->\r\n<!---[](https://erdogant.github.io/donate/?currency=USD&amount=5)-->\r\n\r\n\r\n* ``dicter`` is Python package with advanced dictionary functions:\r\n\r\n\t* Traverse through nested dicts to retrieve key-path.\r\n\t* Set value in dictionary using key-path\r\n\t* Get value in dictionary using key-path.\r\n\t* Flattens dicts.\r\n\t* Compare two dicts.\r\n\t* Store and load in json.\r\n\r\n\r\n\r\n# \r\n**Star this repo if you like it! \u2b50\ufe0f**\r\n#\r\n\r\n## Documentation\r\n\r\n* [**dicter documentation pages (Sphinx)**](https://erdogant.github.io/dicter/)\r\n\r\n\r\n## Installation\r\n* Install dicter from PyPI (recommended). dicter is compatible with Python 3.6+ and runs on Linux, MacOS X and Windows. \r\n* A new environment can be created as following:\r\n\r\n\r\n```bash\r\npip install -U dicter\r\n```\r\n\r\n* Alternatively, you can install from the GitHub source:\r\n```bash\r\n# Directly install from github source\r\npip install git+https://github.com/erdogant/dicter\r\n```\r\n\r\n## Examples\r\n\r\n#### Import dicter package\r\n```python\r\nimport dicter as dt\r\n```\r\n\r\n#### Traverse all paths in dictionary.\r\n```python\r\nimport dicter as dt\r\n # Example dict:\r\nd = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}\r\n# Walk through dict to get all paths\r\npaths = dt.traverse(d)\r\n\r\nprint(paths)\r\n# [[['level_a'], 1],\r\n# [['level_c'], 3],\r\n# [['level_e'], 2],\r\n# [['level_b', 'a'], 'hello world'],\r\n# [['level_d', 'a'], 1],\r\n# [['level_d', 'b'], 2],\r\n# [['level_d', 'c', 'e'], 10]]\r\n```\r\n\r\n#### Get value from dictionary using nested keys.\r\n```python\r\n# Import dicter\r\nimport dicter as dt\r\n\r\n# Example dictionary\r\nd = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}\r\n# Get the value for the nested path for:\r\nvalue = dt.get_nested(d, key_path=[\"level_b\", \"a\"])\r\nprint(value)\r\n# 'hello world'\r\n\r\n```\r\n\r\n#### Set value from dictionary using nested keys.\r\n```python\r\n# Import dicter\r\nimport dicter as dt\r\n\r\n# Example: New path and value in dictionary.\r\nd = {}\r\nkey_path = ['person', 'address', 'city']\r\ndt.set_nested(d, key_path, 'New York')\r\n# Print updated dictionary\r\nprint(d)\r\n# {'person': {'address': {'city': 'New York'}}}\r\n\r\n```\r\n\r\n#### Set value from dictionary using nested keys.\r\n```python\r\n# Import dicter\r\nimport dicter as dt\r\n\r\n# Example dict\r\nd = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}\r\n# Flatten dictionary\r\ndflat = dt.flatten(d)\r\n\r\nprint(d_flat)\r\n\r\n# [['level_a', 1],\r\n# ['a', 'hello world'],\r\n# ['level_c', 3],\r\n# ['a', 1],\r\n# ['b', 2],\r\n# ['e', 10],\r\n# ['level_e', 2]]\r\n \r\n```\r\n\r\n\r\n#### Depth of dictionary.\r\n```python\r\n# Import dicter\r\nimport dicter as dt\r\n\r\nd = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}\r\nn = dt.depth(d)\r\n\r\n```\r\n\r\n#### Compare dictionary.\r\n```python\r\n# Import dicter\r\nimport dicter as dt\r\n\r\nExample: Add\r\nd1 = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 'new in d2'}\r\nd2 = {'level_a': 1, 'level_b': {'a': 'hello world'}}\r\nout = dt.compare(d1, d2)\r\nprint(out)\r\n\r\nExample: Remove\r\nd1 = {'level_a': 1, 'level_b': {'a': 'hello world'}}\r\nd2 = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 'new in d2'}\r\nout = dt.compare(d1, d2)\r\nprint(out)\r\n\r\nExample: Modified\r\nd1 = {'level_a': 1, 'level_b': {'a': 'hello world'}}\r\nd2 = {'level_a': 1, 'level_b': {'a': 'modified'}}\r\nout = dt.compare(d1, d2)\r\nprint(out['modified'])\r\n\r\n```\r\n\r\n#### Save and load dictionary.\r\n```python\r\n\r\n# Import dicter\r\nimport dicter as dt\r\n\r\nd = {'level_a': None, 'level_b': {'a': 'hello world'}, 'level_c': True, 'level_d': 2.3, 'level_e': [[1,2,3], [1,2]]}\r\nfilepath='c:/temp/test/dicter_save.json'\r\n\r\n# First save\r\ndt.save(d, filepath=filepath, overwrite=True)\r\n\r\n# Load\r\nd = dt.load(filepath)\r\n```\r\n\r\n## General\r\n\r\n#### References\r\n* https://github.com/erdogant/dicter\r\n\r\n#### Citation\r\nPlease cite in your publications if this is useful for your research (see citation).\r\n \r\n### Maintainers\r\n* Erdogan Taskesen, github: [erdogant](https://github.com/erdogant)\r\n\r\n### Contribute\r\n* All kinds of contributions are welcome!\r\n* If you wish to buy me a <a href=\"https://www.buymeacoffee.com/erdogant\">Coffee</a> for this work, it is very appreciated :)\r\n\r\n### Licence\r\nSee [LICENSE](LICENSE) for details.\r\n",
"bugtrack_url": null,
"license": null,
"summary": "dicter is a Python package with advanced dictionary functions.",
"version": "0.1.6",
"project_urls": {
"Download": "https://github.com/erdogant/dicter/archive/0.1.6.tar.gz",
"Homepage": "https://github.com/erdogant/dicter"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "137de8f0f39ec7d9ab43d3e4b068e5549070d8168477f3e92688187c980f5942",
"md5": "1a35366ff48f7a2ee29f1d6a0e43d9dd",
"sha256": "0ed07600ccc0bf4bebe2f241b60ef0c288fd11280fd00c736cf00feb70410794"
},
"downloads": -1,
"filename": "dicter-0.1.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1a35366ff48f7a2ee29f1d6a0e43d9dd",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3",
"size": 10168,
"upload_time": "2024-05-09T19:11:31",
"upload_time_iso_8601": "2024-05-09T19:11:31.262117Z",
"url": "https://files.pythonhosted.org/packages/13/7d/e8f0f39ec7d9ab43d3e4b068e5549070d8168477f3e92688187c980f5942/dicter-0.1.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a110c0775c285ba67b26d40911efa4ba5dee125e7b1f3ce0825c84d529d35b57",
"md5": "88aff2771c77e2140b239dbc887438f3",
"sha256": "9df64d4f7c522449041ccd5265fd61cbf0f6bb3e9384ce8b7e23fa58f0f8a78f"
},
"downloads": -1,
"filename": "dicter-0.1.6.tar.gz",
"has_sig": false,
"md5_digest": "88aff2771c77e2140b239dbc887438f3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3",
"size": 11202,
"upload_time": "2024-05-09T19:11:32",
"upload_time_iso_8601": "2024-05-09T19:11:32.404260Z",
"url": "https://files.pythonhosted.org/packages/a1/10/c0775c285ba67b26d40911efa4ba5dee125e7b1f3ce0825c84d529d35b57/dicter-0.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-09 19:11:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "erdogant",
"github_project": "dicter",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "dicter"
}