Package: fastscript
fastscriptA fast way to turn your python function into a script.Part offast.ai's toolkit for delightful developer experiences. Written by Jeremy Howard.Installpip install fastscriptOverviewSometimes, you want to create a quick script, either for yourself, or for others. But in Python, that involves a whole lot of boilerplate and ceremony, especially if you want to support command line arguments, provide help, and other niceties. You can useargparsefor this purpose, which comes with Python, but it's complex and verbose.fastscriptmakes life easier. There are much fancier modules to help you write scripts (we recommendPython Fire, andClickis also popular), but fastscript is very fast and very simple. In fact, it's <50 lines of code! Basically, it's just a little wrapper aroundargparsethat uses modern Python features and some thoughtful defaults to get rid of the boilerplate.ExampleHere's a complete example - it's provided in the fastscript repo asexamples/test_fastscript.py:fromfastscriptimport*@call_parsedefmain(msg:Param("The message",str),upper:Param("Convert to uppercase?",bool_arg)=False):print(msg.upper()ifupperelsemsg)When you run this script, you'll see:$ python examples/test_fastscript.py
usage: test_fastscript.py [-h] [--upper UPPER] msg
test_fastscript.py: error: the following arguments are required: msgAs you see, we didn't need anyif __name__ == "__main__", we didn't have to parse arguments, we just wrote a function, added a decorator to it, and added some annotations to our function's parameters. As a bonus, we can also use this function directly from a REPL such as Jupyter Notebook - it's not just for command line scripts!ParamEach parameter in your function should have an annotationParam(...)(as in the example above). You can pass the following when callingParam:help,type,opt,action,nargs,const,choices,required. Except foropt, all of these are just passed directly toargparse, so you have all the power of that module at your disposal. Generally you'll want to pass at leasthelp(since this is provided as the help string for that parameter) andtype(to ensure that you get the type of data you expect).optis a bool that defines whether a param is optional or required (positional) - but you'll generally not need to set this manually, because fastscript will set it for you automatically based ondefaultvalues.You should provide a default (after the=) for anyoptionalparameters. If you don't provide a default for a parameter, then it will be apositionalparameter.setuptools scriptsThere's a really nice feature of pip/setuptools that lets you create commandline scripts directly from functions, makes them available in thePATH, and even makes your scripts cross-platform (e.g. in Windows it creates an exe). fastscript supports this feature too. To use it, followthis examplefromfastscript/test_cli.pyin the repo. As you see, it's basically identical to the script example above, except that we can treat it as a module. The trick to making this available as a script is to add aconsole_scriptssection to your setup file, of the form:script_name=module:function_name. E.g. in this case we use:test_fastscript=fastscript.test_cli:main. With this, you can then just typetest_fastscriptat any time, from any directory, and your script will be called (once it's installed using one of the methods below).You don't actually have to write asetup.pyyourself. Instead, just copy the setup.py we have in the fastscript repo, and copysettings.inias well. Then modifysettings.inias appropriate for your module/script. Then, to install your script directly, you can typepip install -e .. Your script, when installed this way (it's called aneditable install, will automatically be up to date even if you edit it - there's no need to reinstall it after editing.You can even make your module and script available for installation directly from pip by runningmake. There shouldn't be anything else to edit - you just need to make sure you have an account onpypiand have set up a.pypirc file.Importing Command Line FunctionsSometimes it might be useful to also be able to import command-line functions and use them as regular functions.from fastscript.test_cli import main
main("This can also be used as a regular imported function.", upper=True)'THIS CAN ALSO BE USED AS A REGULAR IMPORTED FUNCTION.'