Skip to content

Commit 3bc7aff

Browse files
committed
add sparkline checking
1 parent 82e374d commit 3bc7aff

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

fastcore/xtras.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,15 @@ def truncstr(s:str, maxlen:int, suf:str='…', space='')->str:
243243
# Cell
244244
def _sparkchar(x, mn, incr, empty_zero):
245245
if x is None or (empty_zero and not x): return ' '
246+
if incr == 0: return spark_chars[0]
246247
res = int((x-mn)/incr-0.5)
247248
return spark_chars[res]
248249

249250
# Cell
250251
def sparkline(data, mn=None, mx=None, empty_zero=False):
251252
"Sparkline for `data`, with `None`s (and zero, if `empty_zero`) shown as empty column"
252253
valid = [o for o in data if o is not None]
254+
if not valid: return ' '
253255
mn,mx,n = ifnone(mn,min(valid)),ifnone(mx,max(valid)),len(spark_chars)
254256
res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]
255257
return ''.join(res)

nbs/03_xtras.ipynb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@
350350
" \"Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`\"\n",
351351
" _default='coll'\n",
352352
" def __init__(self, coll, idxs=None, cache=None, tfm=noop):\n",
353-
" if idxs is None: idxs = L.range(coll) \n",
353+
" if idxs is None: idxs = L.range(coll)\n",
354354
" store_attr()\n",
355355
" if cache is not None: self._get = functools.lru_cache(maxsize=cache)(self._get)\n",
356356
"\n",
@@ -1401,6 +1401,7 @@
14011401
"#export\n",
14021402
"def _sparkchar(x, mn, incr, empty_zero):\n",
14031403
" if x is None or (empty_zero and not x): return ' '\n",
1404+
" if incr == 0: return spark_chars[0]\n",
14041405
" res = int((x-mn)/incr-0.5)\n",
14051406
" return spark_chars[res]"
14061407
]
@@ -1415,6 +1416,7 @@
14151416
"def sparkline(data, mn=None, mx=None, empty_zero=False):\n",
14161417
" \"Sparkline for `data`, with `None`s (and zero, if `empty_zero`) shown as empty column\"\n",
14171418
" valid = [o for o in data if o is not None]\n",
1419+
" if not valid: return ' '\n",
14181420
" mn,mx,n = ifnone(mn,min(valid)),ifnone(mx,max(valid)),len(spark_chars)\n",
14191421
" res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]\n",
14201422
" return ''.join(res)"
@@ -1479,7 +1481,7 @@
14791481
" self._reset()\n",
14801482
" self.events +=n\n",
14811483
" self.last = default_timer()\n",
1482-
" \n",
1484+
"\n",
14831485
" @property\n",
14841486
" def duration(self): return default_timer()-self.start\n",
14851487
" @property\n",
@@ -1587,7 +1589,7 @@
15871589
" def __init__(self):\n",
15881590
" self.missing = set()\n",
15891591
" super().__init__()\n",
1590-
" \n",
1592+
"\n",
15911593
" def get_field(self, nm, args, kwargs):\n",
15921594
" try: return super().get_field(nm, args, kwargs)\n",
15931595
" except KeyError:\n",

0 commit comments

Comments
 (0)