Skip to content

Commit 31e072d

Browse files
committed
fix pdb
1 parent 9b75a3e commit 31e072d

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"true": "02_utils.ipynb",
7676
"gen": "02_utils.ipynb",
7777
"chunked": "02_utils.ipynb",
78+
"otherwise": "02_utils.ipynb",
7879
"AttrDict": "02_utils.ipynb",
7980
"dict2obj": "02_utils.ipynb",
8081
"with_cast": "02_utils.ipynb",

fastcore/script.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def anno_parser(func, prog=None, from_name=False):
5050
# Cell
5151
def args_from_prog(func, prog):
5252
"Extract args from `prog`"
53+
if prog is None or '#' not in prog: return {}
5354
if '##' in prog: _,prog = prog.split('##', 1)
5455
progsp = prog.split("#")
5556
args = {progsp[i]:progsp[i+1] for i in range(0, len(progsp), 2)}
@@ -69,10 +70,10 @@ def _f(*args, **kwargs):
6970
mod = inspect.getmodule(inspect.currentframe().f_back)
7071
if not mod: return func(*args, **kwargs)
7172
p = anno_parser(func)
72-
args = p.parse_args().__dict
73-
if args.pop('pdb_debug', False): func = trace(func)
74-
xtra = otherwise(args.pop('xtra', eq(1), p.prog))
75-
func(**merge(args, args_from_prog(func, xtra)))
73+
args = p.parse_args().__dict__
74+
xtra = otherwise(args.pop('xtra', ''), eq(1), p.prog)
75+
tfunc = trace(func) if args.pop('pdb', False) else func
76+
tfunc(**merge(args, args_from_prog(func, xtra)))
7677

7778
if mod.__name__=="__main__":
7879
setattr(mod, func.__name__, _f)

fastcore/utils.py

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

33
__all__ = ['ifnone', 'maybe_attr', 'basic_repr', 'get_class', 'mk_class', 'wrap_class', 'ignore_exceptions',
44
'exec_local', 'risinstance', 'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul',
5-
'truediv', 'is_', 'is_not', 'in_', 'true', 'gen', 'chunked', 'AttrDict', 'dict2obj', 'with_cast',
6-
'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', 'class2attr', 'hasattrs', 'setattrs',
7-
'ShowPrint', 'Int', 'Str', 'Float', 'tuplify', 'detuplify', 'replicate', 'uniqueify', 'setify', 'merge',
8-
'is_listy', 'range_of', 'groupby', 'last_index', 'shufflish', 'IterLen', 'ReindexCollection', 'num_methods',
9-
'rnum_methods', 'inum_methods', 'fastuple', 'trace', 'compose', 'maps', 'partialler', 'mapped',
10-
'instantiate', 'using_attr', 'Self', 'Self', 'open_file', 'save_pickle', 'load_pickle', 'bunzip',
5+
'truediv', 'is_', 'is_not', 'in_', 'true', 'gen', 'chunked', 'otherwise', 'AttrDict', 'dict2obj',
6+
'with_cast', 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', 'class2attr', 'hasattrs',
7+
'setattrs', 'ShowPrint', 'Int', 'Str', 'Float', 'tuplify', 'detuplify', 'replicate', 'uniqueify', 'setify',
8+
'merge', 'is_listy', 'range_of', 'groupby', 'last_index', 'shufflish', 'IterLen', 'ReindexCollection',
9+
'num_methods', 'rnum_methods', 'inum_methods', 'fastuple', 'trace', 'compose', 'maps', 'partialler',
10+
'mapped', 'instantiate', 'using_attr', 'Self', 'Self', 'open_file', 'save_pickle', 'load_pickle', 'bunzip',
1111
'join_path_file', 'urlread', 'urljson', 'run', 'do_request', 'sort_by_run', 'PrettyString', 'round_multiple',
1212
'even_mults', 'num_cpus', 'add_props', 'ContextManagers', 'typed', 'set_num_threads', 'ProcessPoolExecutor',
1313
'ThreadPoolExecutor', 'parallel', 'run_procs', 'parallel_gen', 'threaded']
@@ -166,6 +166,11 @@ def chunked(it, chunk_sz=None, drop_last=False, n_chunks=None):
166166
if res and (len(res)==chunk_sz or not drop_last): yield res
167167
if len(res)<chunk_sz: return
168168

169+
# Cell
170+
def otherwise(x, tst, y):
171+
"`y if tst(x) else x`"
172+
return y if tst(x) else x
173+
169174
# Cell
170175
class AttrDict(dict):
171176
"`dict` subclass that also provides access to keys as attrs"

nbs/02_utils.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@
852852
"metadata": {},
853853
"outputs": [],
854854
"source": [
855+
"#export\n",
855856
"def otherwise(x, tst, y):\n",
856857
" \"`y if tst(x) else x`\"\n",
857858
" return y if tst(x) else x"

nbs/07_script.ipynb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280
"#export\n",
281281
"def args_from_prog(func, prog):\n",
282282
" \"Extract args from `prog`\"\n",
283+
" if prog is None or '#' not in prog: return {}\n",
283284
" if '##' in prog: _,prog = prog.split('##', 1)\n",
284285
" progsp = prog.split(\"#\")\n",
285286
" args = {progsp[i]:progsp[i+1] for i in range(0, len(progsp), 2)}\n",
@@ -324,10 +325,10 @@
324325
" mod = inspect.getmodule(inspect.currentframe().f_back)\n",
325326
" if not mod: return func(*args, **kwargs)\n",
326327
" p = anno_parser(func)\n",
327-
" args = p.parse_args().__dict\n",
328-
" if args.pop('pdb_debug', False): func = trace(func)\n",
329-
" xtra = otherwise(args.pop('xtra', eq(1), p.prog))\n",
330-
" func(**merge(args, args_from_prog(func, xtra)))\n",
328+
" args = p.parse_args().__dict__\n",
329+
" xtra = otherwise(args.pop('xtra', ''), eq(1), p.prog)\n",
330+
" tfunc = trace(func) if args.pop('pdb', False) else func\n",
331+
" tfunc(**merge(args, args_from_prog(func, xtra)))\n",
331332
"\n",
332333
" if mod.__name__==\"__main__\":\n",
333334
" setattr(mod, func.__name__, _f)\n",

0 commit comments

Comments
 (0)