Skip to content

Commit

Permalink
fixes #162
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Nov 1, 2020
1 parent aabe94b commit e321b9c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
6 changes: 6 additions & 0 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,16 @@ def __getattr__(self,k):
self.ready = False
return self

def _call(self, *args, **kwargs):
self.args,self.kwargs,self.nms = [args],[kwargs],['__call__']
self.ready = True
return self

# Cell
class _SelfCls:
def __getattr__(self,k): return getattr(_Self(),k)
def __getitem__(self,i): return self.__getattr__('__getitem__')(i)
def __call__(self,*args,**kwargs): return self.__getattr__('_call')(*args,**kwargs)

Self = _SelfCls()

Expand Down
36 changes: 36 additions & 0 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,11 @@
" self.kwargs.append(None)\n",
" self.nms.append(k)\n",
" self.ready = False\n",
" return self\n",
" \n",
" def _call(self, *args, **kwargs):\n",
" self.args,self.kwargs,self.nms = [args],[kwargs],['__call__']\n",
" self.ready = True\n",
" return self"
]
},
Expand All @@ -3177,6 +3182,7 @@
"class _SelfCls:\n",
" def __getattr__(self,k): return getattr(_Self(),k)\n",
" def __getitem__(self,i): return self.__getattr__('__getitem__')(i)\n",
" def __call__(self,*args,**kwargs): return self.__getattr__('_call')(*args,**kwargs)\n",
"\n",
"Self = _SelfCls()"
]
Expand Down Expand Up @@ -3234,6 +3240,36 @@
"test_eq(f(x), 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`Self` is also callable, which creates a function which calls any function passed to it, using the arguments passed to `Self`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 2]"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f(a, b=3): return a+b+2\n",
"def g(a, b=3): return a*b\n",
"fg = Self(1,b=2)\n",
"list(map(fg, [f,g]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
41 changes: 33 additions & 8 deletions nbs/03_xtras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@
{
"data": {
"text/plain": [
"['b', 'd', 'g', 'h', 'f', 'c', 'e', 'a']"
"['a', 'e', 'b', 'c', 'f', 'd', 'g', 'h']"
]
},
"execution_count": null,
Expand Down Expand Up @@ -869,7 +869,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"For example, we can use this to reimplement `imghdr.what` from the Python standard library, which is written in Python 3.7 as:"
"For example, we can use this to reimplement [`imghdr.what`](https://docs.python.org/3/library/imghdr.html#imghdr.what) from the Python standard library, which is [written in Python 3.9](https://github.com/python/cpython/blob/3.9/Lib/imghdr.py#L11) as:"
]
},
{
Expand Down Expand Up @@ -929,7 +929,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"With `maybe_open` and `first`, we can rewrite this in a much more concise and (in our opinion) clear way:"
"With `maybe_open`, `Self`, and `first`, we can rewrite this in a much more concise and (in our opinion) clear way:"
]
},
{
Expand All @@ -938,11 +938,10 @@
"metadata": {},
"outputs": [],
"source": [
"def what(f):\n",
" with maybe_open(f, 'rb') as f:\n",
" location,h = f.tell(),f.read(32)\n",
" f.seek(location)\n",
" return first((tf(h,f) for tf in imghdr.tests), noop)"
"def what(file, h=None):\n",
" if h is None:\n",
" with maybe_open(file, 'rb') as f: h = f.peek(32)\n",
" return first(map(Self(h,file), imghdr.tests), noop)"
]
},
{
Expand Down Expand Up @@ -972,6 +971,32 @@
"what(fname)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"...along with the `h` parameter version:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"jpeg\n"
]
}
],
"source": [
"with open(fname,'rb') as f:\n",
" h = f.read(32)\n",
" print(what(None, h=h))"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit e321b9c

Please # to comment.