Skip to content

Commit 18bd33e

Browse files
committed
fixes #743
1 parent 3d3b0fa commit 18bd33e

5 files changed

Lines changed: 61 additions & 30 deletions

File tree

fastcore/_modidx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,8 @@
586586
'fastcore.test.test_shuffled': ('test.html#test_shuffled', 'fastcore/test.py'),
587587
'fastcore.test.test_stdout': ('test.html#test_stdout', 'fastcore/test.py'),
588588
'fastcore.test.test_warns': ('test.html#test_warns', 'fastcore/test.py')},
589-
'fastcore.tools': { 'fastcore.tools.create': ('tools.html#create', 'fastcore/tools.py'),
589+
'fastcore.tools': { 'fastcore.tools._fmt_path': ('tools.html#_fmt_path', 'fastcore/tools.py'),
590+
'fastcore.tools.create': ('tools.html#create', 'fastcore/tools.py'),
590591
'fastcore.tools.get_callable': ('tools.html#get_callable', 'fastcore/tools.py'),
591592
'fastcore.tools.insert': ('tools.html#insert', 'fastcore/tools.py'),
592593
'fastcore.tools.move_lines': ('tools.html#move_lines', 'fastcore/tools.py'),

fastcore/docments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def _escape_markdown(s):
231231
# %% ../nbs/04_docments.ipynb
232232
def _maybe_nm(o):
233233
if (o == inspect._empty): return ''
234-
else: return o.__name__ if hasattr(o, '__name__') else _escape_markdown(str(o))
234+
else: return o.__name__ if hasattr(o, '__name__') else str(o)
235235

236236
# %% ../nbs/04_docments.ipynb
237237
def _list2row(l:list): return '| '+' | '.join([_maybe_nm(o) for o in l]) + ' |'

fastcore/tools.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,26 @@ def valid_path(path:str, must_exist:bool=True) -> Path:
5252
if must_exist and not p.exists(): raise FileNotFoundError(f'File not found: {p}')
5353
return p
5454

55+
# %% ../nbs/12_tools.ipynb
56+
def _fmt_path(f, p):
57+
'Format path with emoji for dirs/symlinks or size for files'
58+
if any(part.startswith('.') for part in f.relative_to(p).parts): return None
59+
if f.is_symlink(): return f'{f} 🔗'
60+
if f.is_dir(): return f'{f} 📁'
61+
return f'{f} ({f.stat().st_size/1024:.1f}k)'
62+
63+
# %% ../nbs/12_tools.ipynb
5564
def view(
5665
path:str, # Path to directory or file to view
57-
view_range:tuple[int,int]=None, # Optional 1-indexed (start, end) line range for files, end=-1 for EOF
66+
view_range:tuple[int,int]=None, # Optional 1-indexed (start, end) line range for files, end=-1 for EOF. Do NOT use unless it's known that the file is too big to keep in context—simply view the WHOLE file when possible
5867
nums:bool=False # Whether to show line numbers
5968
):
6069
'View directory or file contents with optional line range and numbers'
6170
try:
6271
p = valid_path(path)
6372
header = None
6473
if p.is_dir():
65-
files = [str(f) for f in p.glob('**/*')
66-
if not any(part.startswith('.') for part in f.relative_to(p).parts)]
67-
lines = files
74+
lines = [s for f in p.glob('**/*') if (s := _fmt_path(f, p))]
6875
header = f'Directory contents of {p}:'
6976
else: lines = p.read_text().splitlines()
7077
s, e = 1, len(lines)

nbs/04_docments.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,7 @@
16971697
"#| export\n",
16981698
"def _maybe_nm(o):\n",
16991699
" if (o == inspect._empty): return ''\n",
1700-
" else: return o.__name__ if hasattr(o, '__name__') else _escape_markdown(str(o))"
1700+
" else: return o.__name__ if hasattr(o, '__name__') else str(o)"
17011701
]
17021702
},
17031703
{
@@ -1842,7 +1842,7 @@
18421842
"| **Returns** | **int** | | |"
18431843
],
18441844
"text/plain": [
1845-
"DocmentTbl(obj=<function _f at 0x1361a9300>, dm={'a': {'docment': 'description of param a', 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'description of param b', 'anno': <class 'bool'>, 'default': True}, 'c': {'docment': <class 'inspect._empty'>, 'anno': <class 'str'>, 'default': None}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
1845+
"DocmentTbl(obj=<function _f at 0x134bf1440>, dm={'a': {'docment': 'description of param a', 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'description of param b', 'anno': <class 'bool'>, 'default': True}, 'c': {'docment': <class 'inspect._empty'>, 'anno': <class 'str'>, 'default': None}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
18461846
]
18471847
},
18481848
"execution_count": null,
@@ -1920,7 +1920,7 @@
19201920
"| **Returns** | **str** | | **Result of doing it** |"
19211921
],
19221922
"text/plain": [
1923-
"DocmentTbl(obj=<function _f at 0x1361a91c0>, dm={'a': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'param b', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'c': {'docment': 'param c', 'anno': <class 'str'>, 'default': 'foo'}, 'return': {'docment': 'Result of doing it', 'anno': <class 'str'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
1923+
"DocmentTbl(obj=<function _f at 0x134b9b600>, dm={'a': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'param b', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'c': {'docment': 'param c', 'anno': <class 'str'>, 'default': 'foo'}, 'return': {'docment': 'Result of doing it', 'anno': <class 'str'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
19241924
]
19251925
},
19261926
"execution_count": null,
@@ -2013,7 +2013,7 @@
20132013
"| d | bool | True | description of param d |"
20142014
],
20152015
"text/plain": [
2016-
"DocmentTbl(obj=<function _Test.foo at 0x1361a9120>, dm={'c': {'docment': 'description of param c', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'd': {'docment': 'description of param d', 'anno': <class 'bool'>, 'default': True}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['self', 'c', 'd'])"
2016+
"DocmentTbl(obj=<function _Test.foo at 0x134b9a840>, dm={'c': {'docment': 'description of param c', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'd': {'docment': 'description of param d', 'anno': <class 'bool'>, 'default': True}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['self', 'c', 'd'])"
20172017
]
20182018
},
20192019
"execution_count": null,
@@ -2456,7 +2456,7 @@
24562456
"\n",
24572457
"@delegates(f)\n",
24582458
"def g(\n",
2459-
" b:int, # bb\n",
2459+
" b:int|str, # bb\n",
24602460
" **kwargs\n",
24612461
"): return kwargs"
24622462
]
@@ -2473,7 +2473,7 @@
24732473
"```python\n",
24742474
"\n",
24752475
"def g(\n",
2476-
" b:int, # bb\n",
2476+
" b:int | str, # bb\n",
24772477
" a:int=0, # aa\n",
24782478
"):\n",
24792479
"\n",
@@ -2484,7 +2484,7 @@
24842484
"```python\n",
24852485
"\n",
24862486
"def g(\n",
2487-
" b:int, # bb\n",
2487+
" b:int | str, # bb\n",
24882488
" a:int=0, # aa\n",
24892489
"):\n",
24902490
"\n",

