Skip to main content

Automatically turn functions into executables.

Project description

Automatically turn python functions into executable scripts

Whenever you have written some code and want to run it on a cluster, the first step is to turn it into an executable. This typically leads to a quick stop over at the docs for argparse, together with a lot of duplicated effort in reproducing the function signature. This can be error-prone and annoying, especially if one edits the function.

This script automates this process, using information about types provided either through function annotation, default values in keyword arguments, or type information in numpydoc-style docstrings. Like any shell script, this limits the functions to arguments that can be passed in the terminal, (str, int, float,…).


Let’s say you’ve written the following function in, which provides all kinds of different type information

def add(a: int, b, c=5, d=7., e=None):
"""Some cool addition.

    It's super complicated.
    You know, adding and stuff.

    b : int
        This is the second complicated parameter
        super complicated
    e : int, optional
    if e is None:
        e = 0
    return a + b + c + d + e

Now all you have to do to turn this into an executable is add the following code at the bottom

if __name__ == '__main__':
    import autoexec
    res = autoexec.execute_function(add)

and run chmod +x Now if you run ./ --help you get the following output

❯❯❯ ./ --help
usage: [-h] --a A --b B [--c C] [--d D] [--e E]

Some cool addition.

It's super complicated.
You know, adding and stuff.

optional arguments:
  -h, --help  show this help message and exit
  --a A       int
  --b B       This is the second complicated parameter
              super complicated
  --c C       int, default: 5
  --d D       float, default: 7.0
  --e E       int, default: None

And you’re ready to call the script from the command line

❯❯❯ ./ --a 1 --b 2 --c 3

There is type-checking by argparse

❯❯❯ ./ --a 1 --b stringy
usage: [-h] --a A --b B [--c C] [--d D] [--e E] error: argument --b: invalid int value: 'stringy'

and it complains about missing arguments

❯❯❯ ./ --a 1
usage: [-h] --a A --b B [--c C] [--d D] [--e E] error: the following arguments are required: --b

There is also support for multiple functions via autoexec.execute_functions.

Project details

Download files

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

Files for autoexec, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size autoexec-0.1.1-py3-none-any.whl (6.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size autoexec-0.1.1.tar.gz (4.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page