Skip to content

Commit 4e2b8a3

Browse files
committed
fixes #191
1 parent 29b0fd4 commit 4e2b8a3

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

fastcore/basics.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ def exec_local(code, var_name):
143143
# Cell
144144
def risinstance(types, obj=None):
145145
"Curried `isinstance` but with args reversed"
146+
types = tuplify(types)
146147
if obj is None: return partial(risinstance,types)
148+
if any(isinstance(t,str) for t in types):
149+
return any(t.__name__ in types for t in type(obj).__mro__)
147150
return isinstance(obj, types)
148151

149152
# Cell

fastcore/dispatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def default_set_meta(self, x, as_copy=False):
174174
def cast(x, typ):
175175
"cast `x` to type `typ` (may also change `x` inplace)"
176176
res = typ._before_cast(x) if hasattr(typ, '_before_cast') else x
177-
if isinstance_str(res, 'ndarray'): res = res.view(typ)
177+
if risinstance('ndarray', res): res = res.view(typ)
178178
elif hasattr(res, 'as_subclass'): res = res.as_subclass(typ)
179179
else:
180180
try: res.__class__ = typ

fastcore/foundation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def coll_repr(c, max_n=10):
5050
# Cell
5151
def is_bool(x):
5252
"Check whether `x` is a bool or None"
53-
return isinstance(x,(bool,NoneType)) or isinstance_str(x, 'bool_')
53+
return isinstance(x,(bool,NoneType)) or risinstance('bool_', x)
5454

5555
# Cell
5656
def mask2idxs(mask):
@@ -127,7 +127,7 @@ def __setitem__(self, idx, o):
127127
for i,o_ in zip(idx,o): self.items[i] = o_
128128

129129
def __eq__(self,b):
130-
if isinstance_str(b, 'ndarray'): return array_equal(b, self)
130+
if risinstance('ndarray', b): return array_equal(b, self)
131131
if isinstance(b, (str,dict)): return False
132132
return all_equal(b,self)
133133

