Skip to content

Commit f99148b

Browse files
committed
fixes #153
1 parent 52db6ca commit f99148b

File tree

3 files changed

+229
-14
lines changed

3 files changed

+229
-14
lines changed

fastcore/_nbdev.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
"basic_repr": "01_basics.ipynb",
2626
"is_array": "01_basics.ipynb",
2727
"listify": "01_basics.ipynb",
28+
"true": "01_basics.ipynb",
29+
"NullType": "01_basics.ipynb",
30+
"null": "01_basics.ipynb",
31+
"tonull": "01_basics.ipynb",
2832
"get_class": "01_basics.ipynb",
2933
"mk_class": "01_basics.ipynb",
3034
"wrap_class": "01_basics.ipynb",
@@ -34,7 +38,6 @@
3438
"Inf": "01_basics.ipynb",
3539
"in_": "01_basics.ipynb",
3640
"operator.in_": "01_basics.ipynb",
37-
"true": "01_basics.ipynb",
3841
"stop": "01_basics.ipynb",
3942
"gen": "01_basics.ipynb",
4043
"chunked": "01_basics.ipynb",

fastcore/basics.py

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

3-
__all__ = ['defaults', 'ifnone', 'maybe_attr', 'basic_repr', 'is_array', 'listify', 'get_class', 'mk_class',
4-
'wrap_class', 'ignore_exceptions', 'exec_local', 'risinstance', 'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq',
5-
'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'in_', 'true', 'stop', 'gen', 'chunked', 'otherwise',
6-
'custom_dir', 'AttrDict', 'with_cast', 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel',
7-
'class2attr', 'hasattrs', 'setattrs', 'try_attrs', 'ShowPrint', 'Int', 'Str', 'Float', 'detuplify',
8-
'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys',
9-
'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'negate_func', 'argwhere', 'filter_ex', 'range_of',
10-
'renumerate', 'first', 'nested_attr', 'nested_idx', 'num_methods', 'rnum_methods', 'inum_methods',
11-
'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'map_ex', 'compose', 'maps', 'partialler',
12-
'instantiate', 'using_attr', 'Self', 'Self', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'typed']
3+
__all__ = ['defaults', 'ifnone', 'maybe_attr', 'basic_repr', 'is_array', 'listify', 'true', 'NullType', 'null',
4+
'tonull', 'get_class', 'mk_class', 'wrap_class', 'ignore_exceptions', 'exec_local', 'risinstance', 'Inf',
5+
'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'in_', 'true',
6+
'stop', 'gen', 'chunked', 'otherwise', 'custom_dir', 'AttrDict', 'with_cast', 'store_attr', 'attrdict',
7+
'properties', 'camel2snake', 'snake2camel', 'class2attr', 'hasattrs', 'setattrs', 'try_attrs', 'ShowPrint',
8+
'Int', 'Str', 'Float', 'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index',
9+
'filter_dict', 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'negate_func', 'argwhere',
10+
'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_idx', 'num_methods', 'rnum_methods',
11+
'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'map_ex', 'compose', 'maps',
12+
'partialler', 'instantiate', 'using_attr', 'Self', 'Self', 'PrettyString', 'even_mults', 'num_cpus',
13+
'add_props', 'typed']
1314

1415
# Cell
1516
from .imports import *
@@ -37,15 +38,40 @@ def _f(self):
3738
return _f
3839

3940
# Cell
40-
def is_array(x): return hasattr(x,'__array__') or hasattr(x,'iloc')
41+
def is_array(x):
42+
"`True` if `x` supports `__array__` or `iloc`"
43+
return hasattr(x,'__array__') or hasattr(x,'iloc')
4144

45+
# Cell
4246
def listify(o):
47+
"Convert `o` to a `list`"
4348
if o is None: return []
4449
if isinstance(o, list): return o
4550
if isinstance(o, str) or is_array(o): return [o]
4651
if is_iter(o): return list(o)
4752
return [o]
4853

54+
# Cell
55+
def true(x):
56+
"Test whether `x` is truthy; collections with >0 elements are considered `True`"
57+
try: return bool(len(x))
58+
except: return bool(x)
59+
60+
# Cell
61+
class NullType:
62+
"An object that is `False` and can be called, chained, and indexed"
63+
def __getattr__(self,*args):return null
64+
def __call__(self,*args, **kwargs):return null
65+
def __getitem__(self, *args):return null
66+
def __bool__(self): return False
67+
68+
null = NullType()
69+
70+
# Cell
71+
def tonull(x):
72+
"Convert `None` to `null`"
73+
return null if x is None else x
74+
4975
# Cell
5076
def get_class(nm, *fld_names, sup=None, doc=None, funcs=None, **flds):
5177
"Dynamically create a class, optionally inheriting from `sup`, containing `fld_names`"

nbs/01_basics.ipynb

