Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created February 8, 2020 11:49
Show Gist options
  • Save podhmo/9437d27a3c35213b2293a50511b5647e to your computer and use it in GitHub Desktop.
Save podhmo/9437d27a3c35213b2293a50511b5647e to your computer and use it in GitHub Desktop.
from handofcats import as_command
@as_command
def hello(*, name="world") -> str:
return f"hello, {name}"
from handofcats import as_command
@as_command
def nums() -> list:
return list(range(10))
from handofcats import as_command, Config
import json
@as_command(
config=Config(cont=lambda x: print(json.dumps(x, indent=2, ensure_ascii=False)))
)
def people() -> list:
import faker
faker.Faker.seed(0)
fake = faker.Faker("ja_JP")
return [{"no": i, "name": fake.name(), "address": fake.address()} for i in range(5)]
def _output(x):
import json
print(json.dumps(x, indent=2, ensure_ascii=False))
def people() -> list:
import faker
faker.Faker.seed(0)
fake = faker.Faker("ja_JP")
return [{"no": i, "name": fake.name(), "address": fake.address()} for i in range(5)]
--- output.full.py 2020-02-08 20:46:14.000000000 +0900
+++ output.simple.py 2020-02-08 20:46:14.000000000 +0900
@@ -1,13 +1,13 @@
-import typing as t
+
def hello(*, name="world") -> str:
return f"hello, {name}"
-def main(argv: t.Optional[t.List[str]] = None) -> t.Any:
+def main(argv=None):
import argparse
- parser = argparse.ArgumentParser(prog=hello.__name__, description=hello.__doc__, formatter_class=type('_HelpFormatter', (argparse.ArgumentDefaultsHelpFormatter, argparse.RawTextHelpFormatter), {}))
+ parser = argparse.ArgumentParser(prog=hello.__name__, description=hello.__doc__)
parser.print_usage = parser.print_help # type: ignore
parser.add_argument('--name', required=False, default='world', help='-')
args = parser.parse_args(argv)
00:
python $(shell echo $@*.py)
01:
python $(shell echo $@*.py)
02:
python $(shell echo $@*.py)
03:
python -m handofcats $(shell echo $@*.py) people --cont=$(shell echo $@*.py):_output
diff:
python 00*.py --expose > output.full.py
python 00*.py --expose --simple > output.simple.py
diff -u output.full.py output.simple.py
import typing as t
def hello(*, name="world") -> str:
return f"hello, {name}"
def main(argv: t.Optional[t.List[str]] = None) -> t.Any:
import argparse
parser = argparse.ArgumentParser(prog=hello.__name__, description=hello.__doc__, formatter_class=type('_HelpFormatter', (argparse.ArgumentDefaultsHelpFormatter, argparse.RawTextHelpFormatter), {}))
parser.print_usage = parser.print_help # type: ignore
parser.add_argument('--name', required=False, default='world', help='-')
args = parser.parse_args(argv)
params = vars(args).copy()
return hello(**params)
if __name__ == '__main__':
main()
def hello(*, name="world") -> str:
return f"hello, {name}"
def main(argv=None):
import argparse
parser = argparse.ArgumentParser(prog=hello.__name__, description=hello.__doc__)
parser.print_usage = parser.print_help # type: ignore
parser.add_argument('--name', required=False, default='world', help='-')
args = parser.parse_args(argv)
params = vars(args).copy()
return hello(**params)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment