From 98fc6a2230f2a1ff10bb7cbfbbb5333ee98482e0 Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Tue, 24 Nov 2020 14:40:10 -0800 Subject: [PATCH] fixes #221 --- fastcore/_nbdev.py | 2 +- fastcore/basics.py | 8 ++++---- nbs/01_basics.ipynb | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/fastcore/_nbdev.py b/fastcore/_nbdev.py index a89d5a05..6ffb2c6a 100644 --- a/fastcore/_nbdev.py +++ b/fastcore/_nbdev.py @@ -77,7 +77,7 @@ "cycle": "02_foundation.ipynb", "zip_cycle": "02_foundation.ipynb", "sorted_ex": "01_basics.ipynb", - "negate_func": "01_basics.ipynb", + "not_": "01_basics.ipynb", "argwhere": "01_basics.ipynb", "filter_ex": "01_basics.ipynb", "renumerate": "01_basics.ipynb", diff --git a/fastcore/basics.py b/fastcore/basics.py index cc82f526..9258e17c 100644 --- a/fastcore/basics.py +++ b/fastcore/basics.py @@ -7,7 +7,7 @@ 'anno_ret', 'argnames', 'with_cast', 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', 'class2attr', 'getattrs', 'hasattrs', 'setattrs', 'try_attrs', 'ShowPrint', 'Int', 'Str', 'Float', 'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', - 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'negate_func', 'argwhere', 'filter_ex', + 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_', 'argwhere', 'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_idx', 'val2idx', 'uniqueify', 'num_methods', 'rnum_methods', 'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to', @@ -451,7 +451,7 @@ def sorted_ex(iterable, key=None, reverse=False): return sorted(iterable, key=k, reverse=reverse) # Cell -def negate_func(f): +def not_(f): "Create new function that negates result of `f`" def _f(*args, **kwargs): return not f(*args, **kwargs) return _f @@ -460,7 +460,7 @@ def _f(*args, **kwargs): return not f(*args, **kwargs) def argwhere(iterable, f, negate=False, **kwargs): "Like `filter_ex`, but return indices for matching items" if kwargs: f = partial(f,**kwargs) - if negate: f = negate_func(f) + if negate: f = not_(f) return [i for i,o in enumerate(iterable) if f(o)] # Cell @@ -468,7 +468,7 @@ def filter_ex(iterable, f=noop, negate=False, gen=False, **kwargs): "Like `filter`, but passing `kwargs` to `f`, defaulting `f` to `noop`, and adding `negate` and `gen`" if f is None: f = lambda _: True if kwargs: f = partial(f,**kwargs) - if negate: f = negate_func(f) + if negate: f = not_(f) res = filter(f, iterable) if gen: return res return list(res) diff --git a/nbs/01_basics.ipynb b/nbs/01_basics.ipynb index 4dea56e2..f427c067 100644 --- a/nbs/01_basics.ipynb +++ b/nbs/01_basics.ipynb @@ -2608,7 +2608,7 @@ "outputs": [], "source": [ "#export\n", - "def negate_func(f):\n", + "def not_(f):\n", " \"Create new function that negates result of `f`\"\n", " def _f(*args, **kwargs): return not f(*args, **kwargs)\n", " return _f" @@ -2622,8 +2622,8 @@ "source": [ "def f(a): return a>0\n", "test_eq(f(1),True)\n", - "test_eq(negate_func(f)(1),False)\n", - "test_eq(negate_func(f)(a=-1),True)" + "test_eq(not_(f)(1),False)\n", + "test_eq(not_(f)(a=-1),True)" ] }, { @@ -2636,7 +2636,7 @@ "def argwhere(iterable, f, negate=False, **kwargs):\n", " \"Like `filter_ex`, but return indices for matching items\"\n", " if kwargs: f = partial(f,**kwargs)\n", - " if negate: f = negate_func(f)\n", + " if negate: f = not_(f)\n", " return [i for i,o in enumerate(iterable) if f(o)]" ] }, @@ -2651,7 +2651,7 @@ " \"Like `filter`, but passing `kwargs` to `f`, defaulting `f` to `noop`, and adding `negate` and `gen`\"\n", " if f is None: f = lambda _: True\n", " if kwargs: f = partial(f,**kwargs)\n", - " if negate: f = negate_func(f)\n", + " if negate: f = not_(f)\n", " res = filter(f, iterable)\n", " if gen: return res\n", " return list(res)"