Skip to content

Instantly share code, notes, and snippets.

@darvil82
Last active July 26, 2022 15:50
Show Gist options
  • Save darvil82/97e6a5d44591348b086dd1c175422e3b to your computer and use it in GitHub Desktop.
Save darvil82/97e6a5d44591348b086dd1c175422e3b to your computer and use it in GitHub Desktop.
Finally non cringe entry points!
# utils.py
from sys import argv
from typing import Any, Callable
def entry_point(
func: Callable[[list[str]], Any] | Callable[[], Any]
) -> Callable[[], Any]:
new_func = lambda: func(argv) if func.__code__.co_argcount == 1 else func() # type: ignore
if func.__module__ == "__main__":
new_func()
return new_func
# runner.py
from utils import entry_point
@entry_point
def main(argv: list[str]):
print(argv)
@entry_point
def main2():
print("nice")
# `if __name__ == "__main__": ...`? ew... ugly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment