Skip to content

Commit 8b63a01

Browse files
committed
fixes #260
1 parent 8a5d7da commit 8b63a01

File tree

5 files changed

+124
-7
lines changed

5 files changed

+124
-7
lines changed

fastcore/_nbdev.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"Int": "01_basics.ipynb",
6868
"Str": "01_basics.ipynb",
6969
"Float": "01_basics.ipynb",
70+
"concat": "01_basics.ipynb",
7071
"detuplify": "01_basics.ipynb",
7172
"replicate": "01_basics.ipynb",
7273
"setify": "01_basics.ipynb",
@@ -158,6 +159,8 @@
158159
"Path.mk_write": "03_xtras.ipynb",
159160
"Path.ls": "03_xtras.ipynb",
160161
"Path.__repr__": "03_xtras.ipynb",
162+
"spark_chars": "03_xtras.ipynb",
163+
"sparkline": "03_xtras.ipynb",
161164
"autostart": "03_xtras.ipynb",
162165
"time_events": "03_xtras.ipynb",
163166
"stringfmt_names": "03_xtras.ipynb",

fastcore/basics.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
'true', 'stop', 'gen', 'chunked', 'otherwise', 'custom_dir', 'AttrDict', 'type_hints', 'annotations',
77
'anno_ret', 'argnames', 'with_cast', 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel',
88
'class2attr', 'getattrs', 'hasattrs', 'setattrs', 'try_attrs', 'GetAttrBase', 'GetAttr', 'delegate_attr',
9-
'ShowPrint', 'Int', 'Str', 'Float', 'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby',
10-
'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_',
11-
'argwhere', 'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_idx', 'val2idx',
9+
'ShowPrint', 'Int', 'Str', 'Float', 'concat', 'detuplify', 'replicate', 'setify', 'merge', 'range_of',
10+
'groupby', 'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex',
11+
'not_', 'argwhere', 'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_idx', 'val2idx',
1212
'uniqueify', 'num_methods', 'rnum_methods', 'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3',
1313
'arg4', 'bind', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'Self', 'Self',
1414
'copy_func', 'patch_to', 'patch', 'patch_property', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful',
@@ -409,6 +409,11 @@ class Float(float,ShowPrint):
409409
"An extensible `float`"
410410
pass
411411

412+
# Cell
413+
def concat(colls)->list:
414+
"Concatenate all collections in `colls`"
415+
return list(itertools.chain.from_iterable(colls))
416+
412417
# Cell
413418
def detuplify(x):
414419
"If `x` is a tuple with one thing, extract it"

fastcore/xtras.py

Lines changed: 20 additions & 3 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', 'untar_dir', 'repo_details', 'run',
5-
'open_file', 'save_pickle', 'load_pickle', 'autostart', 'time_events', 'stringfmt_names', 'PartialFormatter',
6-
'partial_format', 'utc2local', 'local2utc', 'trace', 'round_multiple', 'modified_env', 'ContextManagers',
7-
'str2bool', 'sort_by_run']
5+
'open_file', 'save_pickle', 'load_pickle', 'spark_chars', 'sparkline', 'autostart', 'time_events',
6+
'stringfmt_names', 'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace', 'round_multiple',
7+
'modified_env', 'ContextManagers', 'str2bool', 'sort_by_run']
88

99
# Cell
1010
from .imports import *
@@ -221,6 +221,23 @@ def __repr__(self:Path):
221221
except: pass
222222
return f"Path({self.as_posix()!r})"
223223

224+
# Cell
225+
spark_chars = '▁▂▃▅▆▇'
226+
227+
# Cell
228+
def _sparkchar(x, mn, incr, empty_zero):
229+
if x is None or (empty_zero and not x): return ' '
230+
res = int((x-mn)/incr-0.5)
231+
return spark_chars[res]
232+
233+
# Cell
234+
def sparkline(data, empty_zero=False):
235+
"Sparkline for `data`, with `None`s (and zero, if `empty_zero`) shown as empty column"
236+
valid = [o for o in data if o is not None]
237+
mn,mx,n = min(valid),max(valid),len(spark_chars)
238+
res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]
239+
return ''.join(res)
240+
224241
# Cell
225242
def autostart(g):
226243
"Decorator that automatically starts a generator"

