Skip to content

Commit 80fef4a

Browse files
committed
add ciel
1 parent b641e30 commit 80fef4a

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
"Path.__repr__": "03_xtras.ipynb",
166166
"truncstr": "03_xtras.ipynb",
167167
"spark_chars": "03_xtras.ipynb",
168+
"ceil": "03_xtras.ipynb",
168169
"sparkline": "03_xtras.ipynb",
169170
"autostart": "03_xtras.ipynb",
170171
"EventTimer": "03_xtras.ipynb",

fastcore/xtras.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
__all__ = ['dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'shufflish', 'mapped', 'IterLen', 'ReindexCollection',
44
'maybe_open', 'image_size', 'bunzip', 'join_path_file', 'loads', 'loads_multi', 'untar_dir', 'repo_details',
5-
'run', 'open_file', 'save_pickle', 'load_pickle', 'truncstr', 'spark_chars', 'sparkline', 'autostart',
6-
'EventTimer', 'stringfmt_names', 'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace',
7-
'round_multiple', 'modified_env', 'ContextManagers', 'str2bool', 'sort_by_run']
5+
'run', 'open_file', 'save_pickle', 'load_pickle', 'truncstr', 'spark_chars', 'ceil', 'sparkline',
6+
'autostart', 'EventTimer', 'stringfmt_names', 'PartialFormatter', 'partial_format', 'utc2local', 'local2utc',
7+
'trace', 'round_multiple', 'modified_env', 'ContextManagers', 'str2bool', 'sort_by_run']
88

99
# Cell
1010
from .imports import *
@@ -242,10 +242,12 @@ def truncstr(s:str, maxlen:int, suf:str='…', space='')->str:
242242
spark_chars = '▁▂▃▅▆▇'
243243

244244
# Cell
245-
def _sparkchar(x, mn, incr, empty_zero):
245+
def ceil(x, lim=None): return x if (not lim or x <= lim) else lim
246+
247+
def _sparkchar(x, mn, mx, incr, empty_zero):
246248
if x is None or (empty_zero and not x): return ' '
247249
if incr == 0: return spark_chars[0]
248-
res = int((x-mn)/incr-0.5)
250+
res = int((ceil(x,mx)-mn)/incr-0.5)
249251
return spark_chars[res]
250252

251253
# Cell
@@ -254,7 +256,7 @@ def sparkline(data, mn=None, mx=None, empty_zero=False):
254256
valid = [o for o in data if o is not None]
255257
if not valid: return ' '
256258
mn,mx,n = ifnone(mn,min(valid)),ifnone(mx,max(valid)),len(spark_chars)
257-
res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]
259+
res = [_sparkchar(x=o, mn=mn, mx=mx, incr=(mx-mn)/n, empty_zero=empty_zero) for o in data]
258260
return ''.join(res)
259261

260262
# Cell

nbs/03_xtras.ipynb

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@
618618
{
619619
"data": {
620620
"text/plain": [
621-
"['g', 'a', 'f', 'c', 'e', 'd', 'h', 'b']"
621+
"['c', 'g', 'a', 'e', 'b', 'f', 'd', 'h']"
622622
]
623623
},
624624
"execution_count": null,
@@ -1249,7 +1249,7 @@
12491249
{
12501250
"data": {
12511251
"text/plain": [
1252-
"Path('.ipynb_checkpoints')"
1252+
"Path('hamel.py')"
12531253
]
12541254
},
12551255
"execution_count": null,
@@ -1283,7 +1283,7 @@
12831283
{
12841284
"data": {
12851285
"text/plain": [
1286-
"(Path('../fastcore/net.py'), Path('03a_parallel.ipynb'))"
1286+
"(Path('../fastcore/xtras.py'), Path('08_script.ipynb'))"
12871287
]
12881288
},
12891289
"execution_count": null,
@@ -1400,10 +1400,12 @@
14001400
"outputs": [],
14011401
"source": [
14021402
"#export\n",
1403-
"def _sparkchar(x, mn, incr, empty_zero):\n",
1403+
"def ceil(x, lim=None): return x if (not lim or x <= lim) else lim\n",
1404+
"\n",
1405+
"def _sparkchar(x, mn, mx, incr, empty_zero):\n",
14041406
" if x is None or (empty_zero and not x): return ' '\n",
14051407
" if incr == 0: return spark_chars[0]\n",
1406-
" res = int((x-mn)/incr-0.5)\n",
1408+
" res = int((ceil(x,mx)-mn)/incr-0.5)\n",
14071409
" return spark_chars[res]"
14081410
]
14091411
},
@@ -1419,7 +1421,7 @@
14191421
" valid = [o for o in data if o is not None]\n",
14201422
" if not valid: return ' '\n",
14211423
" mn,mx,n = ifnone(mn,min(valid)),ifnone(mx,max(valid)),len(spark_chars)\n",
1422-
" res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]\n",
1424+
" res = [_sparkchar(x=o, mn=mn, mx=mx, incr=(mx-mn)/n, empty_zero=empty_zero) for o in data]\n",
14231425
" return ''.join(res)"
14241426
]
14251427
},
@@ -1443,6 +1445,33 @@
14431445
"print(f' with \"empty_zero\": {sparkline(data, empty_zero=True )}')"
14441446
]
14451447
},
1448+
{
1449+
"cell_type": "markdown",
1450+
"metadata": {},
1451+
"source": [
1452+
"You can set a maximum and minimum for the y-axis of the sparkline with the arguments `mn` and `mx` respectively:"
1453+
]
1454+
},
1455+
{
1456+
"cell_type": "code",
1457+
"execution_count": null,
1458+
"metadata": {},
1459+
"outputs": [
1460+
{
1461+
"data": {
1462+
"text/plain": [
1463+
"'▂▅▇▇'"
1464+
]
1465+
},
1466+
"execution_count": null,
1467+
"metadata": {},
1468+
"output_type": "execute_result"
1469+
}
1470+
],
1471+
"source": [
1472+
"sparkline([1,2,3,400], mn=0, mx=3)"
1473+
]
1474+
},
14461475
{
14471476
"cell_type": "code",
14481477
"execution_count": null,
@@ -1531,8 +1560,8 @@
15311560
"name": "stdout",
15321561
"output_type": "stream",
15331562
"text": [
1534-
"Num Events: 3, Freq/sec: 272.3\n",
1535-
"Most recent: ▇▁▁▂▁ 460.5 360.2 359.1 391.6 379.3\n"
1563+
"Num Events: 11, Freq/sec: 394.6\n",
1564+
"Most recent: ▇▁▃▁▆ 377.7 299.7 338.0 289.4 361.8\n"
15361565
]
15371566
}
15381567
],

0 commit comments

Comments
 (0)