Skip to main content

func-to-script` is a lightweight and convenient tool which can be used to turn a Python function into a command line

Project description

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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

func_to_script-1.0.1.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

func_to_script-1.0.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file func_to_script-1.0.1.tar.gz.

File metadata

  • Download URL: func_to_script-1.0.1.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for func_to_script-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a8b3237c2b827115c4ac7aae06c7d6ef9ff54fc8d54ebb6eeba0332bc9eac2a5
MD5 b9d33ba235d63364ebe5047808091675
BLAKE2b-256 2357641927ff8b24964af1185858c9465685ff16e6bb081e5504bff3b726e385

See more details on using hashes here.

File details

Details for the file func_to_script-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: func_to_script-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for func_to_script-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7308d76f74b14f82f76281adb72bdd3c60fe302d0ace86e4d9729a87bc69917
MD5 5a67ed8414731477df895cb4357b78c4
BLAKE2b-256 9764934ce9630ad2742e566bc2426d191a9900b55d02c69eb9b71488fded8663

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page