Skip to main content

func-to-script: Quickly turn a function into a command-line script

func-to-script is a lightweight and convenient tool which can be used to turn a Python function into a command line script, with minimal boilerplate!

As func-to-script is only a thin wrapper around argparse, it is incredibly lightweight there are no additional dependencies required!

func-to-script is designed to be used in simple cases, so offers a streamlined feature set. For more complex scenarios, it is recommended to use argparse directly.

Installation

func-to-script can be installed from pip using the following command:

pip install func-to-script

Usage

To get started, all that you need to do is add the script decorator to the function that you wish to convert, as demonstrated below:

# say_hello_script.py

from func_to_script import script

@script
def say_hello(
    greeting: str = "Hello", name: str = "World", print_message: bool = True
):
    """
    A simple function to say hello

    :param greeting: the greeting to use
    :param name: the person to greet
    :param print_message: flag to indicate whether to print to the command line
    """
    hello_str = f"{greeting}, {name}"

    if print_message:
        print(hello_str)

    return hello_str


if __name__ == "__main__":
    say_hello()

We can now call this like so:

python say_hello_script.py --greeting hi --print_message False

As func-to-script uses regular Python type hints and default value syntax, no additional changes are required! If a docstring is provided, func-to-script will also attempt to parse this to provide descriptions of the required parameters to display when using:

python say_hello_script.py -h

The script decorator does not affect normal function usage, so decorated functions can still be imported and called elsewhere:

from say_hello_script import say_hello


def main():
    say_hello(print_message=True)


if __name__ == "__main__":
    main()

Using YAML Configuration Files

For more complex applications, configuration can be loaded from YAML files. func-to-script provides a simple way to convert YAML configs into accessible Python objects:

# config.yaml
data:
  num_classes: 80
  max_instances: 65
  image_size: 640

# train_script.py
from func_to_script import load_config_from_yaml, script

@script
def train(
    config_file: str = "config.yaml",
):
    """
    Train a model using parameters from a config file
    :param config_file: path to YAML configuration file
    """
    config = load_config_from_yaml(config_file)

    # Access config values using dot notation
    print(config.data.num_classes)  # prints: 80

    # Or use dictionary-style access
    print(config["data"]["num_classes"])  # prints: 80

    # Mix both styles as needed
    print(config.data["max_instances"])  # prints: 65

    # Use .get() for safe access with defaults
    batch_size = config.get("training", {}).get("batch_size", 32)

We can now call this like so:

python train_script.py --config_file my_config.yaml

Constraints

  • Only four types are supported for script level arguments: str, int, float, bool
  • When calling a function as a script, arguments must be given as keyword arguments; positional arguments are not supported by design, to promote clarity.
  • To be able to parse documentation, docstrings must be written in sphinx format. If this is not the case, the docstring will be ignored.

Aren't there a bunch of libraries that do this already

There are, and most of those contain more functionality than is contained here! However, most of these tend to require interacting with custom objects or adding additional boilerplate; func-to-script is able to avoid this for simple use cases.

Metadata

Release files for func-to-script 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for func-to-script 1.1.0
File Size Uploaded
func_to_script-1.1.0.tar.gz 26.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for func-to-script 1.1.0
File Interpreter ABI Platform
func_to_script-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.3 kB

Release files / func_to_script-1.1.0.tar.gz

Download URL func_to_script-1.1.0.tar.gz
Size 26.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4268270510d227ca910b57fc9b75744e3656cba24a924ca681077d2425d10e19
BLAKE2b-256 checksum
How to use checksums
6087092d491d2c33c53602889b3f063b43be7d0c5840278aad811938217139b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release files / func_to_script-1.1.0-py3-none-any.whl

Download URL func_to_script-1.1.0-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b61cfbe65c71b21db06c5b659a9d6b5a49bc5409b05cc7d31df673d221456c6
BLAKE2b-256 checksum
How to use checksums
9ef4107f73daac5290f481a0e7b1c2a1fefe64354cde85a89976c665813a7d7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page