mymulti-key-dict


Namemymulti-key-dict JSON
Version 0.15 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/mymulti_key_dict
SummaryAccess nested dict elements as d[[1,2,3]] instead of d[1][2][3] - compatible with dict() - no requirements
upload_time2023-07-11 06:11:38
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords nested dicts
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Access nested dict elements as d[[1,2,3]] instead of d[1][2][3] - compatible with dict() - no requirements

## pip install mymulti-key-dict 

#### Tested against Windows 10 / Python 3.10 / Anaconda 


The MultiKeyDict class is a special type of dictionary that allows you to use lists 
as keys to access and modify nested elements within the dictionary. 
This feature sets it apart from regular dictionaries and provides additional
flexibility in working with hierarchical data structures.

Using lists as keys in the MultiKeyDict allows you to traverse multiple levels of nesting 
in a single operation. For example, if you have a nested dictionary d and you want to 
access the value at **d\[1\]\[2\]\[3\]**, you can simply pass the list **\[1, 2, 3\]** 
as the key ( **d\[\[1, 2, 3\]\]** )
to retrieve the desired value. This makes it easier to work with complex nested 
structures and eliminates the need for multiple indexing operations.

Furthermore, the MultiKeyDict class provides methods for converting the nested dictionary to 
a normal dictionary, making a deep copy of the dictionary, retrieving items, keys, and values, 
updating the dictionary with another dictionary, clearing the dictionary, and more. 
These methods make it convenient to perform common dictionary operations 
while preserving the nested structure.

By extending the dict class, the MultiKeyDict class inherits the basic dictionary functionality 
and adds the ability to handle nested keys. 
It also overrides certain methods, such as **\_\_getitem\_\_**, **\_\_setitem\_\_**, **\_\_delitem\_\_**, and others, 
to enable the list key functionality and provide the expected behavior for accessing 
and modifying nested elements.