nbs/12_tools.ipynb

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@
9595
"name": "stdout",
9696
"output_type": "stream",
9797
"text": [
98-
"000_tour.ipynb\n",
98+
"\u001b[34m__pycache__\u001b[m\u001b[m\n",
99+
"_parallel_win.ipynb\n",
100+
"_quarto.yml\n",
99101
"00_test.ipynb\n",
102+
"000_tour.ipynb\n",
100103
"01_basics.ipynb\n",
101104
"02_foundation.ipynb\n",
102-
"03_xtras.ipynb\n",
103-
"03a_parallel.ipynb\n",
104-
"03b_net.ipynb\n",
105-
"04_docments.ipy\n"
105+
"03_xtras\n"
106106
]
107107
}
108108
],
@@ -128,7 +128,7 @@
128128
"name": "stdout",
129129
"output_type": "stream",
130130
"text": [
131-
"ls: cannot access 'f*': No such file or directory\n",
131+
"ls: f*: No such file or directory\n",
132132
"\n"
133133
]
134134
}
@@ -379,21 +379,44 @@
379379
" 'Return expanded/resolved Path, raising FileNotFoundError if must_exist and missing'\n",
380380
" p = Path(path).expanduser().resolve()\n",
381381
" if must_exist and not p.exists(): raise FileNotFoundError(f'File not found: {p}')\n",
382-
" return p\n",
383-
"\n",
382+
" return p"
383+
]
384+
},
385+
{
386+
"cell_type": "code",
387+
"execution_count": null,
388+
"id": "e967a311",
389+
"metadata": {},
390+
"outputs": [],
391+
"source": [
392+
"#| export\n",
393+
"def _fmt_path(f, p):\n",
394+
" 'Format path with emoji for dirs/symlinks or size for files'\n",
395+
" if any(part.startswith('.') for part in f.relative_to(p).parts): return None\n",
396+
" if f.is_symlink(): return f'{f} 🔗'\n",
397+
" if f.is_dir(): return f'{f} 📁'\n",
398+
" return f'{f} ({f.stat().st_size/1024:.1f}k)'"
399+
]
400+
},
401+
{
402+
"cell_type": "code",
403+
"execution_count": null,
404+
"id": "8e13b7b8",
405+
"metadata": {},
406+
"outputs": [],
407+
"source": [
408+
"#| export\n",
384409
"def view(\n",
385410
" path:str, # Path to directory or file to view\n",
386-
" view_range:tuple[int,int]=None, # Optional 1-indexed (start, end) line range for files, end=-1 for EOF\n",
411+
" view_range:tuple[int,int]=None, # Optional 1-indexed (start, end) line range for files, end=-1 for EOF. Do NOT use unless it's known that the file is too big to keep in context—simply view the WHOLE file when possible\n",
387412
" nums:bool=False # Whether to show line numbers\n",
388413
"):\n",
389414
" 'View directory or file contents with optional line range and numbers'\n",
390415
" try:\n",
391416
" p = valid_path(path)\n",
392417
" header = None\n",
393418
" if p.is_dir():\n",
394-
" files = [str(f) for f in p.glob('**/*') \n",
395-
" if not any(part.startswith('.') for part in f.relative_to(p).parts)]\n",
396-
" lines = files\n",
419+
" lines = [s for f in p.glob('**/*') if (s := _fmt_path(f, p))]\n",
397420
" header = f'Directory contents of {p}:'\n",
398421
" else: lines = p.read_text().splitlines()\n",
399422
" s, e = 1, len(lines)\n",
@@ -462,11 +485,11 @@
462485
"output_type": "stream",
463486
"text": [
464487
"Directory contents of /path:\n",
465-
"/path/000_tour.ipynb\n",
466-
"/path/00_test.ipynb\n",
467-
"/path/01_basics.ipynb\n",
468-
"/path/02_foundation.ipynb\n",
469-
"/path/03_xtras.ipynb\n"
488+
"/path/llms.txt (3.7k)\n",
489+
"/path/000_tour.ipynb (18.2k)\n",
490+
"/path/parallel_test.py (0.6k)\n",
491+
"/path/_quarto.yml (0.8k)\n",
492+
"/path/08_style.ipynb (12.3k)\n"
470493
]
471494
}
472495
],

0 commit comments

Comments
 (0)