Name | reversibledict JSON |
Version |
0.2.2
JSON |
| download |
home_page | https://github.com/tttthomasssss/reversibledict |
Summary | Dictionary with value-key lookup ability |
upload_time | 2023-05-09 11:23:26 |
maintainer | |
docs_url | None |
author | Thomas Kober |
requires_python | >=3.9 |
license | MIT License Copyright (c) 2017-this very day: Thomas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
dict
reversible-dict
value-lookup
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# ReversibleDict
*~~~ A dictionary being able to perform a reverse lookup ~~~*
## How does it work?
I calculate a hash of the value (or a hash of the value's `__str__` representation for unhashable types - i.e. `lists`) and internally store the reverse mapping, so no linear search through the values if you thought about that.
## What if there is more than 1 value for a key?
I simply return a `list` of all keys that matched.
## Usage
```
>>> from reversibledict import ReversibleDict
>>> r = ReversibleDict()
>>> r['a'] = 1
>>> r['b'] = 2
>>> r['c'] = 1
>>> r
{'a': 1, 'c': 1, 'b': 2}
>>> r.key_for_value(2)
'b'
>>> r.key_for_value(1)
['a', 'c']
>>> r.key_for_value(666) == None
True
```
It can also deal with unhashable types such as `lists`:
```
>>> from reversibledict import ReversibleDict
>>> r = ReversibleDict()
>>> r['a'] = [1,2,3]
>>> r['b'] = [3,2,1]
>>> r['c'] = [1]
>>> r['d'] = [1,2,3]
>>> r
{'a': [1, 2, 3], 'c': [1], 'b': [3, 2, 1], 'd': [1, 2, 3]}
>>> r.key_for_value([1,2,3])
['a', 'd']
>>> r.key_for_value([3,2,1])
'b'
>>> r.key_for_value([]) == None
True
```
If you don't like the inconsistency that it is returning a `list` if there is more than 1 matching value, `None` if there is no matching value and a scalar if there is exactly 1 matching value then you can do the following:
```
>>> from reversibledict import ReversibleDict
>>> r = ReversibleDict(reverse_as_list=True)
>>> r['a'] = 1
>>> r['b'] = 2
>>> r['c'] = 1
>>> r
{'a': 1, 'c': 1, 'b': 2}
>>> r.key_for_value(2)
['b']
>>> r.key_for_value(1)
['a', 'c']
>>> r.key_for_value(666)
[]
```
Raw data
{
"_id": null,
"home_page": "https://github.com/tttthomasssss/reversibledict",
"name": "reversibledict",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": "",
"keywords": "dict reversible-dict value-lookup",
"author": "Thomas Kober",
"author_email": "Thomas Kober <tttthomasssss@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/b6/9c/16afc8722aef0bc162ae866a80630315932cf7e8a9d63fdfe8f692d6a84c/reversibledict-0.2.2.tar.gz",
"platform": null,
"description": "# ReversibleDict\n\n*~~~ A dictionary being able to perform a reverse lookup ~~~*\n\n## How does it work?\n\nI calculate a hash of the value (or a hash of the value's `__str__` representation for unhashable types - i.e. `lists`) and internally store the reverse mapping, so no linear search through the values if you thought about that.\n\n## What if there is more than 1 value for a key?\n\nI simply return a `list` of all keys that matched. \n\n## Usage\n```\n>>> from reversibledict import ReversibleDict\n>>> r = ReversibleDict()\n>>> r['a'] = 1\n>>> r['b'] = 2\n>>> r['c'] = 1\n>>> r\n{'a': 1, 'c': 1, 'b': 2}\n>>> r.key_for_value(2)\n'b'\n>>> r.key_for_value(1)\n['a', 'c']\n>>> r.key_for_value(666) == None\nTrue\n```\n\nIt can also deal with unhashable types such as `lists`:\n\n```\n>>> from reversibledict import ReversibleDict\n>>> r = ReversibleDict()\n>>> r['a'] = [1,2,3]\n>>> r['b'] = [3,2,1]\n>>> r['c'] = [1]\n>>> r['d'] = [1,2,3]\n>>> r\n{'a': [1, 2, 3], 'c': [1], 'b': [3, 2, 1], 'd': [1, 2, 3]}\n>>> r.key_for_value([1,2,3])\n['a', 'd']\n>>> r.key_for_value([3,2,1])\n'b'\n>>> r.key_for_value([]) == None\nTrue\n\n```\n\nIf you don't like the inconsistency that it is returning a `list` if there is more than 1 matching value, `None` if there is no matching value and a scalar if there is exactly 1 matching value then you can do the following:\n\n```\n>>> from reversibledict import ReversibleDict\n>>> r = ReversibleDict(reverse_as_list=True)\n>>> r['a'] = 1\n>>> r['b'] = 2\n>>> r['c'] = 1\n>>> r\n{'a': 1, 'c': 1, 'b': 2}\n>>> r.key_for_value(2)\n['b']\n>>> r.key_for_value(1)\n['a', 'c']\n>>> r.key_for_value(666)\n[]\n```\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2017-this very day: Thomas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "Dictionary with value-key lookup ability",
"version": "0.2.2",
"project_urls": {
"Bug Tracker": "https://github.com/tttthomasssss/reversibledict/issues",
"Homepage": "https://github.com/tttthomasssss/reversibledict"
},
"split_keywords": [
"dict",
"reversible-dict",
"value-lookup"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "57fd04eecffbd75741275ad9f72ef98ec33f87fc78dca395e4904562519cbb20",
"md5": "ef87c3c34245da3630df4471685b1271",
"sha256": "d2bdd573289be1ddd1c4901eae05817e726d215166f7535164b35066c45b2978"
},
"downloads": -1,
"filename": "reversibledict-0.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ef87c3c34245da3630df4471685b1271",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 3687,
"upload_time": "2023-05-09T11:23:25",
"upload_time_iso_8601": "2023-05-09T11:23:25.021280Z",
"url": "https://files.pythonhosted.org/packages/57/fd/04eecffbd75741275ad9f72ef98ec33f87fc78dca395e4904562519cbb20/reversibledict-0.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b69c16afc8722aef0bc162ae866a80630315932cf7e8a9d63fdfe8f692d6a84c",
"md5": "eb66d3c8f96716152d9203049627716d",
"sha256": "fbdb14573b73162c0734b1234a3b1d29d8604403cb850b63268532f8e877c3db"
},
"downloads": -1,
"filename": "reversibledict-0.2.2.tar.gz",
"has_sig": false,
"md5_digest": "eb66d3c8f96716152d9203049627716d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 4277,
"upload_time": "2023-05-09T11:23:26",
"upload_time_iso_8601": "2023-05-09T11:23:26.405998Z",
"url": "https://files.pythonhosted.org/packages/b6/9c/16afc8722aef0bc162ae866a80630315932cf7e8a9d63fdfe8f692d6a84c/reversibledict-0.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-09 11:23:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tttthomasssss",
"github_project": "reversibledict",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "reversibledict"
}