```python

from mymulti_key_dict import MultiKeyDict
dict2 = {2: {"c": 222}, 3: {"d": {3, 6}}}
d = MultiKeyDict(dict2)

d[[1, 3, 4, 5, 67]] = 100
print(d[[1, 3]])
dd = {2: {"c": 222}, 3: {"d": {3, 6}}}
print(f"{list(d)=}")
print(f"{len(d)=}")
print(f"{d[1]=}")
print(f"{d[1][3]=}")
print(f"{d[[1,3]]=}")
d[[23, 4, 5, 323]] = "x"
print(f"""d[[23,4,5,323]] = 'x'={d}""")
print(f"{23 in d=}")
del d[[1, 3]]
print(f"""del d[[1,3]]={d}""")
del d[1]
print(f"""del d[1]={d}""")
di2 = d.copy()
print(f"{di2 == d=}")
print(f"{di2 is d=}")
di2.clear()
print(f"""di2.clear()={di2}""")
print(f"{list(iter(d))=}")
print(f"{d.get(2)=}")
print(f"{d.get([23,4,5])=}")
print(f"{d.items()=}")
print(f"{d.keys()=}")
print(f"{d.pop(3)=}")
print(f"{d.pop([23,4,5])=}")
print(f"""{d.popitem()=}""")
print(f"""after d.popitem={d}""")
dict2 = {2: {"c": 222}, 3: {"d": {3, 6}}, 4: 3, 33: {33: 2}}
d = MultiKeyDict(dict2)
print(f"""{list(d.reversed())=}""")
d.update({4: {44: 4}})
print(f"""d.update...={d}""")
d5 = d | {3: 4}
d |= {3: 4}
print(f"""d |= {{3:4}}={d}""")
print(f'{d.to_dict()=}')





{4: {5: {67: 100}}}
list(d)=[2, 3, 1]
len(d)=3
d[1]={3: {4: {5: {67: 100}}}}
d[1][3]={4: {5: {67: 100}}}
d[[1,3]]={4: {5: {67: 100}}}
d[[23,4,5,323]] = 'x'={1: {3: {4: {5: {67: 100}}}},
 2: {'c': 222},
 3: {'d': {3,
           6}},
 23: {4: {5: {323: 'x'}}}}
23 in d=True
del d[[1,3]]={1: {},
 2: {'c': 222},
 3: {'d': {3,
           6}},
 23: {4: {5: {323: 'x'}}}}
del d[1]={2: {'c': 222},
 3: {'d': {3,
           6}},
 23: {4: {5: {323: 'x'}}}}
di2 == d=True
di2 is d=False
di2.clear()={}
list(iter(d))=[2, 3, 23]
d.get(2)={'c': 222}
d.get([23,4,5])={323: 'x'}
d.items()=dict_items([(2, {'c': 222}), (3, {'d': {3, 6}}), (23, {4: {5: {323: 'x'}}})])
d.keys()=dict_keys([2, 3, 23])
d.pop(3)={'d': {3, 6}}
d.pop([23,4,5])={323: 'x'}
d.popitem()=(2, {'c': 222})
after d.popitem={23: {4: {}}}
list(d.reversed())=[33, 4, 3, 2]
d.update...={2: {'c': 222},
 3: {'d': {3,
           6}},
 4: {44: 4},
 33: {33: 2}}
d |= {3:4}={2: {'c': 222},
 3: 4,
 4: {44: 4},
 33: {33: 2}}
d.to_dict()={2: {'c': 222}, 3: 4, 4: {44: 4}, 33: {33: 2}}

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/mymulti_key_dict",
    "name": "mymulti-key-dict",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "nested,dicts",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c3/0a/aa0f08c8dceffa9238175f504c1411a8fd78f832e3a974e96d195261a466/mymulti_key_dict-0.15.tar.gz",
    "platform": null,
    "description": "\r\n# Access nested dict elements as d[[1,2,3]] instead of d[1][2][3] - compatible with dict() - no requirements\r\n\r\n## pip install mymulti-key-dict \r\n\r\n#### Tested against Windows 10 / Python 3.10 / Anaconda \r\n\r\n\r\nThe MultiKeyDict class is a special type of dictionary that allows you to use lists \r\nas keys to access and modify nested elements within the dictionary. \r\nThis feature sets it apart from regular dictionaries and provides additional\r\nflexibility in working with hierarchical data structures.\r\n\r\nUsing lists as keys in the MultiKeyDict allows you to traverse multiple levels of nesting \r\nin a single operation. For example, if you have a nested dictionary d and you want to \r\naccess the value at **d\\[1\\]\\[2\\]\\[3\\]**, you can simply pass the list **\\[1, 2, 3\\]** \r\nas the key ( **d\\[\\[1, 2, 3\\]\\]** )\r\nto retrieve the desired value. This makes it easier to work with complex nested \r\nstructures and eliminates the need for multiple indexing operations.\r\n\r\nFurthermore, the MultiKeyDict class provides methods for converting the nested dictionary to \r\na normal dictionary, making a deep copy of the dictionary, retrieving items, keys, and values, \r\nupdating the dictionary with another dictionary, clearing the dictionary, and more. \r\nThese methods make it convenient to perform common dictionary operations \r\nwhile preserving the nested structure.\r\n\r\nBy extending the dict class, the MultiKeyDict class inherits the basic dictionary functionality \r\nand adds the ability to handle nested keys. \r\nIt also overrides certain methods, such as **\\_\\_getitem\\_\\_**, **\\_\\_setitem\\_\\_**, **\\_\\_delitem\\_\\_**, and others, \r\nto enable the list key functionality and provide the expected behavior for accessing \r\nand modifying nested elements.\r\n\r\n\r\n```python\r\n\r\nfrom mymulti_key_dict import MultiKeyDict\r\ndict2 = {2: {\"c\": 222}, 3: {\"d\": {3, 6}}}\r\nd = MultiKeyDict(dict2)\r\n\r\nd[[1, 3, 4, 5, 67]] = 100\r\nprint(d[[1, 3]])\r\ndd = {2: {\"c\": 222}, 3: {\"d\": {3, 6}}}\r\nprint(f\"{list(d)=}\")\r\nprint(f\"{len(d)=}\")\r\nprint(f\"{d[1]=}\")\r\nprint(f\"{d[1][3]=}\")\r\nprint(f\"{d[[1,3]]=}\")\r\nd[[23, 4, 5, 323]] = \"x\"\r\nprint(f\"\"\"d[[23,4,5,323]] = 'x'={d}\"\"\")\r\nprint(f\"{23 in d=}\")\r\ndel d[[1, 3]]\r\nprint(f\"\"\"del d[[1,3]]={d}\"\"\")\r\ndel d[1]\r\nprint(f\"\"\"del d[1]={d}\"\"\")\r\ndi2 = d.copy()\r\nprint(f\"{di2 == d=}\")\r\nprint(f\"{di2 is d=}\")\r\ndi2.clear()\r\nprint(f\"\"\"di2.clear()={di2}\"\"\")\r\nprint(f\"{list(iter(d))=}\")\r\nprint(f\"{d.get(2)=}\")\r\nprint(f\"{d.get([23,4,5])=}\")\r\nprint(f\"{d.items()=}\")\r\nprint(f\"{d.keys()=}\")\r\nprint(f\"{d.pop(3)=}\")\r\nprint(f\"{d.pop([23,4,5])=}\")\r\nprint(f\"\"\"{d.popitem()=}\"\"\")\r\nprint(f\"\"\"after d.popitem={d}\"\"\")\r\ndict2 = {2: {\"c\": 222}, 3: {\"d\": {3, 6}}, 4: 3, 33: {33: 2}}\r\nd = MultiKeyDict(dict2)\r\nprint(f\"\"\"{list(d.reversed())=}\"\"\")\r\nd.update({4: {44: 4}})\r\nprint(f\"\"\"d.update...={d}\"\"\")\r\nd5 = d | {3: 4}\r\nd |= {3: 4}\r\nprint(f\"\"\"d |= {{3:4}}={d}\"\"\")\r\nprint(f'{d.to_dict()=}')\r\n\r\n\r\n\r\n\r\n\r\n{4: {5: {67: 100}}}\r\nlist(d)=[2, 3, 1]\r\nlen(d)=3\r\nd[1]={3: {4: {5: {67: 100}}}}\r\nd[1][3]={4: {5: {67: 100}}}\r\nd[[1,3]]={4: {5: {67: 100}}}\r\nd[[23,4,5,323]] = 'x'={1: {3: {4: {5: {67: 100}}}},\r\n 2: {'c': 222},\r\n 3: {'d': {3,\r\n           6}},\r\n 23: {4: {5: {323: 'x'}}}}\r\n23 in d=True\r\ndel d[[1,3]]={1: {},\r\n 2: {'c': 222},\r\n 3: {'d': {3,\r\n           6}},\r\n 23: {4: {5: {323: 'x'}}}}\r\ndel d[1]={2: {'c': 222},\r\n 3: {'d': {3,\r\n           6}},\r\n 23: {4: {5: {323: 'x'}}}}\r\ndi2 == d=True\r\ndi2 is d=False\r\ndi2.clear()={}\r\nlist(iter(d))=[2, 3, 23]\r\nd.get(2)={'c': 222}\r\nd.get([23,4,5])={323: 'x'}\r\nd.items()=dict_items([(2, {'c': 222}), (3, {'d': {3, 6}}), (23, {4: {5: {323: 'x'}}})])\r\nd.keys()=dict_keys([2, 3, 23])\r\nd.pop(3)={'d': {3, 6}}\r\nd.pop([23,4,5])={323: 'x'}\r\nd.popitem()=(2, {'c': 222})\r\nafter d.popitem={23: {4: {}}}\r\nlist(d.reversed())=[33, 4, 3, 2]\r\nd.update...={2: {'c': 222},\r\n 3: {'d': {3,\r\n           6}},\r\n 4: {44: 4},\r\n 33: {33: 2}}\r\nd |= {3:4}={2: {'c': 222},\r\n 3: 4,\r\n 4: {44: 4},\r\n 33: {33: 2}}\r\nd.to_dict()={2: {'c': 222}, 3: 4, 4: {44: 4}, 33: {33: 2}}\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Access nested dict elements as d[[1,2,3]] instead of d[1][2][3] - compatible with dict() - no requirements",
    "version": "0.15",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/mymulti_key_dict"
    },
    "split_keywords": [
        "nested",
        "dicts"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fb790b118139cb8daa298073787a123d471d379ca7a16ee93f7eb311151dd5b3",
                "md5": "bdf9974bcbef8ebcde32e5a476b4229e",
                "sha256": "418c518c1cafe7f72d3392d83c43a02cead0ba76de331beb0d7f1d566abd60d5"
            },
            "downloads": -1,
            "filename": "mymulti_key_dict-0.15-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bdf9974bcbef8ebcde32e5a476b4229e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6639,
            "upload_time": "2023-07-11T06:11:37",
            "upload_time_iso_8601": "2023-07-11T06:11:37.017834Z",
            "url": "https://files.pythonhosted.org/packages/fb/79/0b118139cb8daa298073787a123d471d379ca7a16ee93f7eb311151dd5b3/mymulti_key_dict-0.15-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c30aaa0f08c8dceffa9238175f504c1411a8fd78f832e3a974e96d195261a466",
                "md5": "2f851dc6c819c5372c7b712ef4464171",
                "sha256": "ba4f5040aa66a90f576036c4798f9c0ca8e0641a75b98911ab637a1995296b1b"
            },
            "downloads": -1,
            "filename": "mymulti_key_dict-0.15.tar.gz",
            "has_sig": false,
            "md5_digest": "2f851dc6c819c5372c7b712ef4464171",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4841,
            "upload_time": "2023-07-11T06:11:38",
            "upload_time_iso_8601": "2023-07-11T06:11:38.365812Z",
            "url": "https://files.pythonhosted.org/packages/c3/0a/aa0f08c8dceffa9238175f504c1411a8fd78f832e3a974e96d195261a466/mymulti_key_dict-0.15.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-11 06:11:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "mymulti_key_dict",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "mymulti-key-dict"
}
        
Elapsed time: 0.43762s