Lines changed: 188 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,202 @@
170170
"outputs": [],
171171
"source": [
172172
"#export\n",
173-
"def is_array(x): return hasattr(x,'__array__') or hasattr(x,'iloc')\n",
174-
"\n",
173+
"def is_array(x):\n",
174+
" \"`True` if `x` supports `__array__` or `iloc`\"\n",
175+
" return hasattr(x,'__array__') or hasattr(x,'iloc')"
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": null,
181+
"metadata": {},
182+
"outputs": [
183+
{
184+
"data": {
185+
"text/plain": [
186+
"(True, False)"
187+
]
188+
},
189+
"execution_count": null,
190+
"metadata": {},
191+
"output_type": "execute_result"
192+
}
193+
],
194+
"source": [
195+
"is_array(np.array(1)),is_array([1])"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {},
202+
"outputs": [],
203+
"source": [
204+
"#export\n",
175205
"def listify(o):\n",
206+
" \"Convert `o` to a `list`\"\n",
176207
" if o is None: return []\n",
177208
" if isinstance(o, list): return o\n",
178209
" if isinstance(o, str) or is_array(o): return [o]\n",
179210
" if is_iter(o): return list(o)\n",
180211
" return [o]"
181212
]
182213
},
214+
{
215+
"cell_type": "markdown",
216+
"metadata": {},
217+
"source": [
218+
"Conversion is designed to \"do what you mean\", e.g:"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": null,
224+
"metadata": {},
225+
"outputs": [],
226+
"source": [
227+
"[(o,listify(o)) for o in\n",
228+
" ('hi',array(1),1, [1,2], range(3))]"
229+
]
230+
},
231+
{
232+
"cell_type": "markdown",
233+
"metadata": {},
234+
"source": [
235+
"Generators are turned into lists too:"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": null,
241+
"metadata": {},
242+
"outputs": [
243+
{
244+
"data": {
245+
"text/plain": [
246+
"[0, 1, 2]"
247+
]
248+
},
249+
"execution_count": null,
250+
"metadata": {},
251+
"output_type": "execute_result"
252+
}
253+
],
254+
"source": [
255+
"gen = (o for o in range(3))\n",
256+
"listify(gen)"
257+
]
258+
},
259+
{
260+
"cell_type": "code",
261+
"execution_count": null,
262+
"metadata": {},
263+
"outputs": [],
264+
"source": [
265+
"#export\n",
266+
"def true(x):\n",
267+
" \"Test whether `x` is truthy; collections with >0 elements are considered `True`\"\n",
268+
" try: return bool(len(x))\n",
269+
" except: return bool(x)"
270+
]
271+
},
272+
{
273+
"cell_type": "code",
274+
"execution_count": null,
275+
"metadata": {},
276+
"outputs": [
277+
{
278+
"data": {
279+
"text/plain": [
280+
"[(array(0), False),\n",
281+
" (array(1), True),\n",
282+
" (array([0]), True),\n",
283+
" (array([0, 1]), True),\n",
284+
" (1, True),\n",
285+
" (0, False),\n",
286+
" ('', False),\n",
287+
" (None, False)]"
288+
]
289+
},
290+
"execution_count": null,
291+
"metadata": {},
292+
"output_type": "execute_result"
293+
}
294+
],
295+
"source": [
296+
"[(o,true(o)) for o in\n",
297+
" (array(0),array(1),array([0]),array([0,1]),1,0,'',None)]"
298+
]
299+
},
300+
{
301+
"cell_type": "code",
302+
"execution_count": null,
303+
"metadata": {},
304+
"outputs": [],
305+
"source": [
306+
"#export\n",
307+
"class NullType:\n",
308+
" \"An object that is `False` and can be called, chained, and indexed\"\n",
309+
" def __getattr__(self,*args):return null\n",
310+
" def __call__(self,*args, **kwargs):return null\n",
311+
" def __getitem__(self, *args):return null\n",
312+
" def __bool__(self): return False\n",
313+
"\n",
314+
"null = NullType()"
315+
]
316+
},
317+
{
318+
"cell_type": "code",
319+
"execution_count": null,
320+
"metadata": {},
321+
"outputs": [
322+
{
323+
"data": {
324+
"text/plain": [
325+
"False"
326+
]
327+
},
328+
"execution_count": null,
329+
"metadata": {},
330+
"output_type": "execute_result"
331+
}
332+
],
333+
"source": [
334+
"bool(null.hi().there[3])"
335+
]
336+
},
337+
{
338+
"cell_type": "code",
339+
"execution_count": null,
340+
"metadata": {},
341+
"outputs": [],
342+
"source": [
343+
"#export\n",
344+
"def tonull(x):\n",
345+
" \"Convert `None` to `null`\"\n",
346+
" return null if x is None else x"
347+
]
348+
},
349+
{
350+
"cell_type": "code",
351+
"execution_count": null,
352+
"metadata": {},
353+
"outputs": [
354+
{
355+
"data": {
356+
"text/plain": [
357+
"False"
358+
]
359+
},
360+
"execution_count": null,
361+
"metadata": {},
362+
"output_type": "execute_result"
363+
}
364+
],
365+
"source": [
366+
"bool(tonull(None).hi().there[3])"
367+
]
368+
},
183369
{
184370
"cell_type": "code",
185371
"execution_count": null,

0 commit comments

Comments
 (0)