Skip to content

Commit 37a649c

Browse files
committed
fixes #261
1 parent b79d84f commit 37a649c

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
"Path.mk_write": "03_xtras.ipynb",
160160
"Path.ls": "03_xtras.ipynb",
161161
"Path.__repr__": "03_xtras.ipynb",
162+
"truncstr": "03_xtras.ipynb",
162163
"spark_chars": "03_xtras.ipynb",
163164
"sparkline": "03_xtras.ipynb",
164165
"autostart": "03_xtras.ipynb",

fastcore/xtras.py

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

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

224+
# Cell
225+
def truncstr(s:str, maxlen:int, suf:str='…')->str:
226+
"Truncate `s` to length `maxlen`, adding suffix `suf` if truncated"
227+
return s[:maxlen-len(suf)]+suf if len(s)>maxlen else s
228+
224229
# Cell
225230
spark_chars = '▁▂▃▅▆▇'
226231

nbs/03_xtras.ipynb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@
618618
{
619619
"data": {
620620
"text/plain": [
621-
"['e', 'a', 'c', 'h', 'g', 'b', 'f', 'd']"
621+
"['d', 'c', 'h', 'a', 'b', 'g', 'e', 'f']"
622622
]
623623
},
624624
"execution_count": null,
@@ -1319,6 +1319,31 @@
13191319
"## Other Helpers"
13201320
]
13211321
},
1322+
{
1323+
"cell_type": "code",
1324+
"execution_count": null,
1325+
"metadata": {},
1326+
"outputs": [],
1327+
"source": [
1328+
"#export\n",
1329+
"def truncstr(s:str, maxlen:int, suf:str='…')->str:\n",
1330+
" \"Truncate `s` to length `maxlen`, adding suffix `suf` if truncated\"\n",
1331+
" return s[:maxlen-len(suf)]+suf if len(s)>maxlen else s"
1332+
]
1333+
},
1334+
{
1335+
"cell_type": "code",
1336+
"execution_count": null,
1337+
"metadata": {},
1338+
"outputs": [],
1339+
"source": [
1340+
"w = 'abacadabra'\n",
1341+
"test_eq(truncstr(w, 15), w)\n",
1342+
"test_eq(truncstr(w, 5), 'abac…')\n",
1343+
"test_eq(truncstr(w, 5, suf=''), 'abaca')\n",
1344+
"test_eq(truncstr(w, 5, suf='!!'), 'aba!!')"
1345+
]
1346+
},
13221347
{
13231348
"cell_type": "code",
13241349
"execution_count": null,

0 commit comments

Comments
 (0)