nbs/01_basics.ipynb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,6 +2616,38 @@
26162616
"Functions that manipulate popular python collections."
26172617
]
26182618
},
2619+
{
2620+
"cell_type": "code",
2621+
"execution_count": null,
2622+
"metadata": {},
2623+
"outputs": [],
2624+
"source": [
2625+
"#export\n",
2626+
"def concat(colls)->list:\n",
2627+
" \"Concatenate all collections in `colls`\"\n",
2628+
" return list(itertools.chain.from_iterable(colls))"
2629+
]
2630+
},
2631+
{
2632+
"cell_type": "code",
2633+
"execution_count": null,
2634+
"metadata": {},
2635+
"outputs": [
2636+
{
2637+
"data": {
2638+
"text/plain": [
2639+
"[0, 1, 2, 3, 4]"
2640+
]
2641+
},
2642+
"execution_count": null,
2643+
"metadata": {},
2644+
"output_type": "execute_result"
2645+
}
2646+
],
2647+
"source": [
2648+
"concat([(o for o in range(2)),[2,3,4]])"
2649+
]
2650+
},
26192651
{
26202652
"cell_type": "code",
26212653
"execution_count": null,
@@ -5158,6 +5190,8 @@
51585190
"Converted 01_basics.ipynb.\n",
51595191
"Converted 02_foundation.ipynb.\n",
51605192
"Converted 03_xtras.ipynb.\n",
5193+
"Converted 03a_parallel.ipynb.\n",
5194+
"Converted 03b_net.ipynb.\n",
51615195
"Converted 04_dispatch.ipynb.\n",
51625196
"Converted 05_transform.ipynb.\n",
51635197
"Converted 07_meta.ipynb.\n",

nbs/03_xtras.ipynb

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@
618618
{
619619
"data": {
620620
"text/plain": [
621-
"['g', 'a', 'b', 'c', 'e', 'f', 'd', 'h']"
621+
"['e', 'a', 'c', 'h', 'g', 'b', 'f', 'd']"
622622
]
623623
},
624624
"execution_count": null,
@@ -1319,6 +1319,64 @@
13191319
"## Other Helpers"
13201320
]
13211321
},
1322+
{
1323+
"cell_type": "code",
1324+
"execution_count": null,
1325+
"metadata": {},
1326+
"outputs": [],
1327+
"source": [
1328+
"#export\n",
1329+
"spark_chars = '▁▂▃▅▆▇'"
1330+
]
1331+
},
1332+
{
1333+
"cell_type": "code",
1334+
"execution_count": null,
1335+
"metadata": {},
1336+
"outputs": [],
1337+
"source": [
1338+
"#export\n",
1339+
"def _sparkchar(x, mn, incr, empty_zero):\n",
1340+
" if x is None or (empty_zero and not x): return ' '\n",
1341+
" res = int((x-mn)/incr-0.5)\n",
1342+
" return spark_chars[res]"
1343+
]
1344+
},
1345+
{
1346+
"cell_type": "code",
1347+
"execution_count": null,
1348+
"metadata": {},
1349+
"outputs": [],
1350+
"source": [
1351+
"#export\n",
1352+
"def sparkline(data, empty_zero=False):\n",
1353+
" \"Sparkline for `data`, with `None`s (and zero, if `empty_zero`) shown as empty column\"\n",
1354+
" valid = [o for o in data if o is not None]\n",
1355+
" mn,mx,n = min(valid),max(valid),len(spark_chars)\n",
1356+
" res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]\n",
1357+
" return ''.join(res)"
1358+
]
1359+
},
1360+
{
1361+
"cell_type": "code",
1362+
"execution_count": null,
1363+
"metadata": {},
1364+
"outputs": [
1365+
{
1366+
"name": "stdout",
1367+
"output_type": "stream",
1368+
"text": [
1369+
"without \"empty_zero\": ▅▂ ▁▂▁▃▇▅\n",
1370+
" with \"empty_zero\": ▅▂ ▁▂ ▃▇▅\n"
1371+
]
1372+
}
1373+
],
1374+
"source": [
1375+
"data = [9,6,None,1,4,0,8,15,10]\n",
1376+
"print(f'without \"empty_zero\": {sparkline(data, empty_zero=False)}')\n",
1377+
"print(f' with \"empty_zero\": {sparkline(data, empty_zero=True )}')"
1378+
]
1379+
},
13221380
{
13231381
"cell_type": "code",
13241382
"execution_count": null,

0 commit comments

Comments
 (0)