All Projects → fastai → Fastscript

fastai / Fastscript

Licence: apache-2.0
A fast way to turn your python function into a script

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects
script
160 projects

Labels

Projects that are alternatives of or similar to Fastscript

Pomd
🍅 A good old cli based Pomodoro timer with native notifications
Stars: ✭ 151 (-2.58%)
Mutual labels:  cli
Sherlock Js
Find usernames across over 170 social networks - Fast & flexible remake of sdushantha/sherlock
Stars: ✭ 153 (-1.29%)
Mutual labels:  cli
Tsrc
Manage groups of git repositories
Stars: ✭ 154 (-0.65%)
Mutual labels:  cli
Defopt
Effortless argument parser
Stars: ✭ 150 (-3.23%)
Mutual labels:  cli
Simplesh
🐧 Quick installation for Ubuntu terminal.
Stars: ✭ 152 (-1.94%)
Mutual labels:  cli
Pully
A simple CLI and library for downloading high quality YouTube videos!
Stars: ✭ 153 (-1.29%)
Mutual labels:  cli
Multiprocess
🚀Easy to make the common PHP/Python/js...script change daemon and multi-process execution
Stars: ✭ 151 (-2.58%)
Mutual labels:  cli
Psql2csv
Run a query in psql and output the result as CSV.
Stars: ✭ 153 (-1.29%)
Mutual labels:  cli
Go Audio
An offline solution to convert pdfs into audiobooks
Stars: ✭ 153 (-1.29%)
Mutual labels:  cli
Cbox
convert any python function to unix-style command
Stars: ✭ 154 (-0.65%)
Mutual labels:  cli
Deno Cliffy
Command line framework for deno 🦕 Including Commandline-Interfaces, Prompts, CLI-Table, Arguments Parser and more...
Stars: ✭ 149 (-3.87%)
Mutual labels:  cli
Grafcli
Grafana CLI for quick and easy dashboards management.
Stars: ✭ 152 (-1.94%)
Mutual labels:  cli
React Native Rename
Rename react-native app with just one command
Stars: ✭ 2,033 (+1211.61%)
Mutual labels:  cli
Gitin
commit/branch/workdir explorer for git
Stars: ✭ 1,815 (+1070.97%)
Mutual labels:  cli
Sqsmover
AWS SQS Message mover
Stars: ✭ 154 (-0.65%)
Mutual labels:  cli
Ffpb
A progress bar for ffmpeg. Yay !
Stars: ✭ 149 (-3.87%)
Mutual labels:  cli
Gloria
Gloria is a static website generator, based on NodeJS.
Stars: ✭ 153 (-1.29%)
Mutual labels:  cli
Handlr
A better xdg-utils
Stars: ✭ 151 (-2.58%)
Mutual labels:  cli
Upx
UPYUN Storage Command Tool
Stars: ✭ 154 (-0.65%)
Mutual labels:  cli
Aeneas
aeneas is a Python/C library and a set of tools to automagically synchronize audio and text (aka forced alignment)
Stars: ✭ 1,942 (+1152.9%)
Mutual labels:  cli

fastscript - DEPRECATED

Please use fastcore.script instead

All functionality from this module has been moved to fastcore.script. Please use that instead.

Install

Either pip install fastscript or conda install -c fastai fastscript

Overview

Sometimes, 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 use argparse for this purpose, which comes with Python, but it's complex and verbose.

fastscript makes life easier. There are much fancier modules to help you write scripts (we recommend Python Fire, and Click is 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 around argparse that uses modern Python features and some thoughtful defaults to get rid of the boilerplate.

For full details, see the docs for core.

Example

Here's a complete example - it's provided in the fastscript repo as examples/test_fastscript.py:

from fastscript import *
@call_parse
def main(msg:Param("The message", str),
         upper:Param("Convert to uppercase?", bool_arg)=False):
    print(msg.upper() if upper else msg)

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: msg

As you see, we didn't need any if __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!

Param

Each parameter in your function should have an annotation Param(...) (as in the example above). You can pass the following when calling Param: help,type,opt,action,nargs,const,choices,required . 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). 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 fastscript 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.

setuptools scripts

There's a really nice feature of pip/setuptools that lets you create commandline scripts directly from functions, makes them available in the PATH, and even makes your scripts cross-platform (e.g. in Windows it creates an exe). fastscript supports this feature too. To use it, follow this example from fastscript/test_cli.py in 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 a console_scripts section 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 type test_fastscript at 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 a setup.py yourself. Instead, just use nbdev. Then modify settings.ini as appropriate for your module/script. To install your script directly, you can type pip install -e .. Your script, when installed this way (it's called an editable install, will automatically be up to date even if you edit it - there's no need to reinstall it after editing. With nbdev you can even make your module and script available for installation directly from pip and conda by running make release.

Importing Command Line Functions

Sometimes it might be useful to also be able to import command-line functions and use them as regular functions. You can do that just fine with fastscript:

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].