Created
January 13, 2020 20:32
-
-
Save gwbischof/1a6e4370267f790a447eb08745110e27 to your computer and use it in GitHub Desktop.
This file contains hidden or 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": 91, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import collections\n", | |
"import random\n", | |
"\n", | |
"class LazyEntries(collections.abc.Mapping):\n", | |
" \n", | |
" def __init__(self, dictionary=None):\n", | |
" self._dict = dictionary or {}\n", | |
" \n", | |
" def __getitem__(self, key):\n", | |
" if callable(self._dict[key]):\n", | |
" result = self._dict[key].__call__()\n", | |
" self._dict[key] = result\n", | |
" return result\n", | |
" else:\n", | |
" return self._dict[key]\n", | |
" \n", | |
" def __setitem__(self, key, value):\n", | |
" self._dict[key] = value\n", | |
" \n", | |
" def __iter__(self):\n", | |
" return iter(self._dict)\n", | |
"\n", | |
" def __len__(self):\n", | |
" return len(self._dict)\n", | |
"\n", | |
"class Entry:\n", | |
" def __init__(self, name):\n", | |
" print(\"init\", name)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 96, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"d = {key: lambda: Entry(4) for key in range(10)}\n", | |
"lm = LazyEntries(d)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"lm[1] = lambda: A('G')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"init G\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<__main__.A at 0x7f26309a3358>" | |
] | |
}, | |
"execution_count": 88, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lm[1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 89, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"list(lm)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 97, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"entries = LazyEntries()\n", | |
"\n", | |
"for i in range(10):\n", | |
" entries[i] = lambda: Entry(random.randint(0,10))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<__main__.LazyEntries at 0x7f2630a188d0>" | |
] | |
}, | |
"execution_count": 98, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"entries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 99, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 99, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"list(entries)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 100, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"init 5\n", | |
"init 9\n", | |
"init 4\n", | |
"init 8\n", | |
"init 5\n", | |
"init 5\n", | |
"init 9\n", | |
"init 2\n", | |
"init 2\n", | |
"init 2\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[(0, <__main__.Entry at 0x7f2630a180f0>),\n", | |
" (1, <__main__.Entry at 0x7f2630a0a6a0>),\n", | |
" (2, <__main__.Entry at 0x7f26309bf198>),\n", | |
" (3, <__main__.Entry at 0x7f2630a0aa90>),\n", | |
" (4, <__main__.Entry at 0x7f2630a0a710>),\n", | |
" (5, <__main__.Entry at 0x7f2630a0a5c0>),\n", | |
" (6, <__main__.Entry at 0x7f2630a0a2b0>),\n", | |
" (7, <__main__.Entry at 0x7f2630a0a4e0>),\n", | |
" (8, <__main__.Entry at 0x7f2630a0a1d0>),\n", | |
" (9, <__main__.Entry at 0x7f2630a0a320>)]" | |
] | |
}, | |
"execution_count": 100, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"list(entries.items())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment