Skip to main content

python function to command translator

Project description

https://travis-ci.org/podhmo/handofcats.svg

A tiny Converter that making executable command script from python function. If the function is type annotated, it is also used.

Please using as_command() decorator.

as_command()

greeting.py

from handofcats import as_command

@as_command
def greeting(message: str, is_surprised: bool = False, name: str = "foo") -> None:
    """greeting message"""
    suffix = "!" if is_surprised else ""
    print("{name}: {message}{suffix}".format(name=name, message=message, suffix=suffix))
$ python greeting.py -h
usage: greeting.py [-h] [--expose] [--is-surprised] [--name NAME] message

greeting message

positional arguments:
  message

optional arguments:
  -h, --help      show this help message and exit
  --expose
  --is-surprised
  --name NAME

$ python greeting.py hello
foo: hello
$ python greeting.py --is-surprised hello
foo: hello!
$ python greeting.py --is-surprised --name=bar bye
bar: bye!

(TODO: detail description)

–expose

calling with –expose option, generationg the code that dropping dependencies of handofcats module.

$ python greeting.py --expose
def greeting(message: str, is_surprised: bool = False, name: str = "foo") -> None:
    """greeting message"""
    suffix = "!" if is_surprised else ""
    print("{name}: {message}{suffix}".format(name=name, message=message, suffix=suffix))

def main(argv=None):
    import argparse
    parser = argparse.ArgumentParser(description='greeting message')
    parser.print_usage = parser.print_help
    parser.add_argument('message')
    parser.add_argument('--is-surprised', action='store_true')
    parser.add_argument('--name', default='foo', required=False)
    args = parser.parse_args(argv)
    greeting(**vars(args))


if __name__ == '__main__':
    main()

–inplace

With inplace option, when –expose, overwrite target source.

handofcats command

sum.py

def sum(x: int, y: int) -> None:
    print(f"{x} + {y} = {x + y}")

It is also ok, calling the function that not decorated via handofcats command.

$ handofcats sum.py:sum 10 20
10 + 20 = 30

$ handofcats sum.py:sum -h
handofcats sum.py:sum -h
usage: handofcats [-h] [--expose] x y

positional arguments:
  x
  y

optional arguments:
  -h, --help  show this help message and exit
  --expose

experimental

sequences

from typing import List, Optional

def psum(xs: List[int], *, ys: Optional[List[int]] = None):
    # treated as
    # parser.add_argument('xs', nargs='*', type=int)
    # parser.add_argument('--ys', action='append', required=False, type=int)
    ..

choices

from typing import NewType

DumpFormat = NewType("DumpFormat", str)
DumpFormat.choices = ["json", "csv"]   # this: (experimental)


def run(*, format: DumpFormat = "json"):
    # treated as
    # parser.add_argument("--format", defaul="json", choices=("json", "csv"), required=False)
    ...

2.3.0

  • support Literal types

  • fix, error is occured, running with from __future__ import annotations

  • fix, generated code is invalid when positional arguments with “_”

  • fix, unable to use the function named “main”

  • –typed option, with –expose

2.2.0

  • –inplace option, with –expose

2.1.0

  • choices function

  • fix bug that it is not working, importing with physical filepath

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

handofcats-2.3.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file handofcats-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: handofcats-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.4

File hashes

Hashes for handofcats-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 77a33b1391d25bfeaf1301818f3aee933f66ecdbe83596845210ca63748ab6ca
MD5 043f8e311123be2c911496197ce646be
BLAKE2b-256 a599a2eb8606e418446ae959f3a832c1ffa4cb9c5849896104f261b3d661141a

See more details on using hashes here.

Supported by

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