aboutsummaryrefslogtreecommitdiffstats
path: root/nbs/core.ipynb
blob: 43ece7364bf2177c2ca4ea2fa8f244e99197fa8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aed9daa9-f05d-4e25-b9a5-a74d0211ef6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "596de171-5567-43c1-98ce-66eccc28bbe9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| default_exp: core"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a2cc7f8-6c65-4a7c-890c-3cf9d1cd314e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "import inspect\n",
    "from typing import Callable\n",
    "from nb import test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8485c16a",
   "metadata": {},
   "source": [
    "# Core\n",
    "> Fundamental Utilities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4875cc0-2d87-4c87-8789-7dc950be06c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def callable_name(c: Callable) -> str:\n",
    "    \"Get the name of the callable `c`.\"\n",
    "    return getattr(c, '__name__', c.__class__.__name__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "406d605d",
   "metadata": {},
   "source": [
    "This works for any function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e9c39c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert callable_name(max) == \"max\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d66b2d42",
   "metadata": {},
   "source": [
    "And for any classes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "056cb776",
   "metadata": {},
   "outputs": [],
   "source": [
    "class _A: pass\n",
    "assert callable_name(_A) == \"_A\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2c70dd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def pubattr(o):\n",
    "    \"Retrieve the public attributes of the object `o`.\"\n",
    "    return [a for a in dir(o) if not a.startswith('_')]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53cb2bd5",
   "metadata": {},
   "source": [
    "`pubattr` will hide any attribute on an object if it starts with an underscore."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac30af03",
   "metadata": {},
   "outputs": [],
   "source": [
    "_A.print = print\n",
    "_A._private_print = print\n",
    "_A.__python_internal__ = print\n",
    "assert pubattr(_A) == [\"print\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f892f4f1",
   "metadata": {},
   "source": [
    "## Class Definitions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4008335c",
   "metadata": {},
   "source": [
    "Iterative notebook-driven development makes class definitions tricky. Typically, you'd write a clas definition in one monolithic block, which leaves no room for prose or incremental execution. Inspired by the wonderful [fastcore](https://github.com/AnswerDotAI/fastcore) library, we'll add `patch` and `patch_to` function to enable piecemeal class definition via monkeypatching."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78d61a07",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def patch_to(o: object) -> Callable:\n",
    "    \"Decorator that takes in a object and attaches the decorated function onto it.\"\n",
    "    def inner(fn: Callable) -> Callable:\n",
    "        nm = callable_name(fn)\n",
    "        setattr(o, nm, fn)\n",
    "        return fn\n",
    "    return inner"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d4bb2e6",
   "metadata": {},
   "source": [
    "Let's try it out. We'll create a dummy class named `_T`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c67a3c29",
   "metadata": {},
   "outputs": [],
   "source": [
    "class _T: pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7d0a482",
   "metadata": {},
   "source": [
    "Now, we'll attach on a function to `_T` in a separate code cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42580f8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "@patch_to(_T)\n",
    "def say_hello(self: _T, name: str) -> str:\n",
    "    return f'Hello, {name}'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0edc152b",
   "metadata": {},
   "source": [
    "`_T` should have a `say_hello` function on it. Let's confirm."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "404bd72f",
   "metadata": {},
   "outputs": [],
   "source": [
    "_t = _T()\n",
    "test.equal(_t.say_hello('eugene'), 'Hello, eugene')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4acd774c",
   "metadata": {},
   "source": [
    "That's neat and all but notice that `self: _T` and `@patch_to(_T)` are both two different ways to provide the same sort of information (attach this method onto `_T`).\n",
    "\n",
    "What if we could look at the type hint to determine which object to patch?\n",
    "\n",
    "We already have `patch_to`, so let's create a `patch` decorator. All we'll need to do inside `patch` is find the object to patch and call `patch_to`. If we assume the object we're patching will always be called `self` i.e. patching a class, then this should be easy-peasy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4eb650ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def patch(fn: Callable) -> Callable:\n",
    "    \"Decorator to patch the object of the type-hinted 'self' argument of `fn` with `fn`.\"\n",
    "    self = inspect.get_annotations(fn)['self']\n",
    "    return patch_to(self)(fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97378af3",
   "metadata": {},
   "source": [
    "Let's see if we can try `patch` out now with our dummy `_T` class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "601e27f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "@patch\n",
    "def say_goodbye(self: _T, name: str) -> str:\n",
    "    return f'Goodbye, {name}'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d001522d",
   "metadata": {},
   "source": [
    "Can our `_t` object now say goodbye?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6aa1ffbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "test.equal(_t.say_goodbye('eugene'), 'Goodbye, eugene')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12b62cf8-2193-4f03-b80a-46304d73896e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nb; nb.export_nb('core.ipynb', src_dir='../nb/')"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}