{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#default_exp core.script" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from local.core.foundation import *\n", "from local.core.utils import *\n", "from local.core.imports import *\n", "from local.test import *\n", "\n", "from argparse import ArgumentParser" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from local.notebook.showdoc import show_doc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "_all_ = ['Param']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## call_parse" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _param_pre(self): return '--' if self.opt else ''\n", "def _param_kwargs(self): return {k:v for k,v in self.__dict__.items() if v is not None and k!='opt'}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "mk_class('Param', help=None, type=None, opt=True, action=None, nargs=None, const=None, choices=None, required=None,\n", " pre=property(_param_pre), kwargs=property(_param_kwargs),\n", " doc=\"A parameter in a function used in `anno_parser` or `call_parse`\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

class Param[source]

\n", "\n", "> Param(**\\*`args`**, **\\*\\*`kwargs`**)\n", "\n", "A parameter in a function used in [`anno_parser`](/script.html#anno_parser) or [`call_parse`](/script.html#call_parse)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(Param, title_level=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'help': 1}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = Param(opt=True, help=1)\n", "p.kwargs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def anno_parser(func):\n", " \"Look at params (annotated with `Param`) in func and return an `ArgumentParser`\"\n", " p = ArgumentParser(description=func.__doc__)\n", " for k,v in inspect.signature(func).parameters.items():\n", " param = func.__annotations__.get(k, Param())\n", " kwargs = param.kwargs\n", " if v.default != inspect.Parameter.empty: kwargs['default'] = v.default\n", " p.add_argument(f\"{param.pre}{k}\", **kwargs)\n", " return p" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def f1(p1:Param(\"h1\", str)='1', p2:Param(\"h2\", float)=1e-3):\n", " \"mydoc\"\n", " pass\n", "\n", "t = anno_parser(f1)\n", "h = t.parse_args(['--p1', 'a'])\n", "test_eq(h.p1, 'a')\n", "test_eq(h.p2, 1e-3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def call_parse(func):\n", " \"Decorator to create a simple CLI from `func` using `anno_parser`\"\n", " name = inspect.currentframe().f_back.f_globals['__name__']\n", " if name == \"__main__\":\n", " args = anno_parser(func).parse_args()\n", " func(**args.__dict__)\n", " else: return func" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export -" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Converted 00_test.ipynb.\n", "Converted 01_core.ipynb.\n", "Converted 01a_utils.ipynb.\n", "Converted 01b_dispatch.ipynb.\n", "Converted 01c_transform.ipynb.\n", "Converted 02_script.ipynb.\n", "Converted 03_torch_core.ipynb.\n", "Converted 04_dataloader.ipynb.\n", "Converted 05_data_core.ipynb.\n", "Converted 06_data_transforms.ipynb.\n", "Converted 07_vision_core.ipynb.\n", "Converted 08_pets_tutorial.ipynb.\n", "Converted 09_vision_augment.ipynb.\n", "Converted 10_data_block.ipynb.\n", "Converted 11_layers.ipynb.\n", "Converted 11a_vision_models_xresnet.ipynb.\n", "Converted 12_optimizer.ipynb.\n", "Converted 13_learner.ipynb.\n", "Converted 14_callback_schedule.ipynb.\n", "Converted 14a_callback_data.ipynb.\n", "Converted 15_callback_hook.ipynb.\n", "Converted 15a_vision_models_unet.ipynb.\n", "Converted 16_callback_progress.ipynb.\n", "Converted 17_callback_tracker.ipynb.\n", "Converted 18_callback_fp16.ipynb.\n", "Converted 19_callback_mixup.ipynb.\n", "Converted 20_metrics.ipynb.\n", "Converted 21_vision_learner.ipynb.\n", "Converted 22_tutorial_imagenette.ipynb.\n", "Converted 23_tutorial_transfer_learning.ipynb.\n", "Converted 30_text_core.ipynb.\n", "Converted 31_text_data.ipynb.\n", "Converted 32_text_models_awdlstm.ipynb.\n", "Converted 33_text_models_core.ipynb.\n", "Converted 34_callback_rnn.ipynb.\n", "Converted 35_tutorial_wikitext.ipynb.\n", "Converted 36_text_models_qrnn.ipynb.\n", "Converted 37_text_learner.ipynb.\n", "Converted 38_tutorial_ulmfit.ipynb.\n", "Converted 40_tabular_core.ipynb.\n", "Converted 41_tabular_model.ipynb.\n", "Converted 42_tabular_rapids.ipynb.\n", "Converted 50_data_block_examples.ipynb.\n", "Converted 60_medical_imaging.ipynb.\n", "Converted 65_medical_text.ipynb.\n", "Converted 90_notebook_core.ipynb.\n", "Converted 91_notebook_export.ipynb.\n", "Converted 92_notebook_showdoc.ipynb.\n", "Converted 93_notebook_export2html.ipynb.\n", "Converted 94_notebook_test.ipynb.\n", "Converted 95_index.ipynb.\n", "Converted 96_data_external.ipynb.\n", "Converted 97_utils_test.ipynb.\n", "Converted notebook2jekyll.ipynb.\n" ] } ], "source": [ "#hide\n", "from local.notebook.export import notebook2script\n", "notebook2script(all_fs=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }