Created
June 30, 2021 14:44
-
-
Save vikasavnish/22425b3d453ca1fa115b2e257c8b6167 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Map(dict):\n", | |
" def __init__(self, *args, **kwargs):\n", | |
" super(Map, self).__init__(*args, **kwargs)\n", | |
" for arg in args:\n", | |
" if isinstance(arg, dict):\n", | |
" for k, v in arg.items():\n", | |
" if isinstance(v, dict):\n", | |
" v = Map(v)\n", | |
" if isinstance(v, list):\n", | |
" self.__convert(v)\n", | |
" self[k] = v\n", | |
"\n", | |
" if kwargs:\n", | |
" for k, v in kwargs.items():\n", | |
" if isinstance(v, dict):\n", | |
" v = Map(v)\n", | |
" elif isinstance(v, list):\n", | |
" self.__convert(v)\n", | |
" self[k] = v\n", | |
"\n", | |
" def __convert(self, v):\n", | |
" for elem in range(0, len(v)):\n", | |
" if isinstance(v[elem], dict):\n", | |
" v[elem] = Map(v[elem])\n", | |
" elif isinstance(v[elem], list):\n", | |
" self.__convert(v[elem])\n", | |
"\n", | |
" def __getattr__(self, attr):\n", | |
" return self.get(attr)\n", | |
"\n", | |
" def __setattr__(self, key, value):\n", | |
" self.__setitem__(key, value)\n", | |
"\n", | |
" def __setitem__(self, key, value):\n", | |
" super(Map, self).__setitem__(key, value)\n", | |
" self.__dict__.update({key: value})\n", | |
"\n", | |
" def __delattr__(self, item):\n", | |
" self.__delitem__(item)\n", | |
"\n", | |
" def __delitem__(self, key):\n", | |
" super(Map, self).__delitem__(key)\n", | |
" del self.__dict__[key]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"sample_dict = {\n", | |
" \"a\":{\"b\":{\"c\":{\"d\":{\"e\":{\"f\":{\"g\":\"h\"}}}}}}\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "AttributeError", | |
"evalue": "'dict' object has no attribute 'a'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-4-a2a691e37ef6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msample_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m: 'dict' object has no attribute 'a'" | |
] | |
} | |
], | |
"source": [ | |
"sample_dict.a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'c': {'d': {'e': {'f': {'g': 'h'}}}}}" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sample_dict.get(\"a\").get(\"b\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'c': {'d': {'e': {'f': {'g': 'h'}}}}}" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# difficult to set deep nested \n", | |
"sample_dict[\"a\"][\"b\"]={\"y\":\"z\"}" | |
] | |
} | |
], | |
"metadata": { | |
"interpreter": { | |
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" | |
}, | |
"kernelspec": { | |
"display_name": "Python 3.8.6 64-bit", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.6" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment