Skip to content

Commit 1a64c71

Browse files
committed
sorted_topologically function instead of sorted to fix issue #100
1 parent d5e43c9 commit 1a64c71

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
"parallel_gen": "02_utils.ipynb",
126126
"type_hints": "03_dispatch.ipynb",
127127
"anno_ret": "03_dispatch.ipynb",
128-
"cmp_instance": "03_dispatch.ipynb",
128+
"sorted_topologically": "03_dispatch.ipynb",
129129
"TypeDispatch": "03_dispatch.ipynb",
130130
"DispatchReg": "03_dispatch.ipynb",
131131
"typedispatch": "03_dispatch.ipynb",

fastcore/dispatch.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/03_dispatch.ipynb (unless otherwise specified).
22

3-
__all__ = ['type_hints', 'anno_ret', 'cmp_instance', 'TypeDispatch', 'DispatchReg', 'typedispatch', 'cast',
3+
__all__ = ['type_hints', 'anno_ret', 'sorted_topologically', 'TypeDispatch', 'DispatchReg', 'typedispatch', 'cast',
44
'retain_meta', 'default_set_meta', 'retain_type', 'retain_types', 'explode_types']
55

66
# Cell
@@ -23,7 +23,13 @@ def anno_ret(func):
2323
return ann.get('return')
2424

2525
# Cell
26-
cmp_instance = functools.cmp_to_key(lambda a,b: 0 if a==b else 1 if issubclass(a,b) else -1)
26+
def sorted_topologically(iterable, *, cmp=operator.lt, reverse=False):
27+
"Return a new list containing all items from the iterable sorted topologically"
28+
l,res = L(list(iterable)),[]
29+
for _ in range(len(l)):
30+
t = l.reduce(lambda x,y: y if cmp(x,y) else x)
31+
res.append(t), l.remove(t)
32+
return res[::-1] if reverse else res
2733

2834
# Cell
2935
def _chk_defaults(f, ann):
@@ -49,7 +55,7 @@ class _TypeDict:
4955
def __init__(self): self.d,self.cache = {},{}
5056

5157
def _reset(self):
52-
self.d = {k:self.d[k] for k in sorted(self.d, key=cmp_instance, reverse=True)}
58+
self.d = {k:self.d[k] for k in sorted_topologically(self.d, cmp=issubclass, reverse=True)}
5359
self.cache = {}
5460

5561
def add(self, t, f):

nbs/03_dispatch.ipynb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
{
2626
"cell_type": "code",
2727
"execution_count": null,
28-
"metadata": {},
28+
"metadata": {
29+
"tags": []
30+
},
2931
"outputs": [],
3032
"source": [
3133
"from nbdev.showdoc import *\n",
@@ -199,7 +201,13 @@
199201
"outputs": [],
200202
"source": [
201203
"#export\n",
202-
"cmp_instance = functools.cmp_to_key(lambda a,b: 0 if a==b else 1 if issubclass(a,b) else -1)"
204+
"def sorted_topologically(iterable, *, cmp=operator.lt, reverse=False):\n",
205+
" \"Return a new list containing all items from the iterable sorted topologically\"\n",
206+
" l,res = L(list(iterable)),[]\n",
207+
" for _ in range(len(l)):\n",
208+
" t = l.reduce(lambda x,y: y if cmp(x,y) else x)\n",
209+
" res.append(t), l.remove(t)\n",
210+
" return res[::-1] if reverse else res"
203211
]
204212
},
205213
{
@@ -209,7 +217,18 @@
209217
"outputs": [],
210218
"source": [
211219
"td = {int:1, numbers.Number:2, numbers.Integral:3}\n",
212-
"test_eq(sorted(td, key=cmp_instance), [numbers.Number, numbers.Integral, int])"
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])"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": null,
227+
"metadata": {},
228+
"outputs": [],
229+
"source": [
230+
"td = sorted_topologically([numbers.Integral, tuple, list, int, dict], cmp=issubclass)\n",
231+
"assert td.index(numbers.Integral) < td.index(int)"
213232
]
214233
},
215234
{
@@ -339,7 +358,7 @@
339358
" def __init__(self): self.d,self.cache = {},{}\n",
340359
"\n",
341360
" def _reset(self):\n",
342-
" self.d = {k:self.d[k] for k in sorted(self.d, key=cmp_instance, reverse=True)}\n",
361+
" self.d = {k:self.d[k] for k in sorted_topologically(self.d, cmp=issubclass, reverse=True)}\n",
343362
" self.cache = {}\n",
344363
"\n",
345364
" def add(self, t, f):\n",

0 commit comments

Comments
 (0)