nbs/01_basics.ipynb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,10 @@
747747
"#export\n",
748748
"def risinstance(types, obj=None):\n",
749749
" \"Curried `isinstance` but with args reversed\"\n",
750+
" types = tuplify(types)\n",
750751
" if obj is None: return partial(risinstance,types)\n",
752+
" if any(isinstance(t,str) for t in types):\n",
753+
" return any(t.__name__ in types for t in type(obj).__mro__)\n",
751754
" return isinstance(obj, types)"
752755
]
753756
},
@@ -762,6 +765,24 @@
762765
"assert risinstance(int)(1)"
763766
]
764767
},
768+
{
769+
"cell_type": "markdown",
770+
"metadata": {},
771+
"source": [
772+
"`types` can also be strings:"
773+
]
774+
},
775+
{
776+
"cell_type": "code",
777+
"execution_count": null,
778+
"metadata": {},
779+
"outputs": [],
780+
"source": [
781+
"assert risinstance(('str','int'), 'a')\n",
782+
"assert risinstance('str', 'a')\n",
783+
"assert not risinstance('int', 'a')"
784+
]
785+
},
765786
{
766787
"cell_type": "markdown",
767788
"metadata": {},

nbs/02_foundation.ipynb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
"# export\n",
348348
"def is_bool(x):\n",
349349
" \"Check whether `x` is a bool or None\"\n",
350-
" return isinstance(x,(bool,NoneType)) or isinstance_str(x, 'bool_')"
350+
" return isinstance(x,(bool,NoneType)) or risinstance('bool_', x)"
351351
]
352352
},
353353
{
@@ -576,7 +576,7 @@
576576
" for i,o_ in zip(idx,o): self.items[i] = o_\n",
577577
"\n",
578578
" def __eq__(self,b): \n",
579-
" if isinstance_str(b, 'ndarray'): return array_equal(b, self)\n",
579+
" if risinstance('ndarray', b): return array_equal(b, self)\n",
580580
" if isinstance(b, (str,dict)): return False\n",
581581
" return all_equal(b,self)\n",
582582
"\n",
@@ -685,7 +685,7 @@
685685
{
686686
"data": {
687687
"text/plain": [
688-
"b'\\x80\\x04\\x95\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x8c\\x07inspect\\x94\\x8c\\tSignature\\x94\\x93\\x94(h\\x00\\x8c\\tParameter\\x94\\x93\\x94\\x8c\\x05items\\x94h\\x00\\x8c\\x0e_ParameterKind\\x94\\x93\\x94K\\x01\\x85\\x94R\\x94\\x86\\x94R\\x94}\\x94(\\x8c\\x08_default\\x94N\\x8c\\x0b_annotation\\x94h\\x00\\x8c\\x06_empty\\x94\\x93\\x94ubh\\x04\\x8c\\x04rest\\x94h\\x07K\\x02\\x85\\x94R\\x94\\x86\\x94R\\x94}\\x94(h\\rh\\x10h\\x0eh\\x10ubh\\x04\\x8c\\x08use_list\\x94h\\x07K\\x03\\x85\\x94R\\x94\\x86\\x94R\\x94}\\x94(h\\r\\x89h\\x0eh\\x10ubh\\x04\\x8c\\x05match\\x94h\\x19\\x86\\x94R\\x94}\\x94(h\\rNh\\x0eh\\x10ubt\\x94\\x85\\x94R\\x94}\\x94\\x8c\\x12_return_annotation\\x94h\\x10sb.'"
688+
"b'\\x80\\x03cinspect\\nSignature\\nq\\x00(cinspect\\nParameter\\nq\\x01X\\x05\\x00\\x00\\x00itemsq\\x02cinspect\\n_ParameterKind\\nq\\x03K\\x01\\x85q\\x04Rq\\x05\\x86q\\x06Rq\\x07}q\\x08(X\\x08\\x00\\x00\\x00_defaultq\\tNX\\x0b\\x00\\x00\\x00_annotationq\\ncinspect\\n_empty\\nq\\x0bubh\\x01X\\x04\\x00\\x00\\x00restq\\x0ch\\x03K\\x02\\x85q\\rRq\\x0e\\x86q\\x0fRq\\x10}q\\x11(h\\th\\x0bh\\nh\\x0bubh\\x01X\\x08\\x00\\x00\\x00use_listq\\x12h\\x03K\\x03\\x85q\\x13Rq\\x14\\x86q\\x15Rq\\x16}q\\x17(h\\t\\x89h\\nh\\x0bubh\\x01X\\x05\\x00\\x00\\x00matchq\\x18h\\x14\\x86q\\x19Rq\\x1a}q\\x1b(h\\tNh\\nh\\x0bubtq\\x1c\\x85q\\x1dRq\\x1e}q\\x1fX\\x12\\x00\\x00\\x00_return_annotationq h\\x0bsb.'"
689689
]
690690
},
691691
"execution_count": null,
@@ -818,7 +818,7 @@
818818
{
819819
"data": {
820820
"text/plain": [
821-
"[10, 3, 7]"
821+
"['j', 5, 10]"
822822
]
823823
},
824824
"execution_count": null,
@@ -1991,7 +1991,6 @@
19911991
"Converted 05_transform.ipynb.\n",
19921992
"Converted 07_meta.ipynb.\n",
19931993
"Converted 08_script.ipynb.\n",
1994-
"Converted Untitled1.ipynb.\n",
19951994
"Converted index.ipynb.\n"
19961995
]
19971996
}

nbs/04_dispatch.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@
12101210
"def cast(x, typ):\n",
12111211
" \"cast `x` to type `typ` (may also change `x` inplace)\"\n",
12121212
" res = typ._before_cast(x) if hasattr(typ, '_before_cast') else x\n",
1213-
" if isinstance_str(res, 'ndarray'): res = res.view(typ)\n",
1213+
" if risinstance('ndarray', res): res = res.view(typ)\n",
12141214
" elif hasattr(res, 'as_subclass'): res = res.as_subclass(typ)\n",
12151215
" else:\n",
12161216
" try: res.__class__ = typ\n",
@@ -1412,9 +1412,10 @@
14121412
"Converted 01_basics.ipynb.\n",
14131413
"Converted 02_foundation.ipynb.\n",
14141414
"Converted 03_xtras.ipynb.\n",
1415+
"Converted 03a_parallel.ipynb.\n",
1416+
"Converted 03b_net.ipynb.\n",
14151417
"Converted 04_dispatch.ipynb.\n",
14161418
"Converted 05_transform.ipynb.\n",
1417-
"Converted 06_logargs.ipynb.\n",
14181419
"Converted 07_meta.ipynb.\n",
14191420
"Converted 08_script.ipynb.\n",
14201421
"Converted index.ipynb.\n"

0 commit comments

Comments
 (0)