Created
June 16, 2022 12:10
-
-
Save kidpixo/e85bfee889f764fd35d547f5612363f8 to your computer and use it in GitHub Desktop.
extend python dict class to use overload division operator à la pathlib
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, | |
"id": "a652d9fe-3077-4e4d-a3d4-049666c3c6eb", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2022-06-16T12:05:19.546097Z", | |
"iopub.status.busy": "2022-06-16T12:05:19.545576Z", | |
"iopub.status.idle": "2022-06-16T12:05:19.563031Z", | |
"shell.execute_reply": "2022-06-16T12:05:19.561906Z", | |
"shell.execute_reply.started": "2022-06-16T12:05:19.545992Z" | |
}, | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"class pathdict(dict):\n", | |
" def __init__(self, *args, **kw):\n", | |
" super(pathdict,self).__init__(*args, **kw)\n", | |
"\n", | |
" # inspired by pathlib!\n", | |
" # this is where the magic begins (overload the '/' operator)\n", | |
" def __truediv__(self, key): \n", | |
" try:\n", | |
" return self._extract_path(key)\n", | |
" except TypeError:\n", | |
" return NotImplemented\n", | |
"\n", | |
" def _extract_path(self, args):\n", | |
" keys = self._parse_args(args)\n", | |
" tmp = ''\n", | |
" for i,k in enumerate(keys):\n", | |
" # for the first level get the original dict and store it in tmp\n", | |
" if i == 0 :\n", | |
" tmp = self.get(k, {})\n", | |
" else:\n", | |
" # for levels >=1 get from tmp dict\n", | |
" tmp = tmp.get(k, {})\n", | |
" return tmp\n", | |
"\n", | |
" def _parse_args(self, args):\n", | |
" return args.split('/')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "15594ccb-a44d-4c03-9a60-482235252e0a", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2022-06-16T12:05:19.565486Z", | |
"iopub.status.busy": "2022-06-16T12:05:19.565038Z", | |
"iopub.status.idle": "2022-06-16T12:05:19.666991Z", | |
"shell.execute_reply": "2022-06-16T12:05:19.666029Z", | |
"shell.execute_reply.started": "2022-06-16T12:05:19.565448Z" | |
}, | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">{</span>\n", | |
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'k1'</span>: <span style=\"font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'sub_k1_k1'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'sub_k1_v1'</span><span style=\"font-weight: bold\">}</span>,\n", | |
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'k2'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'v2'</span>,\n", | |
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'k3'</span>: <span style=\"font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'sub_k3_k1'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'sub_k3_v1'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'sub_k3_k2'</span>: <span style=\"font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'sub_k3_k2_k1'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'sub_k3_k2_v1'</span><span style=\"font-weight: bold\">}}</span>\n", | |
"<span style=\"font-weight: bold\">}</span>\n", | |
"</pre>\n" | |
], | |
"text/plain": [ | |
"\u001b[1m{\u001b[0m\n", | |
"\u001b[2;32m│ \u001b[0m\u001b[32m'k1'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'sub_k1_k1'\u001b[0m: \u001b[32m'sub_k1_v1'\u001b[0m\u001b[1m}\u001b[0m,\n", | |
"\u001b[2;32m│ \u001b[0m\u001b[32m'k2'\u001b[0m: \u001b[32m'v2'\u001b[0m,\n", | |
"\u001b[2;32m│ \u001b[0m\u001b[32m'k3'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'sub_k3_k1'\u001b[0m: \u001b[32m'sub_k3_v1'\u001b[0m, \u001b[32m'sub_k3_k2'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'sub_k3_k2_k1'\u001b[0m: \u001b[32m'sub_k3_k2_v1'\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m\n", | |
"\u001b[1m}\u001b[0m\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"a = {'k1':{'sub_k1_k1':'sub_k1_v1'},\n", | |
" 'k2':'v2',\n", | |
" 'k3':{'sub_k3_k1':'sub_k3_v1','sub_k3_k2':{'sub_k3_k2_k1':'sub_k3_k2_v1'}}}\n", | |
"import rich.pretty\n", | |
"pa = pathdict(a)\n", | |
"rich.pretty.pprint(pa)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "7b98f884-cafe-4a02-ab77-8b5bbe747608", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2022-06-16T12:05:19.668808Z", | |
"iopub.status.busy": "2022-06-16T12:05:19.668450Z", | |
"iopub.status.idle": "2022-06-16T12:05:19.676657Z", | |
"shell.execute_reply": "2022-06-16T12:05:19.675564Z", | |
"shell.execute_reply.started": "2022-06-16T12:05:19.668771Z" | |
}, | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['k3', 'sub_k3_k2', 'sub_k3_k2_k1']" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# parsed keys\n", | |
"pa._parse_args('k3/sub_k3_k2/sub_k3_k2_k1')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "a58c8e68-119e-4eaa-84f0-4296dff9a619", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2022-06-16T12:05:19.679708Z", | |
"iopub.status.busy": "2022-06-16T12:05:19.679205Z", | |
"iopub.status.idle": "2022-06-16T12:05:19.687259Z", | |
"shell.execute_reply": "2022-06-16T12:05:19.685942Z", | |
"shell.execute_reply.started": "2022-06-16T12:05:19.679670Z" | |
}, | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'sub_k3_k2_v1'" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# query the nested keys\n", | |
"pa / 'k3/sub_k3_k2/sub_k3_k2_k1'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "47a61e32-d8cf-4b47-8fef-1fe102296fa7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"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.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment