diff --git a/fastcore/script.py b/fastcore/script.py index 360ac213..a692bb76 100644 --- a/fastcore/script.py +++ b/fastcore/script.py @@ -4,7 +4,8 @@ 'SCRIPT_INFO', 'call_parse'] # Cell -import inspect,functools,argparse,shutil +import inspect,argparse,shutil +from functools import wraps,partial from .imports import * from .utils import * from .docments import docments @@ -97,12 +98,9 @@ def args_from_prog(func, prog): # Cell def call_parse(func=None, nested=False): "Decorator to create a simple CLI from `func` using `anno_parser`" - if func is None: return functools.partial(call_parse, nested=nested) + if func is None: return partial(call_parse, nested=nested) - mod = inspect.getmodule(inspect.currentframe().f_back) - if not mod: return func - - @functools.wraps(func) + @wraps(func) def _f(*args, **kwargs): mod = inspect.getmodule(inspect.currentframe().f_back) if not mod: return func(*args, **kwargs) @@ -114,9 +112,10 @@ def _f(*args, **kwargs): args = args.__dict__ xtra = otherwise(args.pop('xtra', ''), eq(1), p.prog) tfunc = trace(func) if args.pop('pdb', False) else func - tfunc(**merge(args, args_from_prog(func, xtra))) + return tfunc(**merge(args, args_from_prog(func, xtra))) - if mod.__name__=="__main__": + mod = inspect.getmodule(inspect.currentframe().f_back) + if getattr(mod, __name__, '') =="__main__": setattr(mod, func.__name__, _f) SCRIPT_INFO.func = func.__name__ return _f() diff --git a/nbs/03_xtras.ipynb b/nbs/03_xtras.ipynb index fcfbb512..0e617f58 100644 --- a/nbs/03_xtras.ipynb +++ b/nbs/03_xtras.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -97,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -134,7 +134,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -143,7 +143,7 @@ "(#9) ['./08_script.ipynb','./04_dispatch.ipynb','./06_docments.ipynb','./01_basics.ipynb','./fastcore/docments.py','./fastcore/dispatch.py','./fastcore/basics.py','./fastcore/docscrape.py','./fastcore/script.py']" ] }, - "execution_count": 13, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -154,7 +154,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -176,7 +176,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -198,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -231,7 +231,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -240,7 +240,7 @@ "'jpeg'" ] }, - "execution_count": 17, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -259,7 +259,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -279,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -295,7 +295,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -311,7 +311,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -320,7 +320,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -351,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -360,7 +360,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -378,7 +378,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -393,7 +393,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -408,7 +408,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -427,7 +427,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -445,7 +445,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -460,7 +460,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -474,7 +474,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -499,7 +499,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -522,7 +522,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -539,7 +539,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -555,7 +555,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -565,7 +565,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -579,7 +579,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -589,7 +589,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -627,7 +627,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -636,7 +636,7 @@ "'pip 21.2.4 from /Users/hamel/opt/anaconda3/lib/python3.9/site-packages/pip (python 3.9)'" ] }, - "execution_count": 39, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -649,7 +649,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -672,7 +672,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -691,7 +691,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -704,7 +704,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -722,7 +722,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -735,7 +735,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -748,7 +748,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -772,7 +772,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -793,7 +793,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -812,7 +812,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1565,7 +1565,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1608,7 +1608,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1617,7 +1617,7 @@ "'https://github.com/fastai/fastcore/tree/master/fastcore/test.py#L34'" ] }, - "execution_count": 62, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -2389,18 +2389,6 @@ "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.7" } }, "nbformat": 4, diff --git a/nbs/08_script.ipynb b/nbs/08_script.ipynb index 5410585d..5bc9199d 100644 --- a/nbs/08_script.ipynb +++ b/nbs/08_script.ipynb @@ -147,7 +147,8 @@ "outputs": [], "source": [ "#export\n", - "import inspect,functools,argparse,shutil\n", + "import inspect,argparse,shutil\n", + "from functools import wraps,partial\n", "from fastcore.imports import *\n", "from fastcore.utils import *\n", "from fastcore.docments import docments" @@ -528,12 +529,9 @@ "#export\n", "def call_parse(func=None, nested=False):\n", " \"Decorator to create a simple CLI from `func` using `anno_parser`\"\n", - " if func is None: return functools.partial(call_parse, nested=nested)\n", + " if func is None: return partial(call_parse, nested=nested)\n", "\n", - " mod = inspect.getmodule(inspect.currentframe().f_back)\n", - " if not mod: return func\n", - "\n", - " @functools.wraps(func)\n", + " @wraps(func)\n", " def _f(*args, **kwargs):\n", " mod = inspect.getmodule(inspect.currentframe().f_back)\n", " if not mod: return func(*args, **kwargs)\n", @@ -545,9 +543,10 @@ " args = args.__dict__\n", " xtra = otherwise(args.pop('xtra', ''), eq(1), p.prog)\n", " tfunc = trace(func) if args.pop('pdb', False) else func\n", - " tfunc(**merge(args, args_from_prog(func, xtra)))\n", + " return tfunc(**merge(args, args_from_prog(func, xtra)))\n", "\n", - " if mod.__name__==\"__main__\":\n", + " mod = inspect.getmodule(inspect.currentframe().f_back)\n", + " if getattr(mod, __name__, '') ==\"__main__\":\n", " setattr(mod, func.__name__, _f)\n", " SCRIPT_INFO.func = func.__name__\n", " return _f()\n",