|
189 | 189 | " return str2bool(v)" |
190 | 190 | ] |
191 | 191 | }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": null, |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [], |
| 197 | + "source": [ |
| 198 | + "#export\n", |
| 199 | + "def clean_type_str(x:str):\n", |
| 200 | + " x = str(x)\n", |
| 201 | + " x = re.sub(\"(class|function|__main__\\.|\\ at.*)\", '', x)\n", |
| 202 | + " x = re.sub(\"(<|>|'|\\ )\", '', x) # spl characters\n", |
| 203 | + " return x" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": null, |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [], |
| 211 | + "source": [ |
| 212 | + "class Test: pass\n", |
| 213 | + "\n", |
| 214 | + "test_eq(clean_type_str(argparse.ArgumentParser), 'argparse.ArgumentParser')\n", |
| 215 | + "test_eq(clean_type_str(Test), 'Test')\n", |
| 216 | + "test_eq(clean_type_str(int), 'int')\n", |
| 217 | + "test_eq(clean_type_str(float), 'float')\n", |
| 218 | + "test_eq(clean_type_str(store_false), 'store_false')" |
| 219 | + ] |
| 220 | + }, |
192 | 221 | { |
193 | 222 | "cell_type": "code", |
194 | 223 | "execution_count": null, |
|
214 | 243 | " def pre(self): return '--' if self.opt else ''\n", |
215 | 244 | " @property\n", |
216 | 245 | " def kwargs(self): return {k:v for k,v in self.__dict__.items()\n", |
217 | | - " if v is not None and k!='opt' and k[0]!='_'}" |
| 246 | + " if v is not None and k!='opt' and k[0]!='_'}\n", |
| 247 | + " def __repr__(self):\n", |
| 248 | + " if self.help is None and self.type is None: return \"\"\n", |
| 249 | + " if self.help is None and self.type is not None: return f\"{clean_type_str(self.type)}\"\n", |
| 250 | + " if self.help is not None and self.type is None: return f\"<{self.help}>\"\n", |
| 251 | + " if self.help is not None and self.type is not None: return f\"{clean_type_str(self.type)} <{self.help}>\"" |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "code", |
| 256 | + "execution_count": null, |
| 257 | + "metadata": {}, |
| 258 | + "outputs": [], |
| 259 | + "source": [ |
| 260 | + "test_eq(repr(Param(\"Help goes here\")), '<Help goes here>')\n", |
| 261 | + "test_eq(repr(Param(\"Help\", int)), 'int <Help>')\n", |
| 262 | + "test_eq(repr(Param(help=None, type=int)), 'int')\n", |
| 263 | + "test_eq(repr(Param(help=None, type=None)), '')" |
218 | 264 | ] |
219 | 265 | }, |
220 | 266 | { |
|
223 | 269 | "source": [ |
224 | 270 | "Each parameter in your function should have an annotation `Param(...)`. You can pass the following when calling `Param`: `help`,`type`,`opt`,`action`,`nargs`,`const`,`choices`,`required` (i.e. it takes the same parameters as `argparse.ArgumentParser.add_argument`, plus `opt`). Except for `opt`, all of these are just passed directly to `argparse`, so you have all the power of that module at your disposal. Generally you'll want to pass at least `help` (since this is provided as the help string for that parameter) and `type` (to ensure that you get the type of data you expect).\n", |
225 | 271 | "\n", |
226 | | - "`opt` is a bool that defines whether a param is optional or required (positional) - but you'll generally not need to set this manually, because fastcore.script will set it for you automatically based on *default* values. You should provide a default (after the `=`) for any *optional* parameters. If you don't provide a default for a parameter, then it will be a *positional* parameter." |
| 272 | + "`opt` is a bool that defines whether a param is optional or required (positional) - but you'll generally not need to set this manually, because fastcore.script will set it for you automatically based on *default* values. You should provide a default (after the `=`) for any *optional* parameters. If you don't provide a default for a parameter, then it will be a *positional* parameter.\n", |
| 273 | + "\n", |
| 274 | + "Param's `__repr__` also allows for more informative function annotation when looking up the function's doc using shift+tab. You see the type annotation (if there is one) and the accompanying help documentation with it." |
| 275 | + ] |
| 276 | + }, |
| 277 | + { |
| 278 | + "cell_type": "code", |
| 279 | + "execution_count": null, |
| 280 | + "metadata": {}, |
| 281 | + "outputs": [], |
| 282 | + "source": [ |
| 283 | + "def f(required:Param(\"Required param\", int),\n", |
| 284 | + " a:Param(\"param 1\", bool_arg),\n", |
| 285 | + " b:Param(\"param 2\", str)=\"test\"):\n", |
| 286 | + " \"my docs\"\n", |
| 287 | + " ..." |
| 288 | + ] |
| 289 | + }, |
| 290 | + { |
| 291 | + "cell_type": "code", |
| 292 | + "execution_count": null, |
| 293 | + "metadata": {}, |
| 294 | + "outputs": [], |
| 295 | + "source": [ |
| 296 | + "f?" |
227 | 297 | ] |
228 | 298 | }, |
229 | 299 | { |
|
431 | 501 | "Converted 03_xtras.ipynb.\n", |
432 | 502 | "Converted 04_dispatch.ipynb.\n", |
433 | 503 | "Converted 05_transform.ipynb.\n", |
| 504 | + "Converted 06_logargs.ipynb.\n", |
434 | 505 | "Converted 07_meta.ipynb.\n", |
435 | 506 | "Converted 08_script.ipynb.\n", |
436 | 507 | "Converted index.ipynb.\n" |
|
0 commit comments