Skip to content

Instantly share code, notes, and snippets.

@prakashjayy
Last active November 5, 2020 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prakashjayy/cd3e18c8cc73fd1feed63756624053e7 to your computer and use it in GitHub Desktop.
Save prakashjayy/cd3e18c8cc73fd1feed63756624053e7 to your computer and use it in GitHub Desktop.
fastscript usage
# Fastscript usage under various instancees
# https://github.com/fastai/fastscript/blob/master/00_core.ipynb
# https://fastcore.fast.ai/script.html
from fastcore.all import *
@call_parse
def test_script(p: Param(help="any basic string", type=str)):
print(len(p))
"""
# In settings.ini add params=fastscript:test_script to console scripts
$ params "xxx" > 3
$ params "yyyyy" > 5
$ params --help
```
positional arguments:
p any basic string
```
"""
## bool and choices
@call_parse
def test_script(p: Param(help="any basic string", type=str),
p_bool: Param(help="this is bool", type=bool_arg, default=False),
p_choices: Param(help="this is a choice", type=str, choices=["a", "b", "c"], default="a")):
print(type(p_bool), p_bool)
print(type(p_choices), p_choices)
return len(p)
"""
similar to above
$ params "prakash" --p_bool false --p_choices "f" will through an error as "f" is not present in p_choices
$ params "prakash" --p_bool false --p_choices "a"
```
<class 'bool'> False
<class 'str'> a
```
$ params "prakash"
<class 'function'> <function store_true at 0x7fd273a65830>
<class 'str'> a
```
"""
#Adding nargs too
@call_parse
def test_script(p: Param(help="any basic string", type=str),
p_bool: Param(help="this is bool", type=bool_arg, default=False),
p_choices: Param(help="this is a choice", type=str, choices=["a", "b", "c"], default="a"),
p_nargs_list = Param(help="this is nargs testing", type=str, nargs=2, default=["hey"])): # if you want a dynamic list use "+" instead of 2
print(type(p_bool), p_bool)
print(type(p_choices), p_choices)
print(f"p_nargs_list: {p_nargs_list}")
"""
$ params "prakash" --p_nargs_list "xx" "yy" "mm"
> crowdx_params: error: unrecognized arguments: mm
$ params "prakash" --p_nargs_list "xx" "yy"
> <class 'function'> <function store_true at 0x7f97400e28c0>
> <class 'str'> a
> p_nargs_list: ['xx', 'yy']
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment