|
194 | 194 | "test_eq(anno_ret(None), None) # instead of passing in a func, pass in None" |
195 | 195 | ] |
196 | 196 | }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": null, |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [], |
| 202 | + "source": [ |
| 203 | + "#export\n", |
| 204 | + "def _lenient_issubclass(cls, types):\n", |
| 205 | + " \"If possible return whether `cls` is a subclass of `types`, otherwise return False.\"\n", |
| 206 | + " try: return isinstance(cls, types) or issubclass(cls, types)\n", |
| 207 | + " except: return False" |
| 208 | + ] |
| 209 | + }, |
| 210 | + { |
| 211 | + "cell_type": "code", |
| 212 | + "execution_count": null, |
| 213 | + "metadata": {}, |
| 214 | + "outputs": [], |
| 215 | + "source": [ |
| 216 | + "# depict the need for _lenient_issubclass:\n", |
| 217 | + "t = lambda: issubclass(typing.Collection, object)\n", |
| 218 | + "test_fail(t, contains='issubclass() arg 1 must be a class')" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "code", |
| 223 | + "execution_count": null, |
| 224 | + "metadata": {}, |
| 225 | + "outputs": [], |
| 226 | + "source": [ |
| 227 | + "assert not _lenient_issubclass(typing.Collection, list)\n", |
| 228 | + "assert _lenient_issubclass(list, typing.Collection)\n", |
| 229 | + "assert _lenient_issubclass(typing.Collection, object)\n", |
| 230 | + "assert _lenient_issubclass(typing.List, typing.Collection)\n", |
| 231 | + "assert not _lenient_issubclass(typing.Collection, typing.List)" |
| 232 | + ] |
| 233 | + }, |
197 | 234 | { |
198 | 235 | "cell_type": "code", |
199 | 236 | "execution_count": null, |
|
205 | 242 | " \"Return a new list containing all items from the iterable sorted topologically\"\n", |
206 | 243 | " l,res = L(list(iterable)),[]\n", |
207 | 244 | " for _ in range(len(l)):\n", |
208 | | - " t = l.reduce(lambda x,y: y if cmp(x,y) else x)\n", |
| 245 | + " t = l.reduce(lambda x,y: y if cmp(y,x) else x)\n", |
209 | 246 | " res.append(t), l.remove(t)\n", |
210 | 247 | " return res[::-1] if reverse else res" |
211 | 248 | ] |
212 | 249 | }, |
| 250 | + { |
| 251 | + "cell_type": "code", |
| 252 | + "execution_count": null, |
| 253 | + "metadata": {}, |
| 254 | + "outputs": [], |
| 255 | + "source": [ |
| 256 | + "td = [3, 1, 2, 5]\n", |
| 257 | + "test_eq(sorted_topologically(td), [1, 2, 3, 5])\n", |
| 258 | + "test_eq(sorted_topologically(td, reverse=True), [5, 3, 2, 1])" |
| 259 | + ] |
| 260 | + }, |
213 | 261 | { |
214 | 262 | "cell_type": "code", |
215 | 263 | "execution_count": null, |
216 | 264 | "metadata": {}, |
217 | 265 | "outputs": [], |
218 | 266 | "source": [ |
219 | 267 | "td = {int:1, numbers.Number:2, numbers.Integral:3}\n", |
220 | | - "test_eq(sorted_topologically(td, cmp=issubclass), [numbers.Number, numbers.Integral, int])\n", |
221 | | - "test_eq(sorted_topologically(td, cmp=issubclass, reverse=True), [int, numbers.Integral, numbers.Number])" |
| 268 | + "test_eq(sorted_topologically(td, cmp=_lenient_issubclass), [int, numbers.Integral, numbers.Number])" |
222 | 269 | ] |
223 | 270 | }, |
224 | 271 | { |
|
227 | 274 | "metadata": {}, |
228 | 275 | "outputs": [], |
229 | 276 | "source": [ |
230 | | - "td = sorted_topologically([numbers.Integral, tuple, list, int, dict], cmp=issubclass)\n", |
231 | | - "assert td.index(numbers.Integral) < td.index(int)" |
| 277 | + "td = [numbers.Integral, tuple, list, int, dict]\n", |
| 278 | + "td = sorted_topologically(td, cmp=_lenient_issubclass)\n", |
| 279 | + "assert td.index(int) < td.index(numbers.Integral)" |
232 | 280 | ] |
233 | 281 | }, |
234 | 282 | { |
|
358 | 406 | " def __init__(self): self.d,self.cache = {},{}\n", |
359 | 407 | "\n", |
360 | 408 | " def _reset(self):\n", |
361 | | - " self.d = {k:self.d[k] for k in sorted_topologically(self.d, cmp=issubclass, reverse=True)}\n", |
| 409 | + " self.d = {k:self.d[k] for k in sorted_topologically(self.d, cmp=_lenient_issubclass)}\n", |
362 | 410 | " self.cache = {}\n", |
363 | 411 | "\n", |
364 | 412 | " def add(self, t, f):\n", |
|
605 | 653 | { |
606 | 654 | "data": { |
607 | 655 | "text/plain": [ |
608 | | - "(list,object) -> f_bll\n", |
609 | | - "(typing.Collection,object) -> f_col\n", |
610 | 656 | "(bool,object) -> f_bll\n", |
611 | 657 | "(int,object) -> f_ni2\n", |
612 | 658 | "(Integral,object) -> f_nin\n", |
613 | 659 | "(Number,object) -> f_num\n", |
| 660 | + "(list,object) -> f_bll\n", |
| 661 | + "(typing.Collection,object) -> f_col\n", |
614 | 662 | "(object,object) -> NoneType" |
615 | 663 | ] |
616 | 664 | }, |
|
0 commit comments