Last active
February 24, 2020 08:12
-
-
Save takotab/b1b11240ddf8f4fff1c0d3af8f032a6c 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": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from fastcore.utils import *\n", | |
"from fastcore.dispatch import *" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\u001b[0;31mSignature:\u001b[0m \u001b[0mtypedispatch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mType:\u001b[0m DispatchReg\n", | |
"\u001b[0;31mString form:\u001b[0m <fastcore.dispatch.DispatchReg object at 0x7fbb36a08290>\n", | |
"\u001b[0;31mFile:\u001b[0m ~/dev/env37/lib/python3.7/site-packages/fastcore/dispatch.py\n", | |
"\u001b[0;31mSource:\u001b[0m \n", | |
"\u001b[0;32mclass\u001b[0m \u001b[0mDispatchReg\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0;34m\"A global registry for `TypeDispatch` objects keyed by function name\"\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0md\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdefaultdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mTypeDispatch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0mnm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34mf'{f.__qualname__}'\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnm\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnm\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"typedispatch??" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(float,str) -> hello\n", | |
"(int,str) -> hello\n" | |
] | |
} | |
], | |
"source": [ | |
"@typedispatch\n", | |
"def hello(x:int,y:str):\n", | |
" print('intx',y)\n", | |
" \n", | |
"@typedispatch\n", | |
"def hello(x:float,y:str):\n", | |
" print('floatx',y)\n", | |
" \n", | |
"print(hello)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"intx y\n" | |
] | |
} | |
], | |
"source": [ | |
"hello(1,'y')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"floatx y\n" | |
] | |
} | |
], | |
"source": [ | |
"hello(1.,'y')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(float,str) -> hello\n", | |
"(int,str) -> hello\n" | |
] | |
} | |
], | |
"source": [ | |
"@typedispatch\n", | |
"def hello(x:(float,int),y:str):\n", | |
" print('float,int x',y)\n", | |
"@typedispatch\n", | |
"def hello(x:(int,float),y:str):\n", | |
" print('int, float x',y)\n", | |
"\n", | |
"print(hello)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int, float x y\n" | |
] | |
} | |
], | |
"source": [ | |
"hello(1,'y')#bug???" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int, float x y\n" | |
] | |
} | |
], | |
"source": [ | |
"hello(1.,'y')#bug???" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1.0, 1)" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"hello((1.,1),'y') # it does not reconize within the tuple" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1, 1.0)" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"hello((1,1.),'y')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"hello(tuple(''),'y')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"@typedispatch\n", | |
"def hello(x,y):\n", | |
" print('unknown',y)\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int, float x y\n" | |
] | |
} | |
], | |
"source": [ | |
"hello(1,'y')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int, float x y\n" | |
] | |
} | |
], | |
"source": [ | |
"hello(1.,'y')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(int,str) -> hello\n", | |
"(float,str) -> hello\n", | |
"(object,object) -> hello\n" | |
] | |
} | |
], | |
"source": [ | |
"print(hello)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from fastcore.dispatch import _p2_anno, _TypeDict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{}\n", | |
"(<class 'int'>, <class 'float'>)\n", | |
"{<class 'float'>: {<class 'str'>: <function f at 0x7fbb36980440>}, <class 'int'>: {<class 'str'>: <function f at 0x7fbb36980440>}}\n" | |
] | |
} | |
], | |
"source": [ | |
"# @typedispatch\n", | |
"def f(x:(int,float),y:str,*args):\n", | |
" print([type(o) for o in x] , 'wo dispatch')\n", | |
"a0,a1 = _p2_anno(f)\n", | |
"funcs = _TypeDict()\n", | |
"print(funcs)\n", | |
"t = None\n", | |
"if t is None:\n", | |
" t = _TypeDict()\n", | |
" print(a0)\n", | |
" funcs.add(a0, t)\n", | |
"t.add(a1, f)\n", | |
"print(funcs)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{<class 'int'>: {<class 'str'>: <function f at 0x7fbb36980830>}}\n" | |
] | |
} | |
], | |
"source": [ | |
"print(funcs)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[<class 'object'>, <class 'object'>]\n" | |
] | |
} | |
], | |
"source": [ | |
"@typedispatch\n", | |
"def tuple_inside(x:(int,float),y:str):\n", | |
" if type(x) is tuple:\n", | |
" print([type(o) for o in x] , 'w dispatch')\n", | |
" print(type(x), 'w distpatch')\n", | |
"print(_p2_anno(tuple_inside))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1, 1.0)" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"tuple_inside((1,1.),'')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'int'> w distpatch\n" | |
] | |
} | |
], | |
"source": [ | |
"tuple_inside(1,'')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'float'> w distpatch\n" | |
] | |
} | |
], | |
"source": [ | |
"tuple_inside(1.,'')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# So it just adds both types in the tuple???\n", | |
"# is this intented?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# yes this is (semi) intented!\n", | |
"# _TypeDict.add :\n", | |
"def add(self, t, f):\n", | |
" \"Add type `t` and function `f`\"\n", | |
" if not isinstance(t,tuple): t=tuple(L(t))\n", | |
" for t_ in t: self.d[t_] = f\n", | |
" self._reset()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/home/tako/dev/env37/lib/python3.7/site-packages/pandas/compat/__init__.py:117: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.\n", | |
" warnings.warn(msg)\n" | |
] | |
} | |
], | |
"source": [ | |
"from fastai2.basics import *" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(TensorBase([1]), {'meta': '1'})" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"o = TensorBase([1], meta='1')\n", | |
"o, o._meta" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class MetaTuple(Tuple):\n", | |
" def __new__(cls, x, *rest, **kwargs):\n", | |
" r = Tuple.__new__(cls,x, *rest)\n", | |
" r._meta = {i:a._meta for i,a in enumerate(L(r)) if hasattr(a,'_meta')}\n", | |
" r._types = [type(a) for a in L(r)]\n", | |
" return r" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"((1), {}, [int])" | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"o = MetaTuple(1)\n", | |
"o, o._meta, o._types" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'label': 'x'}\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"((TensorBase(1), TensorBase(1)),\n", | |
" {0: {'label': 'x'}, 1: {'label': 'y'}},\n", | |
" [fastai2.torch_core.TensorBase, fastai2.torch_core.TensorBase])" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = TensorBase(tensor(1), label='x')\n", | |
"b = TensorBase(tensor(1), label='y')\n", | |
"print(a._meta)\n", | |
"o = MetaTuple((a,b))\n", | |
"o, getattr(o,'_meta',None), o._types" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "env37", | |
"language": "python", | |
"name": "env37" | |
}, | |
"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.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment