Skip to content

Commit 4519497

Browse files
committed
fixes #130
1 parent eb049b3 commit 4519497

File tree

6 files changed

+65
-33
lines changed

6 files changed

+65
-33
lines changed

fastcore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.1.3"
1+
__version__ = "2.0.0"

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"wrap_class": "02_utils.ipynb",
6969
"ignore_exceptions": "02_utils.ipynb",
7070
"exec_local": "02_utils.ipynb",
71+
"risinstance": "02_utils.ipynb",
7172
"Inf": "02_utils.ipynb",
7273
"in_": "02_utils.ipynb",
7374
"operator.in_": "02_utils.ipynb",

fastcore/foundation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ def __contains__(self,b): return b in self.items
327327
def __reversed__(self): return self._new(reversed(self.items))
328328
def __invert__(self): return self._new(not i for i in self)
329329
def __repr__(self): return repr(self.items)
330-
def _repr_pretty_(self, p, cycle): p.text(repr(self.items) if _is_array(self.items) else coll_repr(self))
330+
def _repr_pretty_(self, p, cycle):
331+
p.text('...' if cycle else repr(self.items) if _is_array(self.items) else coll_repr(self))
331332
def __mul__ (a,b): return a._new(a.items*b)
332333
def __add__ (a,b): return a._new(a.items+_listify(b))
333334
def __radd__(a,b): return a._new(b)+a

fastcore/utils.py

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

33
__all__ = ['ifnone', 'maybe_attr', 'basic_repr', 'get_class', 'mk_class', 'wrap_class', 'ignore_exceptions',
4-
'exec_local', 'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_',
5-
'is_not', 'in_', 'true', 'gen', 'chunked', 'AttrDict', 'dict2obj', 'with_cast', 'store_attr', 'attrdict',
6-
'properties', 'camel2snake', 'snake2camel', 'class2attr', 'hasattrs', 'setattrs', 'ShowPrint', 'Int', 'Str',
7-
'Float', 'tuplify', 'detuplify', 'replicate', 'uniqueify', 'setify', 'merge', 'is_listy', 'range_of',
8-
'groupby', 'last_index', 'shufflish', 'IterLen', 'ReindexCollection', 'num_methods', 'rnum_methods',
9-
'inum_methods', 'fastuple', 'trace', 'compose', 'maps', 'partialler', 'mapped', 'instantiate', 'using_attr',
10-
'Self', 'Self', 'open_file', 'save_pickle', 'load_pickle', 'bunzip', 'join_path_file', 'urlread', 'urljson',
11-
'run', 'do_request', 'sort_by_run', 'PrettyString', 'round_multiple', 'even_mults', 'num_cpus', 'add_props',
12-
'ContextManagers', 'typed', 'set_num_threads', 'ProcessPoolExecutor', 'ThreadPoolExecutor', 'parallel',
13-
'run_procs', 'parallel_gen', 'threaded']
4+
'exec_local', 'risinstance', 'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul',
5+
'truediv', 'is_', 'is_not', 'in_', 'true', 'gen', 'chunked', 'AttrDict', 'dict2obj', 'with_cast',
6+
'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', 'class2attr', 'hasattrs', 'setattrs',
7+
'ShowPrint', 'Int', 'Str', 'Float', 'tuplify', 'detuplify', 'replicate', 'uniqueify', 'setify', 'merge',
8+
'is_listy', 'range_of', 'groupby', 'last_index', 'shufflish', 'IterLen', 'ReindexCollection', 'num_methods',
9+
'rnum_methods', 'inum_methods', 'fastuple', 'trace', 'compose', 'maps', 'partialler', 'mapped',
10+
'instantiate', 'using_attr', 'Self', 'Self', 'open_file', 'save_pickle', 'load_pickle', 'bunzip',
11+
'join_path_file', 'urlread', 'urljson', 'run', 'do_request', 'sort_by_run', 'PrettyString', 'round_multiple',
12+
'even_mults', 'num_cpus', 'add_props', 'ContextManagers', 'typed', 'set_num_threads', 'ProcessPoolExecutor',
13+
'ThreadPoolExecutor', 'parallel', 'run_procs', 'parallel_gen', 'threaded']
1414

1515
# Cell
1616
from .imports import *
@@ -97,6 +97,12 @@ def exec_local(code, var_name):
9797
exec(code, globals(), loc)
9898
return loc[var_name]
9999

100+
# Cell
101+
def risinstance(types, obj=None):
102+
"Curried `isinstance` but with args reversed, suitable for `partial`"
103+
if not obj: return partial(risinstance,types)
104+
return isinstance(obj, types)
105+
100106
# Cell
101107
#hide
102108
class _InfMeta(type):

nbs/01_foundation.ipynb

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,8 @@
17141714
" def __reversed__(self): return self._new(reversed(self.items))\n",
17151715
" def __invert__(self): return self._new(not i for i in self)\n",
17161716
" def __repr__(self): return repr(self.items)\n",
1717-
" def _repr_pretty_(self, p, cycle): p.text(repr(self.items) if _is_array(self.items) else coll_repr(self))\n",
1717+
" def _repr_pretty_(self, p, cycle):\n",
1718+
" p.text('...' if cycle else repr(self.items) if _is_array(self.items) else coll_repr(self))\n",
17181719
" def __mul__ (a,b): return a._new(a.items*b)\n",
17191720
" def __add__ (a,b): return a._new(a.items+_listify(b))\n",
17201721
" def __radd__(a,b): return a._new(b)+a\n",
@@ -1965,7 +1966,7 @@
19651966
{
19661967
"data": {
19671968
"text/plain": [
1968-
"[2, 0, 10]"
1969+
"[10, 'j', 4]"
19691970
]
19701971
},
19711972
"execution_count": null,
@@ -2233,7 +2234,7 @@
22332234
{
22342235
"data": {
22352236
"text/markdown": [
2236-
"<h4 id=\"L.unique\" class=\"doc_header\"><code>L.unique</code><a href=\"__main__.py#L66\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2237+
"<h4 id=\"L.unique\" class=\"doc_header\"><code>L.unique</code><a href=\"__main__.py#L67\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
22372238
"\n",
22382239
"> <code>L.unique</code>()\n",
22392240
"\n",
@@ -2268,7 +2269,7 @@
22682269
{
22692270
"data": {
22702271
"text/markdown": [
2271-
"<h4 id=\"L.val2idx\" class=\"doc_header\"><code>L.val2idx</code><a href=\"__main__.py#L69\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2272+
"<h4 id=\"L.val2idx\" class=\"doc_header\"><code>L.val2idx</code><a href=\"__main__.py#L70\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
22722273
"\n",
22732274
"> <code>L.val2idx</code>()\n",
22742275
"\n",
@@ -2303,7 +2304,7 @@
23032304
{
23042305
"data": {
23052306
"text/markdown": [
2306-
"<h4 id=\"L.filter\" class=\"doc_header\"><code>L.filter</code><a href=\"__main__.py#L63\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2307+
"<h4 id=\"L.filter\" class=\"doc_header\"><code>L.filter</code><a href=\"__main__.py#L64\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
23072308
"\n",
23082309
"> <code>L.filter</code>(**`f`**=*`noop`*, **`negate`**=*`False`*, **`gen`**=*`False`*, **\\*\\*`kwargs`**)\n",
23092310
"\n",
@@ -2359,7 +2360,7 @@
23592360
{
23602361
"data": {
23612362
"text/markdown": [
2362-
"<h4 id=\"L.argwhere\" class=\"doc_header\"><code>L.argwhere</code><a href=\"__main__.py#L62\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2363+
"<h4 id=\"L.argwhere\" class=\"doc_header\"><code>L.argwhere</code><a href=\"__main__.py#L63\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
23632364
"\n",
23642365
"> <code>L.argwhere</code>(**`f`**, **`negate`**=*`False`*, **\\*\\*`kwargs`**)\n",
23652366
"\n",
@@ -2394,7 +2395,7 @@
23942395
{
23952396
"data": {
23962397
"text/markdown": [
2397-
"<h4 id=\"L.map\" class=\"doc_header\"><code>L.map</code><a href=\"__main__.py#L61\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2398+
"<h4 id=\"L.map\" class=\"doc_header\"><code>L.map</code><a href=\"__main__.py#L62\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
23982399
"\n",
23992400
"> <code>L.map</code>(**`f`**, **\\*`args`**, **`gen`**=*`False`*, **\\*\\*`kwargs`**)\n",
24002401
"\n",
@@ -2478,7 +2479,7 @@
24782479
{
24792480
"data": {
24802481
"text/markdown": [
2481-
"<h4 id=\"L.map_dict\" class=\"doc_header\"><code>L.map_dict</code><a href=\"__main__.py#L78\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2482+
"<h4 id=\"L.map_dict\" class=\"doc_header\"><code>L.map_dict</code><a href=\"__main__.py#L79\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
24822483
"\n",
24832484
"> <code>L.map_dict</code>(**`f`**=*`noop`*, **\\*`args`**, **`gen`**=*`False`*, **\\*\\*`kwargs`**)\n",
24842485
"\n",
@@ -2514,7 +2515,7 @@
25142515
{
25152516
"data": {
25162517
"text/markdown": [
2517-
"<h4 id=\"L.zip\" class=\"doc_header\"><code>L.zip</code><a href=\"__main__.py#L87\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2518+
"<h4 id=\"L.zip\" class=\"doc_header\"><code>L.zip</code><a href=\"__main__.py#L88\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
25182519
"\n",
25192520
"> <code>L.zip</code>(**`cycled`**=*`False`*)\n",
25202521
"\n",
@@ -2561,7 +2562,7 @@
25612562
{
25622563
"data": {
25632564
"text/markdown": [
2564-
"<h4 id=\"L.map_zip\" class=\"doc_header\"><code>L.map_zip</code><a href=\"__main__.py#L89\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2565+
"<h4 id=\"L.map_zip\" class=\"doc_header\"><code>L.map_zip</code><a href=\"__main__.py#L90\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
25652566
"\n",
25662567
"> <code>L.map_zip</code>(**`f`**, **\\*`args`**, **`cycled`**=*`False`*, **\\*\\*`kwargs`**)\n",
25672568
"\n",
@@ -2597,7 +2598,7 @@
25972598
{
25982599
"data": {
25992600
"text/markdown": [
2600-
"<h4 id=\"L.zipwith\" class=\"doc_header\"><code>L.zipwith</code><a href=\"__main__.py#L88\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2601+
"<h4 id=\"L.zipwith\" class=\"doc_header\"><code>L.zipwith</code><a href=\"__main__.py#L89\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
26012602
"\n",
26022603
"> <code>L.zipwith</code>(**\\*`rest`**, **`cycled`**=*`False`*)\n",
26032604
"\n",
@@ -2634,7 +2635,7 @@
26342635
{
26352636
"data": {
26362637
"text/markdown": [
2637-
"<h4 id=\"L.map_zipwith\" class=\"doc_header\"><code>L.map_zipwith</code><a href=\"__main__.py#L90\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2638+
"<h4 id=\"L.map_zipwith\" class=\"doc_header\"><code>L.map_zipwith</code><a href=\"__main__.py#L91\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
26382639
"\n",
26392640
"> <code>L.map_zipwith</code>(**`f`**, **\\*`rest`**, **`cycled`**=*`False`*, **\\*\\*`kwargs`**)\n",
26402641
"\n",
@@ -2669,7 +2670,7 @@
26692670
{
26702671
"data": {
26712672
"text/markdown": [
2672-
"<h4 id=\"L.itemgot\" class=\"doc_header\"><code>L.itemgot</code><a href=\"__main__.py#L70\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2673+
"<h4 id=\"L.itemgot\" class=\"doc_header\"><code>L.itemgot</code><a href=\"__main__.py#L71\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
26732674
"\n",
26742675
"> <code>L.itemgot</code>(**\\*`idxs`**)\n",
26752676
"\n",
@@ -2704,7 +2705,7 @@
27042705
{
27052706
"data": {
27062707
"text/markdown": [
2707-
"<h4 id=\"L.attrgot\" class=\"doc_header\"><code>L.attrgot</code><a href=\"__main__.py#L75\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2708+
"<h4 id=\"L.attrgot\" class=\"doc_header\"><code>L.attrgot</code><a href=\"__main__.py#L76\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
27082709
"\n",
27092710
"> <code>L.attrgot</code>(**`k`**, **`default`**=*`None`*)\n",
27102711
"\n",
@@ -2780,7 +2781,7 @@
27802781
{
27812782
"data": {
27822783
"text/markdown": [
2783-
"<h4 id=\"L.split\" class=\"doc_header\"><code>L.split</code><a href=\"__main__.py#L56\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2784+
"<h4 id=\"L.split\" class=\"doc_header\"><code>L.split</code><a href=\"__main__.py#L57\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
27842785
"\n",
27852786
"> <code>L.split</code>(**`s`**, **`sep`**=*`None`*, **`maxsplit`**=*`-1`*)\n",
27862787
"\n",
@@ -2815,7 +2816,7 @@
28152816
{
28162817
"data": {
28172818
"text/markdown": [
2818-
"<h4 id=\"L.range\" class=\"doc_header\"><code>L.range</code><a href=\"__main__.py#L58\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2819+
"<h4 id=\"L.range\" class=\"doc_header\"><code>L.range</code><a href=\"__main__.py#L59\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
28192820
"\n",
28202821
"> <code>L.range</code>(**`a`**, **`b`**=*`None`*, **`step`**=*`None`*)\n",
28212822
"\n",
@@ -2851,7 +2852,7 @@
28512852
{
28522853
"data": {
28532854
"text/markdown": [
2854-
"<h4 id=\"L.concat\" class=\"doc_header\"><code>L.concat</code><a href=\"__main__.py#L97\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2855+
"<h4 id=\"L.concat\" class=\"doc_header\"><code>L.concat</code><a href=\"__main__.py#L98\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
28552856
"\n",
28562857
"> <code>L.concat</code>()\n",
28572858
"\n",
@@ -2922,7 +2923,7 @@
29222923
{
29232924
"data": {
29242925
"text/markdown": [
2925-
"<h4 id=\"L.map_filter\" class=\"doc_header\"><code>L.map_filter</code><a href=\"__main__.py#L79\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2926+
"<h4 id=\"L.map_filter\" class=\"doc_header\"><code>L.map_filter</code><a href=\"__main__.py#L80\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
29262927
"\n",
29272928
"> <code>L.map_filter</code>(**`f`**=*`noop`*, **`g`**=*`noop`*, **\\*`args`**, **`gen`**=*`False`*, **\\*\\*`kwargs`**)\n",
29282929
"\n",
@@ -2958,7 +2959,7 @@
29582959
{
29592960
"data": {
29602961
"text/markdown": [
2961-
"<h4 id=\"L.map_first\" class=\"doc_header\"><code>L.map_first</code><a href=\"__main__.py#L83\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2962+
"<h4 id=\"L.map_first\" class=\"doc_header\"><code>L.map_first</code><a href=\"__main__.py#L84\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
29622963
"\n",
29632964
"> <code>L.map_first</code>(**`f`**=*`noop`*, **`g`**=*`noop`*, **\\*`args`**, **\\*\\*`kwargs`**)\n",
29642965
"\n",
@@ -2994,7 +2995,7 @@
29942995
{
29952996
"data": {
29962997
"text/markdown": [
2997-
"<h4 id=\"L.setattrs\" class=\"doc_header\"><code>L.setattrs</code><a href=\"__main__.py#L101\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
2998+
"<h4 id=\"L.setattrs\" class=\"doc_header\"><code>L.setattrs</code><a href=\"__main__.py#L102\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
29982999
"\n",
29993000
"> <code>L.setattrs</code>(**`attr`**, **`val`**)\n",
30003001
"\n",

nbs/02_utils.ipynb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
{
297297
"data": {
298298
"text/plain": [
299-
"<__main__._t at 0x7f791a449160>"
299+
"<__main__._t at 0x7f150b4c2f40>"
300300
]
301301
},
302302
"execution_count": null,
@@ -418,6 +418,29 @@
418418
"test_eq(exec_local(\"a=1\", \"a\"), 1)"
419419
]
420420
},
421+
{
422+
"cell_type": "code",
423+
"execution_count": null,
424+
"metadata": {},
425+
"outputs": [],
426+
"source": [
427+
"#export\n",
428+
"def risinstance(types, obj=None):\n",
429+
" \"Curried `isinstance` but with args reversed\"\n",
430+
" if not obj: return partial(risinstance,types)\n",
431+
" return isinstance(obj, types)"
432+
]
433+
},
434+
{
435+
"cell_type": "code",
436+
"execution_count": null,
437+
"metadata": {},
438+
"outputs": [],
439+
"source": [
440+
"assert risinstance(int, 1)\n",
441+
"assert risinstance(int)(1)"
442+
]
443+
},
421444
{
422445
"cell_type": "markdown",
423446
"metadata": {},

0 commit comments

Comments
 (0)