Skip to content

Commit 3395ec1

Browse files
author
Hamel Husain
authored
Merge pull request #274 from fastai/sparkline
add sparkline checking
2 parents f4fdb11 + 3bc7aff commit 3395ec1

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
@@ -244,13 +244,15 @@ def truncstr(s:str, maxlen:int, suf:str='…', space='')->str:
244244
# Cell
245245
def _sparkchar(x, mn, incr, empty_zero):
246246
if x is None or (empty_zero and not x): return ' '
247+
if incr == 0: return spark_chars[0]
247248
res = int((x-mn)/incr-0.5)
248249
return spark_chars[res]
249250

250251
# Cell
251252
def sparkline(data, mn=None, mx=None, empty_zero=False):
252253
"Sparkline for `data`, with `None`s (and zero, if `empty_zero`) shown as empty column"
253254
valid = [o for o in data if o is not None]
255+
if not valid: return ' '
254256
mn,mx,n = ifnone(mn,min(valid)),ifnone(mx,max(valid)),len(spark_chars)
255257
res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]
256258
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",
@@ -1402,6 +1402,7 @@
14021402
"#export\n",
14031403
"def _sparkchar(x, mn, incr, empty_zero):\n",
14041404
" if x is None or (empty_zero and not x): return ' '\n",
1405+
" if incr == 0: return spark_chars[0]\n",
14051406
" res = int((x-mn)/incr-0.5)\n",
14061407
" return spark_chars[res]"
14071408
]
@@ -1416,6 +1417,7 @@
14161417
"def sparkline(data, mn=None, mx=None, empty_zero=False):\n",
14171418
" \"Sparkline for `data`, with `None`s (and zero, if `empty_zero`) shown as empty column\"\n",
14181419
" valid = [o for o in data if o is not None]\n",
1420+
" if not valid: return ' '\n",
14191421
" mn,mx,n = ifnone(mn,min(valid)),ifnone(mx,max(valid)),len(spark_chars)\n",
14201422
" res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]\n",
14211423
" return ''.join(res)"
@@ -1480,7 +1482,7 @@
14801482
" self._reset()\n",
14811483
" self.events +=n\n",
14821484
" self.last = default_timer()\n",
1483-
" \n",
1485+
"\n",
14841486
" @property\n",
14851487
" def duration(self): return default_timer()-self.start\n",
14861488
" @property\n",
@@ -1588,7 +1590,7 @@
15881590
" def __init__(self):\n",
15891591
" self.missing = set()\n",
15901592
" super().__init__()\n",
1591-
" \n",
1593+
"\n",
15921594
" def get_field(self, nm, args, kwargs):\n",
15931595
" try: return super().get_field(nm, args, kwargs)\n",
15941596
" except KeyError:\n",

0 commit comments

Comments
 (0)